Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -124,8 +124,11 @@ def create_story_summary(chat_history):
|
|
| 124 |
return None
|
| 125 |
|
| 126 |
story_text = ""
|
| 127 |
-
for
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
summary_instruction = {
|
| 131 |
"role": "system",
|
|
@@ -133,13 +136,8 @@ def create_story_summary(chat_history):
|
|
| 133 |
}
|
| 134 |
return summary_instruction
|
| 135 |
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
return [(str(user_msg), str(bot_msg)) for user_msg, bot_msg in history_tuples]
|
| 139 |
-
|
| 140 |
-
# 1. Add type hints for better code maintainability
|
| 141 |
-
# 4. Add input validation
|
| 142 |
-
def respond(message: str, chat_history: List[Tuple[str, str]], genre: Optional[str] = None, use_full_memory: bool = True) -> Tuple[str, List[Tuple[str, str]]]:
|
| 143 |
"""Generate a response based on the current message and conversation history."""
|
| 144 |
if not message.strip():
|
| 145 |
return "", chat_history
|
|
@@ -149,13 +147,10 @@ def respond(message: str, chat_history: List[Tuple[str, str]], genre: Optional[s
|
|
| 149 |
api_messages = [{"role": "system", "content": get_enhanced_system_prompt(genre)}]
|
| 150 |
logging.debug(f"System Message: {api_messages[0]}")
|
| 151 |
|
| 152 |
-
# Add chat history
|
| 153 |
if chat_history and use_full_memory:
|
| 154 |
-
for
|
| 155 |
-
api_messages.
|
| 156 |
-
{"role": "user", "content": str(user_msg)},
|
| 157 |
-
{"role": "assistant", "content": str(bot_msg)}
|
| 158 |
-
])
|
| 159 |
logging.debug(f"Chat History Messages: {api_messages[1:]}")
|
| 160 |
|
| 161 |
# Add current message
|
|
@@ -176,14 +171,20 @@ def respond(message: str, chat_history: List[Tuple[str, str]], genre: Optional[s
|
|
| 176 |
bot_message = response.choices[0].message.content
|
| 177 |
logging.debug(f"Bot Response: {bot_message[:100]}...") # First 100 chars
|
| 178 |
|
| 179 |
-
# Update history
|
| 180 |
-
updated_history = chat_history + [
|
|
|
|
|
|
|
|
|
|
| 181 |
return "", updated_history
|
| 182 |
|
| 183 |
except Exception as e:
|
| 184 |
logging.error("Error in respond function", exc_info=True)
|
| 185 |
error_msg = f"Story magic temporarily interrupted. Please try again. (Error: {str(e)})"
|
| 186 |
-
return "", chat_history + [
|
|
|
|
|
|
|
|
|
|
| 187 |
|
| 188 |
def save_story(chat_history):
|
| 189 |
"""Convert chat history to markdown for download"""
|
|
@@ -191,31 +192,67 @@ def save_story(chat_history):
|
|
| 191 |
return "No story to save yet!"
|
| 192 |
|
| 193 |
story_text = "# My Interactive Adventure\n\n"
|
| 194 |
-
for
|
| 195 |
-
|
| 196 |
-
|
|
|
|
|
|
|
| 197 |
|
| 198 |
return story_text
|
| 199 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 201 |
gr.Markdown("# 🔮 Interactive Story Time")
|
| 202 |
with gr.Row():
|
| 203 |
status_message = gr.Markdown("Ready to begin your adventure...", visible=True)
|
| 204 |
gr.Markdown("Create a completely unique literary world, one choice at a time. Dare to explore the unknown.")
|
| 205 |
|
|
|
|
|
|
|
| 206 |
with gr.Row():
|
| 207 |
with gr.Column(scale=3):
|
| 208 |
-
# Chat window + user input
|
| 209 |
chatbot = gr.Chatbot(
|
| 210 |
-
height=500,
|
| 211 |
-
bubble_full_width=True,
|
| 212 |
show_copy_button=True,
|
| 213 |
-
avatar_images=(None,
|
| 214 |
-
type="messages"
|
| 215 |
container=True,
|
| 216 |
scale=1,
|
| 217 |
-
min_width=800,
|
| 218 |
-
value=[],
|
| 219 |
render=True
|
| 220 |
)
|
| 221 |
msg = gr.Textbox(
|
|
@@ -280,14 +317,17 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 280 |
|
| 281 |
except Exception as e:
|
| 282 |
error_msg = f"Story magic temporarily interrupted. Please try again. (Error: {str(e)})"
|
| 283 |
-
return "", history + [
|
|
|
|
|
|
|
|
|
|
| 284 |
|
| 285 |
# Simplified button connections
|
| 286 |
for starter_button in starter_buttons:
|
| 287 |
starter_button.click(
|
| 288 |
fn=use_starter,
|
| 289 |
inputs=[starter_button, chatbot, genre, full_memory],
|
| 290 |
-
outputs=[msg, chatbot],
|
| 291 |
queue=True
|
| 292 |
)
|
| 293 |
|
|
@@ -302,12 +342,12 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 302 |
msg.submit(
|
| 303 |
fn=respond,
|
| 304 |
inputs=[msg, chatbot, genre, full_memory],
|
| 305 |
-
outputs=[msg, chatbot]
|
| 306 |
)
|
| 307 |
submit.click(
|
| 308 |
fn=respond,
|
| 309 |
inputs=[msg, chatbot, genre, full_memory],
|
| 310 |
-
outputs=[msg, chatbot]
|
| 311 |
)
|
| 312 |
|
| 313 |
# Clear the chatbot for a new adventure
|
|
@@ -344,4 +384,4 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 344 |
|
| 345 |
# Run the app
|
| 346 |
if __name__ == "__main__":
|
| 347 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 124 |
return None
|
| 125 |
|
| 126 |
story_text = ""
|
| 127 |
+
for msg in chat_history:
|
| 128 |
+
if msg["role"] == "user":
|
| 129 |
+
story_text += f"User: {msg['content']}\n"
|
| 130 |
+
else:
|
| 131 |
+
story_text += f"Story: {msg['content']}\n\n"
|
| 132 |
|
| 133 |
summary_instruction = {
|
| 134 |
"role": "system",
|
|
|
|
| 136 |
}
|
| 137 |
return summary_instruction
|
| 138 |
|
| 139 |
+
# Modified function to handle message format
|
| 140 |
+
def respond(message: str, chat_history: List[Dict], genre: Optional[str] = None, use_full_memory: bool = True) -> Tuple[str, List[Dict]]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
"""Generate a response based on the current message and conversation history."""
|
| 142 |
if not message.strip():
|
| 143 |
return "", chat_history
|
|
|
|
| 147 |
api_messages = [{"role": "system", "content": get_enhanced_system_prompt(genre)}]
|
| 148 |
logging.debug(f"System Message: {api_messages[0]}")
|
| 149 |
|
| 150 |
+
# Add chat history - convert to API format
|
| 151 |
if chat_history and use_full_memory:
|
| 152 |
+
for msg in chat_history[-MEMORY_WINDOW*2:]: # Multiply by 2 since we have user and assistant messages
|
| 153 |
+
api_messages.append({"role": msg["role"], "content": msg["content"]})
|
|
|
|
|
|
|
|
|
|
| 154 |
logging.debug(f"Chat History Messages: {api_messages[1:]}")
|
| 155 |
|
| 156 |
# Add current message
|
|
|
|
| 171 |
bot_message = response.choices[0].message.content
|
| 172 |
logging.debug(f"Bot Response: {bot_message[:100]}...") # First 100 chars
|
| 173 |
|
| 174 |
+
# Update history with proper message format
|
| 175 |
+
updated_history = chat_history + [
|
| 176 |
+
{"role": "user", "content": message},
|
| 177 |
+
{"role": "assistant", "content": bot_message}
|
| 178 |
+
]
|
| 179 |
return "", updated_history
|
| 180 |
|
| 181 |
except Exception as e:
|
| 182 |
logging.error("Error in respond function", exc_info=True)
|
| 183 |
error_msg = f"Story magic temporarily interrupted. Please try again. (Error: {str(e)})"
|
| 184 |
+
return "", chat_history + [
|
| 185 |
+
{"role": "user", "content": message},
|
| 186 |
+
{"role": "assistant", "content": error_msg}
|
| 187 |
+
]
|
| 188 |
|
| 189 |
def save_story(chat_history):
|
| 190 |
"""Convert chat history to markdown for download"""
|
|
|
|
| 192 |
return "No story to save yet!"
|
| 193 |
|
| 194 |
story_text = "# My Interactive Adventure\n\n"
|
| 195 |
+
for msg in chat_history:
|
| 196 |
+
if msg["role"] == "user":
|
| 197 |
+
story_text += f"**Player:** {msg['content']}\n\n"
|
| 198 |
+
elif msg["role"] == "assistant":
|
| 199 |
+
story_text += f"**Story:** {msg['content']}\n\n---\n\n"
|
| 200 |
|
| 201 |
return story_text
|
| 202 |
|
| 203 |
+
# Add this function to get SVG for avatar
|
| 204 |
+
def get_storyteller_avatar():
|
| 205 |
+
"""Return SVG for storyteller avatar"""
|
| 206 |
+
return """
|
| 207 |
+
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
|
| 208 |
+
<!-- Wizard hat -->
|
| 209 |
+
<path d="M50 10 L70 40 L30 40 Z" fill="#4b0082" stroke="#000" stroke-width="1"/>
|
| 210 |
+
<!-- Hat band -->
|
| 211 |
+
<rect x="30" y="38" width="40" height="5" fill="#ffd700" stroke="#000" stroke-width="1"/>
|
| 212 |
+
<!-- Hat stars -->
|
| 213 |
+
<path d="M40 25 L42 30 L37 27 L43 27 L38 30 Z" fill="#ffd700"/>
|
| 214 |
+
<path d="M60 30 L62 35 L57 32 L63 32 L58 35 Z" fill="#ffd700"/>
|
| 215 |
+
<!-- Face -->
|
| 216 |
+
<circle cx="50" cy="60" r="20" fill="#fbe8d3" stroke="#000" stroke-width="1"/>
|
| 217 |
+
<!-- Eyes -->
|
| 218 |
+
<ellipse cx="40" cy="55" rx="3" ry="4" fill="#fff" stroke="#000" stroke-width="1"/>
|
| 219 |
+
<ellipse cx="60" cy="55" rx="3" ry="4" fill="#fff" stroke="#000" stroke-width="1"/>
|
| 220 |
+
<circle cx="40" cy="55" r="1" fill="#000"/>
|
| 221 |
+
<circle cx="60" cy="55" r="1" fill="#000"/>
|
| 222 |
+
<!-- Eyebrows -->
|
| 223 |
+
<path d="M36 50 Q40 48 44 50" fill="none" stroke="#000" stroke-width="1"/>
|
| 224 |
+
<path d="M56 50 Q60 48 64 50" fill="none" stroke="#000" stroke-width="1"/>
|
| 225 |
+
<!-- Beard -->
|
| 226 |
+
<path d="M30 60 Q50 90 70 60" fill="#7c6e5c" stroke="#000" stroke-width="1"/>
|
| 227 |
+
<!-- Mouth -->
|
| 228 |
+
<path d="M45 65 Q50 70 55 65" fill="none" stroke="#000" stroke-width="1"/>
|
| 229 |
+
<!-- Sparkle -->
|
| 230 |
+
<circle cx="75" cy="25" r="3" fill="#ffd700"/>
|
| 231 |
+
<path d="M75 20 L75 30 M70 25 L80 25" stroke="#ffd700" stroke-width="1"/>
|
| 232 |
+
</svg>
|
| 233 |
+
"""
|
| 234 |
+
|
| 235 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 236 |
gr.Markdown("# 🔮 Interactive Story Time")
|
| 237 |
with gr.Row():
|
| 238 |
status_message = gr.Markdown("Ready to begin your adventure...", visible=True)
|
| 239 |
gr.Markdown("Create a completely unique literary world, one choice at a time. Dare to explore the unknown.")
|
| 240 |
|
| 241 |
+
wizard_avatar = get_storyteller_avatar()
|
| 242 |
+
|
| 243 |
with gr.Row():
|
| 244 |
with gr.Column(scale=3):
|
| 245 |
+
# Chat window + user input - FIXED MESSAGE FORMAT
|
| 246 |
chatbot = gr.Chatbot(
|
| 247 |
+
height=500,
|
| 248 |
+
bubble_full_width=True,
|
| 249 |
show_copy_button=True,
|
| 250 |
+
avatar_images=(None, wizard_avatar), # Added wizard avatar
|
| 251 |
+
type="chatbot", # Changed from "messages" to "chatbot"
|
| 252 |
container=True,
|
| 253 |
scale=1,
|
| 254 |
+
min_width=800,
|
| 255 |
+
value=[],
|
| 256 |
render=True
|
| 257 |
)
|
| 258 |
msg = gr.Textbox(
|
|
|
|
| 317 |
|
| 318 |
except Exception as e:
|
| 319 |
error_msg = f"Story magic temporarily interrupted. Please try again. (Error: {str(e)})"
|
| 320 |
+
return "", history + [
|
| 321 |
+
{"role": "user", "content": starter_text},
|
| 322 |
+
{"role": "assistant", "content": error_msg}
|
| 323 |
+
]
|
| 324 |
|
| 325 |
# Simplified button connections
|
| 326 |
for starter_button in starter_buttons:
|
| 327 |
starter_button.click(
|
| 328 |
fn=use_starter,
|
| 329 |
inputs=[starter_button, chatbot, genre, full_memory],
|
| 330 |
+
outputs=[msg, chatbot],
|
| 331 |
queue=True
|
| 332 |
)
|
| 333 |
|
|
|
|
| 342 |
msg.submit(
|
| 343 |
fn=respond,
|
| 344 |
inputs=[msg, chatbot, genre, full_memory],
|
| 345 |
+
outputs=[msg, chatbot]
|
| 346 |
)
|
| 347 |
submit.click(
|
| 348 |
fn=respond,
|
| 349 |
inputs=[msg, chatbot, genre, full_memory],
|
| 350 |
+
outputs=[msg, chatbot]
|
| 351 |
)
|
| 352 |
|
| 353 |
# Clear the chatbot for a new adventure
|
|
|
|
| 384 |
|
| 385 |
# Run the app
|
| 386 |
if __name__ == "__main__":
|
| 387 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|