Skip to content

Commit 35bac0d

Browse files
committed
update poem_gen example
1 parent 202234f commit 35bac0d

File tree

1 file changed

+25
-22
lines changed

1 file changed

+25
-22
lines changed

py/examples/poem_gen_thread.py

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,47 @@
11
import os
2+
import uuid
3+
from threading import Thread
24

5+
import gooey_gui as gui
36
import openai
47
from fastapi import FastAPI
58

6-
import gooey_gui as gui
7-
89
app = FastAPI()
910

1011

11-
@gui.route(app, "/")
12+
@gui.route(app, "/poem/")
1213
def root():
1314
gui.write("### Poem Generator")
1415
prompt = gui.text_input(
1516
"What kind of poem do you want to generate?", value="john lennon"
1617
)
1718
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
2327
return
2428

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]
3331
if result is None:
34-
# thread has not finished execution, don't render anything
32+
# no result yet
33+
gui.write("Running Thread...")
3534
return
3635

37-
# thread has finished execution, show result
36+
# display result / loading message
3837
gui.write(result)
3938

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
4242

4343

44-
def generate_poem_thread(prompt: str) -> str:
44+
def generate_poem_thread(prompt, channel):
4545
openai.api_key = os.getenv("OPENAI_API_KEY")
4646

4747
completion = openai.chat.completions.create(
@@ -51,4 +51,7 @@ def generate_poem_thread(prompt: str) -> str:
5151
{"role": "user", "content": prompt},
5252
],
5353
)
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

Comments
 (0)