1
1
import os
2
+ import uuid
3
+ from threading import Thread
2
4
5
+ import gooey_gui as gui
3
6
import openai
4
7
from fastapi import FastAPI
5
8
6
- import gooey_gui as gui
7
-
8
9
app = FastAPI ()
9
10
10
11
11
- @gui .route (app , "/" )
12
+ @gui .route (app , "/poem/ " )
12
13
def root ():
13
14
gui .write ("### Poem Generator" )
14
15
prompt = gui .text_input (
15
16
"What kind of poem do you want to generate?" , value = "john lennon"
16
17
)
17
18
if gui .button ("Generate 🪄" ):
18
- # set the flag to indicate that the thread should be started
19
- gui .session_state ["generating_poem" ] = True
20
-
21
- if not gui .session_state .get ("generating_poem" ):
22
- # thread has not started yet, don't render anything
19
+ # a unique channel name for redis pubsub
20
+ gui .session_state ["channel" ] = channel = f"poem-generator/{ uuid .uuid4 ()} "
21
+ # start the thread
22
+ Thread (target = generate_poem_thread , args = [prompt , channel ]).start ()
23
+
24
+ channel = gui .session_state .get ("channel" )
25
+ if not channel :
26
+ # no channel, no need to subscribe
23
27
return
24
28
25
- # start the thread, or if already running, return the result
26
- result = gui .run_in_thread (
27
- generate_poem_thread ,
28
- args = [prompt ],
29
- placeholder = "Generating..." ,
30
- ## if cache=True, the thread will cache its return value and avoid from re-running multiple times for the same args
31
- # cache=True,
32
- )
29
+ # fetch updates from redis pubsub
30
+ result = gui .realtime_pull ([channel ])[0 ]
33
31
if result is None :
34
- # thread has not finished execution, don't render anything
32
+ # no result yet
33
+ gui .write ("Running Thread..." )
35
34
return
36
35
37
- # thread has finished execution, show result
36
+ # display result / loading message
38
37
gui .write (result )
39
38
40
- # avoid re-running the thread
41
- gui .session_state ["generating_poem" ] = False
39
+ ## optionally, stop subscribing from the channel and store result in session state
40
+ # gui.session_state.pop("channel")
41
+ # gui.session_state["poem"] = result
42
42
43
43
44
- def generate_poem_thread (prompt : str ) -> str :
44
+ def generate_poem_thread (prompt , channel ) :
45
45
openai .api_key = os .getenv ("OPENAI_API_KEY" )
46
46
47
47
completion = openai .chat .completions .create (
@@ -51,4 +51,7 @@ def generate_poem_thread(prompt: str) -> str:
51
51
{"role" : "user" , "content" : prompt },
52
52
],
53
53
)
54
- return completion .choices [0 ].message .content or ""
54
+ result = completion .choices [0 ].message .content or ""
55
+
56
+ # push the result to the channel + reload the UI
57
+ gui .realtime_push (channel , result )
0 commit comments