Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -129,39 +129,47 @@ class StorytellerWorkflow(Workflow):
|
|
129 |
|
130 |
|
131 |
# --- Gradio UI and Application Logic ---
|
132 |
-
|
133 |
async def run_turn(user_input, game_state):
|
134 |
-
# game_state holds our workflow instance
|
135 |
-
workflow
|
136 |
|
137 |
# On the first turn, initialize the workflow
|
138 |
if workflow is None:
|
139 |
workflow = StorytellerWorkflow()
|
140 |
-
|
141 |
-
# Manually create the first event to kick off the workflow
|
142 |
-
result_event = await workflow.run_step(current_step_name, StartEvent())
|
143 |
else:
|
144 |
-
# For subsequent turns, create a UserChoice event
|
145 |
-
|
146 |
-
|
147 |
-
#
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
153 |
story_display = f"{textwrap.fill(narrative, width=80)}\n\nChoices:{choices}"
|
154 |
|
155 |
image_path = None
|
156 |
-
if
|
157 |
image_path = await generate_image(narrative, HF_TOKEN)
|
158 |
|
159 |
-
inventory_text = f"**Inventory:** {', '.join(
|
160 |
|
161 |
-
return image_path, story_display, inventory_text,
|
162 |
|
163 |
-
#
|
164 |
-
return None, "An unexpected error occurred.", "",
|
165 |
|
166 |
def create_demo():
|
167 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
|
129 |
|
130 |
|
131 |
# --- Gradio UI and Application Logic ---
|
|
|
132 |
async def run_turn(user_input, game_state):
|
133 |
+
# game_state holds our workflow instance
|
134 |
+
workflow = game_state
|
135 |
|
136 |
# On the first turn, initialize the workflow
|
137 |
if workflow is None:
|
138 |
workflow = StorytellerWorkflow()
|
139 |
+
event = StartEvent()
|
|
|
|
|
140 |
else:
|
141 |
+
# For subsequent turns, create a UserChoice event
|
142 |
+
event = UserChoice(choice=user_input)
|
143 |
+
|
144 |
+
# --- THIS IS THE CORRECTED LOGIC ---
|
145 |
+
# We use stream() to run the workflow until the next input is needed
|
146 |
+
final_event = None
|
147 |
+
async for ev in workflow.stream(event):
|
148 |
+
# The loop will run through generate_story_part and stop when it produces
|
149 |
+
# either a StoryEnd event or a StoryContext event, which triggers the next step.
|
150 |
+
# Since the next step (which we removed) would require user input, the stream pauses.
|
151 |
+
final_event = ev.data
|
152 |
+
|
153 |
+
# Process the result from the workflow stream
|
154 |
+
if isinstance(final_event, StoryEnd):
|
155 |
+
# We need to manually call the end_story step to get the final message
|
156 |
+
stop_event = workflow.end_story(final_event)
|
157 |
+
return None, stop_event.result, "", None # Reset the state
|
158 |
+
|
159 |
+
if isinstance(final_event, StoryContext):
|
160 |
+
narrative, choices = final_event.story_part.split("Choices:", 1)
|
161 |
story_display = f"{textwrap.fill(narrative, width=80)}\n\nChoices:{choices}"
|
162 |
|
163 |
image_path = None
|
164 |
+
if final_event.is_new_scene and HF_TOKEN:
|
165 |
image_path = await generate_image(narrative, HF_TOKEN)
|
166 |
|
167 |
+
inventory_text = f"**Inventory:** {', '.join(final_event.inventory) if final_event.inventory else 'Empty'}"
|
168 |
|
169 |
+
return image_path, story_display, inventory_text, workflow # Pass the workflow instance to the next turn
|
170 |
|
171 |
+
# Graceful fallback
|
172 |
+
return None, "An unexpected error occurred. The story cannot continue.", "", None
|
173 |
|
174 |
def create_demo():
|
175 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|