Abbasid commited on
Commit
ea32eab
·
verified ·
1 Parent(s): 1fd0bdd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -20
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 and the current step
135
- workflow, current_step_name, inventory_text = game_state
136
 
137
  # On the first turn, initialize the workflow
138
  if workflow is None:
139
  workflow = StorytellerWorkflow()
140
- current_step_name = "generate_story_part"
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 and run the next step
145
- result_event = await workflow.run_step("generate_story_part", UserChoice(choice=user_input))
146
-
147
- # Process the result from the workflow
148
- if isinstance(result_event, StoryEnd):
149
- return None, result_event.final_message, "", (None, None, None)
150
-
151
- if isinstance(result_event, StoryContext):
152
- narrative, choices = result_event.story_part.split("Choices:", 1)
 
 
 
 
 
 
 
 
 
 
 
153
  story_display = f"{textwrap.fill(narrative, width=80)}\n\nChoices:{choices}"
154
 
155
  image_path = None
156
- if result_event.is_new_scene and HF_TOKEN:
157
  image_path = await generate_image(narrative, HF_TOKEN)
158
 
159
- inventory_text = f"**Inventory:** {', '.join(result_event.inventory) if result_event.inventory else 'Empty'}"
160
 
161
- return image_path, story_display, inventory_text, (workflow, "generate_story_part", inventory_text)
162
 
163
- # Should not happen, but a graceful fallback
164
- return None, "An unexpected error occurred.", "", game_state
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: