Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -141,7 +141,10 @@ class StorytellerWorkflow(Workflow):
|
|
141 |
# The only change is in the function definition line
|
142 |
# app.py
|
143 |
|
144 |
-
|
|
|
|
|
|
|
145 |
if game_state is None:
|
146 |
game_state = {'inventory': [], 'last_story_part': None}
|
147 |
event = StartEvent()
|
@@ -156,12 +159,8 @@ async def run_turn(user_input, game_state, image_display, story_display, invento
|
|
156 |
result_event = await workflow.generate_story_part(event, ctx)
|
157 |
|
158 |
if isinstance(result_event, StoryEnd):
|
159 |
-
|
160 |
-
|
161 |
-
story_display: result_event.final_message,
|
162 |
-
inventory_display: "",
|
163 |
-
game_state: None
|
164 |
-
}
|
165 |
return
|
166 |
|
167 |
if isinstance(result_event, StoryContext):
|
@@ -174,25 +173,22 @@ async def run_turn(user_input, game_state, image_display, story_display, invento
|
|
174 |
}
|
175 |
inventory_text = f"**Inventory:** {', '.join(new_game_state['inventory']) if new_game_state['inventory'] else 'Empty'}"
|
176 |
|
177 |
-
# --- THIS IS THE FINAL, CORRECTED LOGIC ---
|
178 |
|
179 |
-
# 1. Instantly yield
|
180 |
-
|
181 |
-
|
182 |
-
inventory_display: inventory_text,
|
183 |
-
}
|
184 |
|
185 |
# 2. Generate the image.
|
186 |
image_path = None
|
187 |
if HF_TOKEN:
|
188 |
image_path = await generate_image(narrative, HF_TOKEN)
|
189 |
|
190 |
-
# 3. Yield
|
191 |
-
yield
|
192 |
-
image_display: image_path,
|
193 |
-
game_state: new_game_state,
|
194 |
-
}
|
195 |
|
|
|
|
|
196 |
def create_demo():
|
197 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
198 |
game_state = gr.State(None)
|
@@ -208,9 +204,8 @@ def create_demo():
|
|
208 |
story_display = gr.Textbox(label="Story", lines=15, interactive=False)
|
209 |
user_input = gr.Textbox(label="What do you do?", placeholder="Type your choice and press Enter...")
|
210 |
|
211 |
-
#
|
212 |
-
|
213 |
-
inputs = [user_input, game_state, image_display, story_display, inventory_display]
|
214 |
outputs = [image_display, story_display, inventory_display, game_state]
|
215 |
|
216 |
user_input.submit(
|
@@ -219,8 +214,7 @@ def create_demo():
|
|
219 |
outputs=outputs
|
220 |
)
|
221 |
|
222 |
-
|
223 |
-
load_inputs = [gr.State(None), game_state, image_display, story_display, inventory_display]
|
224 |
demo.load(
|
225 |
fn=run_turn,
|
226 |
inputs=load_inputs,
|
|
|
141 |
# The only change is in the function definition line
|
142 |
# app.py
|
143 |
|
144 |
+
# This is the final and correct version of the main application logic function.
|
145 |
+
|
146 |
+
async def run_turn(user_input, game_state):
|
147 |
+
# This function no longer needs the component objects passed in.
|
148 |
if game_state is None:
|
149 |
game_state = {'inventory': [], 'last_story_part': None}
|
150 |
event = StartEvent()
|
|
|
159 |
result_event = await workflow.generate_story_part(event, ctx)
|
160 |
|
161 |
if isinstance(result_event, StoryEnd):
|
162 |
+
# Return a single tuple of 4 values
|
163 |
+
yield (None, result_event.final_message, "", None)
|
|
|
|
|
|
|
|
|
164 |
return
|
165 |
|
166 |
if isinstance(result_event, StoryContext):
|
|
|
173 |
}
|
174 |
inventory_text = f"**Inventory:** {', '.join(new_game_state['inventory']) if new_game_state['inventory'] else 'Empty'}"
|
175 |
|
176 |
+
# --- THIS IS THE FINAL, CORRECTED YIELD LOGIC ---
|
177 |
|
178 |
+
# 1. Instantly yield a full tuple with a placeholder for the image.
|
179 |
+
# This provides all 4 required output values.
|
180 |
+
yield (None, story_display_text, inventory_text, new_game_state)
|
|
|
|
|
181 |
|
182 |
# 2. Generate the image.
|
183 |
image_path = None
|
184 |
if HF_TOKEN:
|
185 |
image_path = await generate_image(narrative, HF_TOKEN)
|
186 |
|
187 |
+
# 3. Yield a second, complete tuple with the new image path.
|
188 |
+
yield (image_path, story_display_text, inventory_text, new_game_state)
|
|
|
|
|
|
|
189 |
|
190 |
+
# The create_demo function should look like this.
|
191 |
+
|
192 |
def create_demo():
|
193 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
194 |
game_state = gr.State(None)
|
|
|
204 |
story_display = gr.Textbox(label="Story", lines=15, interactive=False)
|
205 |
user_input = gr.Textbox(label="What do you do?", placeholder="Type your choice and press Enter...")
|
206 |
|
207 |
+
# The inputs and outputs lists should be simple.
|
208 |
+
inputs = [user_input, game_state]
|
|
|
209 |
outputs = [image_display, story_display, inventory_display, game_state]
|
210 |
|
211 |
user_input.submit(
|
|
|
214 |
outputs=outputs
|
215 |
)
|
216 |
|
217 |
+
load_inputs = [gr.State(None), game_state]
|
|
|
218 |
demo.load(
|
219 |
fn=run_turn,
|
220 |
inputs=load_inputs,
|