Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -78,16 +78,22 @@ class StorytellerWorkflow(Workflow):
|
|
78 |
"""
|
79 |
elif isinstance(ev, UserChoice):
|
80 |
last_story_part = await ctx.store.get("last_story_part")
|
81 |
-
# ---
|
82 |
-
# Removed the instruction about [NEW_SCENE]
|
83 |
prompt = f"""
|
84 |
You are a creative text adventure game master.
|
85 |
The story so far: "{last_story_part}"
|
86 |
The player chose: "{ev.choice}"
|
87 |
The player's inventory: {inventory}
|
|
|
88 |
Continue the story.
|
89 |
If a choice results in an item, use `[ADD_ITEM: item name]`. If the story should end, write "[END]".
|
90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
"""
|
92 |
|
93 |
llm = Groq(model="llama3-8b-8192", api_key=GROQ_API_KEY)
|
@@ -164,8 +170,15 @@ async def run_turn(user_input, game_state):
|
|
164 |
return
|
165 |
|
166 |
if isinstance(result_event, StoryContext):
|
167 |
-
narrative,
|
168 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
169 |
|
170 |
new_game_state = {
|
171 |
'inventory': result_event.inventory,
|
@@ -173,12 +186,9 @@ async def run_turn(user_input, game_state):
|
|
173 |
}
|
174 |
inventory_text = f"**Inventory:** {', '.join(new_game_state['inventory']) if new_game_state['inventory'] else 'Empty'}"
|
175 |
|
176 |
-
#
|
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:
|
|
|
78 |
"""
|
79 |
elif isinstance(ev, UserChoice):
|
80 |
last_story_part = await ctx.store.get("last_story_part")
|
81 |
+
# --- UPDATED, MORE EXPLICIT PROMPT ---
|
|
|
82 |
prompt = f"""
|
83 |
You are a creative text adventure game master.
|
84 |
The story so far: "{last_story_part}"
|
85 |
The player chose: "{ev.choice}"
|
86 |
The player's inventory: {inventory}
|
87 |
+
|
88 |
Continue the story.
|
89 |
If a choice results in an item, use `[ADD_ITEM: item name]`. If the story should end, write "[END]".
|
90 |
+
|
91 |
+
Format your response exactly like this:
|
92 |
+
STORY:
|
93 |
+
[The story text goes here]
|
94 |
+
CHOICES:
|
95 |
+
1. [First choice on its own line]
|
96 |
+
2. [Second choice on its own line]
|
97 |
"""
|
98 |
|
99 |
llm = Groq(model="llama3-8b-8192", api_key=GROQ_API_KEY)
|
|
|
170 |
return
|
171 |
|
172 |
if isinstance(result_event, StoryContext):
|
173 |
+
narrative, choices_text = result_event.story_part.split("Choices:", 1)
|
174 |
+
|
175 |
+
# --- THIS IS THE NEW DEFENSIVE CODE ---
|
176 |
+
# It finds any choice number (like " 2." or " 3.") that has a space before it
|
177 |
+
# and replaces that space with a newline character. This forces them to be on separate lines.
|
178 |
+
choices_text = re.sub(r" (\d\.)", r"\n\1", choices_text)
|
179 |
+
# --- END OF NEW CODE ---
|
180 |
+
|
181 |
+
story_display_text = f"{textwrap.fill(narrative, width=80)}\n\nChoices:{choices_text}"
|
182 |
|
183 |
new_game_state = {
|
184 |
'inventory': result_event.inventory,
|
|
|
186 |
}
|
187 |
inventory_text = f"**Inventory:** {', '.join(new_game_state['inventory']) if new_game_state['inventory'] else 'Empty'}"
|
188 |
|
189 |
+
# The rest of the function remains the same...
|
|
|
|
|
|
|
190 |
yield (None, story_display_text, inventory_text, new_game_state)
|
191 |
+
|
192 |
# 2. Generate the image.
|
193 |
image_path = None
|
194 |
if HF_TOKEN:
|