Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -141,54 +141,59 @@ def respond(
|
|
141 |
if not message.strip():
|
142 |
return chat_history
|
143 |
|
144 |
-
|
145 |
-
|
146 |
-
1. First, write 2-3 paragraphs (150-300 words) continuing the story based on the user's choice
|
147 |
-
2. Use vivid sensory details and second-person perspective ("you", "your")
|
148 |
-
3. Include dialogue and character thoughts
|
149 |
-
4. After the story segment, provide exactly three numbered choices for what to do next
|
150 |
-
5. Format the choices as:
|
151 |
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
|
|
159 |
|
160 |
-
|
|
|
161 |
for user_msg, bot_msg in chat_history[-MEMORY_WINDOW:]:
|
162 |
formatted_messages.extend([
|
163 |
{"role": "user", "content": str(user_msg)},
|
164 |
{"role": "assistant", "content": str(bot_msg)}
|
165 |
])
|
166 |
|
167 |
-
|
168 |
-
|
|
|
|
|
|
|
|
|
169 |
try:
|
170 |
bot_message = ""
|
|
|
|
|
171 |
for response_chunk in client.chat_completion(
|
172 |
formatted_messages,
|
173 |
max_tokens=MAX_TOKENS,
|
174 |
stream=True,
|
175 |
temperature=TEMPERATURE,
|
176 |
-
top_p=TOP_P
|
177 |
):
|
178 |
if hasattr(response_chunk.choices[0].delta, 'content'):
|
179 |
delta = response_chunk.choices[0].delta.content
|
180 |
if delta:
|
181 |
bot_message += delta
|
182 |
if len(bot_message.strip()) >= MIN_RESPONSE_LENGTH:
|
183 |
-
|
184 |
-
|
185 |
-
|
|
|
|
|
|
|
186 |
|
187 |
except Exception as e:
|
188 |
error_message = f"Story magic temporarily interrupted. Please try again. (Error: {str(e)})"
|
189 |
-
|
190 |
-
|
191 |
-
yield chat_history
|
192 |
|
193 |
def save_story(chat_history):
|
194 |
"""Convert chat history to markdown for download"""
|
@@ -279,7 +284,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
279 |
# 4) Connect each starter button:
|
280 |
for starter_button in starter_buttons:
|
281 |
starter_button.click(
|
282 |
-
fn=lambda x: (
|
283 |
inputs=[starter_button],
|
284 |
outputs=[chatbot],
|
285 |
queue=False
|
|
|
141 |
if not message.strip():
|
142 |
return chat_history
|
143 |
|
144 |
+
# Use the enhanced system prompt instead of the simple one
|
145 |
+
system_message = get_enhanced_system_prompt(genre)
|
|
|
|
|
|
|
|
|
|
|
146 |
|
147 |
+
# Format messages for the API
|
148 |
+
formatted_messages = []
|
149 |
+
|
150 |
+
# Add system message first
|
151 |
+
formatted_messages.append({
|
152 |
+
"role": "system",
|
153 |
+
"content": system_message
|
154 |
+
})
|
155 |
|
156 |
+
# Add chat history
|
157 |
+
if chat_history and use_full_memory:
|
158 |
for user_msg, bot_msg in chat_history[-MEMORY_WINDOW:]:
|
159 |
formatted_messages.extend([
|
160 |
{"role": "user", "content": str(user_msg)},
|
161 |
{"role": "assistant", "content": str(bot_msg)}
|
162 |
])
|
163 |
|
164 |
+
# Add current message
|
165 |
+
formatted_messages.append({
|
166 |
+
"role": "user",
|
167 |
+
"content": str(message)
|
168 |
+
})
|
169 |
+
|
170 |
try:
|
171 |
bot_message = ""
|
172 |
+
current_history = list(chat_history)
|
173 |
+
|
174 |
for response_chunk in client.chat_completion(
|
175 |
formatted_messages,
|
176 |
max_tokens=MAX_TOKENS,
|
177 |
stream=True,
|
178 |
temperature=TEMPERATURE,
|
179 |
+
top_p=TOP_P
|
180 |
):
|
181 |
if hasattr(response_chunk.choices[0].delta, 'content'):
|
182 |
delta = response_chunk.choices[0].delta.content
|
183 |
if delta:
|
184 |
bot_message += delta
|
185 |
if len(bot_message.strip()) >= MIN_RESPONSE_LENGTH:
|
186 |
+
new_history = current_history + [(message, bot_message)]
|
187 |
+
yield new_history
|
188 |
+
|
189 |
+
if bot_message: # Ensure final message is yielded
|
190 |
+
final_history = current_history + [(message, bot_message)]
|
191 |
+
yield final_history
|
192 |
|
193 |
except Exception as e:
|
194 |
error_message = f"Story magic temporarily interrupted. Please try again. (Error: {str(e)})"
|
195 |
+
error_history = current_history + [(message, error_message)]
|
196 |
+
yield error_history
|
|
|
197 |
|
198 |
def save_story(chat_history):
|
199 |
"""Convert chat history to markdown for download"""
|
|
|
284 |
# 4) Connect each starter button:
|
285 |
for starter_button in starter_buttons:
|
286 |
starter_button.click(
|
287 |
+
fn=lambda x: [(x, "")], # Changed to return properly formatted history
|
288 |
inputs=[starter_button],
|
289 |
outputs=[chatbot],
|
290 |
queue=False
|