Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -138,7 +138,8 @@ class StorytellerWorkflow(Workflow):
|
|
| 138 |
|
| 139 |
# app.py
|
| 140 |
|
| 141 |
-
|
|
|
|
| 142 |
if game_state is None:
|
| 143 |
game_state = {'inventory': [], 'last_story_part': None}
|
| 144 |
event = StartEvent()
|
|
@@ -163,7 +164,7 @@ async def run_turn(user_input, game_state):
|
|
| 163 |
|
| 164 |
if isinstance(result_event, StoryContext):
|
| 165 |
narrative, choices = result_event.story_part.split("Choices:", 1)
|
| 166 |
-
|
| 167 |
|
| 168 |
new_game_state = {
|
| 169 |
'inventory': result_event.inventory,
|
|
@@ -174,13 +175,12 @@ async def run_turn(user_input, game_state):
|
|
| 174 |
# Instantly yield the text
|
| 175 |
yield {
|
| 176 |
image_display: None,
|
| 177 |
-
story_display:
|
| 178 |
inventory_display: inventory_text,
|
| 179 |
game_state: new_game_state
|
| 180 |
}
|
| 181 |
|
| 182 |
-
#
|
| 183 |
-
# Removed the `if result_event.is_new_scene` condition
|
| 184 |
image_path = None
|
| 185 |
if HF_TOKEN:
|
| 186 |
image_path = await generate_image(narrative, HF_TOKEN)
|
|
@@ -193,7 +193,6 @@ async def run_turn(user_input, game_state):
|
|
| 193 |
|
| 194 |
def create_demo():
|
| 195 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 196 |
-
# State object to hold the workflow instance between turns
|
| 197 |
game_state = gr.State(None)
|
| 198 |
|
| 199 |
gr.Markdown("# LlamaIndex Workflow: Dynamic Storyteller")
|
|
@@ -207,21 +206,26 @@ def create_demo():
|
|
| 207 |
story_display = gr.Textbox(label="Story", lines=15, interactive=False)
|
| 208 |
user_input = gr.Textbox(label="What do you do?", placeholder="Type your choice and press Enter...")
|
| 209 |
|
| 210 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
user_input.submit(
|
| 212 |
fn=run_turn,
|
| 213 |
-
inputs=
|
| 214 |
-
outputs=
|
| 215 |
)
|
| 216 |
|
| 217 |
-
#
|
|
|
|
| 218 |
demo.load(
|
| 219 |
fn=run_turn,
|
| 220 |
-
inputs=
|
| 221 |
-
outputs=
|
| 222 |
)
|
| 223 |
return demo
|
| 224 |
-
|
| 225 |
if __name__ == "__main__":
|
| 226 |
if not GROQ_API_KEY or not HF_TOKEN:
|
| 227 |
print("ERROR: API keys not found. Make sure to set GROQ_API_KEY and HF_TOKEN in your Hugging Face Space Secrets.")
|
|
|
|
| 138 |
|
| 139 |
# app.py
|
| 140 |
|
| 141 |
+
# The only change is in the function definition line
|
| 142 |
+
async def run_turn(user_input, game_state, image_display, story_display, inventory_display):
|
| 143 |
if game_state is None:
|
| 144 |
game_state = {'inventory': [], 'last_story_part': None}
|
| 145 |
event = StartEvent()
|
|
|
|
| 164 |
|
| 165 |
if isinstance(result_event, StoryContext):
|
| 166 |
narrative, choices = result_event.story_part.split("Choices:", 1)
|
| 167 |
+
story_display_text = f"{textwrap.fill(narrative, width=80)}\n\nChoices:{choices}"
|
| 168 |
|
| 169 |
new_game_state = {
|
| 170 |
'inventory': result_event.inventory,
|
|
|
|
| 175 |
# Instantly yield the text
|
| 176 |
yield {
|
| 177 |
image_display: None,
|
| 178 |
+
story_display: story_display_text,
|
| 179 |
inventory_display: inventory_text,
|
| 180 |
game_state: new_game_state
|
| 181 |
}
|
| 182 |
|
| 183 |
+
# Generate the image
|
|
|
|
| 184 |
image_path = None
|
| 185 |
if HF_TOKEN:
|
| 186 |
image_path = await generate_image(narrative, HF_TOKEN)
|
|
|
|
| 193 |
|
| 194 |
def create_demo():
|
| 195 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
|
|
| 196 |
game_state = gr.State(None)
|
| 197 |
|
| 198 |
gr.Markdown("# LlamaIndex Workflow: Dynamic Storyteller")
|
|
|
|
| 206 |
story_display = gr.Textbox(label="Story", lines=15, interactive=False)
|
| 207 |
user_input = gr.Textbox(label="What do you do?", placeholder="Type your choice and press Enter...")
|
| 208 |
|
| 209 |
+
# Define the inputs and outputs lists
|
| 210 |
+
# --- THIS IS THE KEY CHANGE ---
|
| 211 |
+
inputs = [user_input, game_state, image_display, story_display, inventory_display]
|
| 212 |
+
outputs = [image_display, story_display, inventory_display, game_state]
|
| 213 |
+
|
| 214 |
user_input.submit(
|
| 215 |
fn=run_turn,
|
| 216 |
+
inputs=inputs,
|
| 217 |
+
outputs=outputs
|
| 218 |
)
|
| 219 |
|
| 220 |
+
# The `load` event doesn't have a `user_input`, so we pass None
|
| 221 |
+
load_inputs = [gr.State(None), game_state, image_display, story_display, inventory_display]
|
| 222 |
demo.load(
|
| 223 |
fn=run_turn,
|
| 224 |
+
inputs=load_inputs,
|
| 225 |
+
outputs=outputs
|
| 226 |
)
|
| 227 |
return demo
|
| 228 |
+
|
| 229 |
if __name__ == "__main__":
|
| 230 |
if not GROQ_API_KEY or not HF_TOKEN:
|
| 231 |
print("ERROR: API keys not found. Make sure to set GROQ_API_KEY and HF_TOKEN in your Hugging Face Space Secrets.")
|