Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -178,14 +178,17 @@ def respond(message, chat_history, genre=None, use_full_memory=True):
|
|
| 178 |
error_message = f"Story magic temporarily interrupted. Please try again. (Error: {str(e)})"
|
| 179 |
yield chat_history + [(message, error_message)]
|
| 180 |
|
| 181 |
-
def
|
| 182 |
-
"""
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
|
|
|
|
|
|
|
|
|
| 189 |
|
| 190 |
# Create interface with additional customization options
|
| 191 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
@@ -226,27 +229,52 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 226 |
info="When enabled, the AI will try to remember the entire story"
|
| 227 |
)
|
| 228 |
|
| 229 |
-
#
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 234 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
|
| 236 |
-
#
|
| 237 |
-
def
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
examples_textbox = gr.Textbox(
|
| 241 |
-
value=update_example_text("fantasy"),
|
| 242 |
-
label="Example Story Starters",
|
| 243 |
-
interactive=False
|
| 244 |
-
)
|
| 245 |
|
|
|
|
| 246 |
genre.change(
|
| 247 |
-
fn=
|
| 248 |
inputs=[genre],
|
| 249 |
-
outputs=
|
| 250 |
)
|
| 251 |
|
| 252 |
# Set up event handlers for the chatbot
|
|
@@ -258,17 +286,6 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 258 |
clear.click(lambda: "", None, msg, queue=False)
|
| 259 |
|
| 260 |
# Add a download story button
|
| 261 |
-
def save_story(chat_history):
|
| 262 |
-
if not chat_history:
|
| 263 |
-
return "No story to save yet!"
|
| 264 |
-
|
| 265 |
-
story_text = "# My Interactive Adventure\n\n"
|
| 266 |
-
for user_msg, bot_msg in chat_history:
|
| 267 |
-
story_text += f"**Player:** {user_msg}\n\n"
|
| 268 |
-
story_text += f"**Story:** {bot_msg}\n\n---\n\n"
|
| 269 |
-
|
| 270 |
-
return story_text
|
| 271 |
-
|
| 272 |
with gr.Row():
|
| 273 |
save_btn = gr.Button("Save Story as Markdown", variant="secondary")
|
| 274 |
story_output = gr.Markdown(visible=False)
|
|
|
|
| 178 |
error_message = f"Story magic temporarily interrupted. Please try again. (Error: {str(e)})"
|
| 179 |
yield chat_history + [(message, error_message)]
|
| 180 |
|
| 181 |
+
def save_story(chat_history):
|
| 182 |
+
"""Convert chat history to markdown for download"""
|
| 183 |
+
if not chat_history:
|
| 184 |
+
return "No story to save yet!"
|
| 185 |
+
|
| 186 |
+
story_text = "# My Interactive Adventure\n\n"
|
| 187 |
+
for user_msg, bot_msg in chat_history:
|
| 188 |
+
story_text += f"**Player:** {user_msg}\n\n"
|
| 189 |
+
story_text += f"**Story:** {bot_msg}\n\n---\n\n"
|
| 190 |
+
|
| 191 |
+
return story_text
|
| 192 |
|
| 193 |
# Create interface with additional customization options
|
| 194 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
|
|
| 229 |
info="When enabled, the AI will try to remember the entire story"
|
| 230 |
)
|
| 231 |
|
| 232 |
+
# Create story starter buttons that automatically submit
|
| 233 |
+
gr.Markdown("## Story Starters")
|
| 234 |
+
story_starters = []
|
| 235 |
+
|
| 236 |
+
def create_example_click_handler(example_text):
|
| 237 |
+
def example_click():
|
| 238 |
+
return example_text
|
| 239 |
+
return example_click
|
| 240 |
+
|
| 241 |
+
# Create a start button
|
| 242 |
+
start_btn = gr.Button("Begin my adventure", variant="secondary")
|
| 243 |
+
start_btn.click(
|
| 244 |
+
fn=create_example_click_handler("Begin my adventure"),
|
| 245 |
+
outputs=[msg],
|
| 246 |
+
queue=False
|
| 247 |
+
).then(
|
| 248 |
+
fn=respond,
|
| 249 |
+
inputs=[msg, chatbot, genre, full_memory],
|
| 250 |
+
outputs=[chatbot]
|
| 251 |
)
|
| 252 |
+
|
| 253 |
+
# Create example buttons for the current genre
|
| 254 |
+
example_buttons = []
|
| 255 |
+
for i, example in enumerate(get_examples_for_genre("fantasy")):
|
| 256 |
+
btn = gr.Button(example, elem_id=f"example_{i}")
|
| 257 |
+
btn.click(
|
| 258 |
+
fn=create_example_click_handler(example),
|
| 259 |
+
outputs=[msg],
|
| 260 |
+
queue=False
|
| 261 |
+
).then(
|
| 262 |
+
fn=respond,
|
| 263 |
+
inputs=[msg, chatbot, genre, full_memory],
|
| 264 |
+
outputs=[chatbot]
|
| 265 |
+
)
|
| 266 |
+
example_buttons.append(btn)
|
| 267 |
|
| 268 |
+
# Function to update example buttons when genre changes
|
| 269 |
+
def update_example_buttons(genre):
|
| 270 |
+
examples = get_examples_for_genre(genre)
|
| 271 |
+
return [gr.Button.update(value=example) for example in examples[:4]] # Limit to 4 examples
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 272 |
|
| 273 |
+
# Connect genre dropdown to update example buttons
|
| 274 |
genre.change(
|
| 275 |
+
fn=update_example_buttons,
|
| 276 |
inputs=[genre],
|
| 277 |
+
outputs=example_buttons
|
| 278 |
)
|
| 279 |
|
| 280 |
# Set up event handlers for the chatbot
|
|
|
|
| 286 |
clear.click(lambda: "", None, msg, queue=False)
|
| 287 |
|
| 288 |
# Add a download story button
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 289 |
with gr.Row():
|
| 290 |
save_btn = gr.Button("Save Story as Markdown", variant="secondary")
|
| 291 |
story_output = gr.Markdown(visible=False)
|