Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -103,10 +103,28 @@ def create_story_summary(chat_history):
|
|
| 103 |
}
|
| 104 |
return summary_instruction
|
| 105 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
def respond(message, chat_history, genre=None, use_full_memory=True):
|
| 107 |
"""Generate a response based on the current message and conversation history"""
|
| 108 |
system_message = get_enhanced_system_prompt(genre)
|
| 109 |
|
|
|
|
| 110 |
formatted_history = []
|
| 111 |
for user_msg, bot_msg in chat_history:
|
| 112 |
formatted_history.append({"role": "user", "content": user_msg})
|
|
@@ -114,8 +132,9 @@ def respond(message, chat_history, genre=None, use_full_memory=True):
|
|
| 114 |
|
| 115 |
api_messages = [{"role": "system", "content": system_message}]
|
| 116 |
|
|
|
|
| 117 |
if use_full_memory and formatted_history:
|
| 118 |
-
if len(formatted_history) > 20:
|
| 119 |
summary_instruction = create_story_summary(chat_history[:len(chat_history)-5])
|
| 120 |
if summary_instruction:
|
| 121 |
api_messages.append(summary_instruction)
|
|
@@ -130,8 +149,10 @@ def respond(message, chat_history, genre=None, use_full_memory=True):
|
|
| 130 |
for msg in formatted_history[-memory_length*2:]:
|
| 131 |
api_messages.append(msg)
|
| 132 |
|
|
|
|
| 133 |
api_messages.append({"role": "user", "content": message})
|
| 134 |
|
|
|
|
| 135 |
if not chat_history or message.lower() in ["start", "begin", "begin my adventure"]:
|
| 136 |
api_messages.append({
|
| 137 |
"role": "system",
|
|
@@ -150,12 +171,15 @@ def respond(message, chat_history, genre=None, use_full_memory=True):
|
|
| 150 |
delta = response_chunk.choices[0].delta.content
|
| 151 |
if delta:
|
| 152 |
bot_message += delta
|
|
|
|
| 153 |
new_history = chat_history.copy()
|
| 154 |
new_history.append((message, bot_message))
|
| 155 |
-
|
|
|
|
| 156 |
except Exception as e:
|
| 157 |
error_message = f"Story magic temporarily interrupted. Please try again. (Error: {str(e)})"
|
| 158 |
-
|
|
|
|
| 159 |
|
| 160 |
def save_story(chat_history):
|
| 161 |
"""Convert chat history to markdown for download"""
|
|
@@ -222,7 +246,6 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 222 |
def update_starter_buttons(selected_genre):
|
| 223 |
# Grab up to 4 examples from the chosen genre
|
| 224 |
examples = get_examples_for_genre(selected_genre)
|
| 225 |
-
# If there are fewer than 4, fill the rest with placeholders or hide them
|
| 226 |
button_updates = []
|
| 227 |
for i in range(4):
|
| 228 |
if i < len(examples):
|
|
@@ -233,9 +256,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 233 |
|
| 234 |
# Function that populates the msg with the chosen starter text, then calls respond
|
| 235 |
def pick_starter(starter_text, chat_history, selected_genre, memory_flag):
|
| 236 |
-
#
|
| 237 |
-
# but we also chain the respond function via .then
|
| 238 |
-
return starter_text
|
| 239 |
|
| 240 |
# Hook each starter button:
|
| 241 |
# 1) Put the chosen text into 'msg'
|
|
@@ -273,7 +294,6 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 273 |
save_btn = gr.Button("Download My Story", variant="secondary")
|
| 274 |
story_output = gr.Markdown(visible=False)
|
| 275 |
|
| 276 |
-
# "Download My Story" logic
|
| 277 |
save_btn.click(save_story, inputs=[chatbot], outputs=[story_output])
|
| 278 |
save_btn.click(
|
| 279 |
fn=lambda: True,
|
|
|
|
| 103 |
}
|
| 104 |
return summary_instruction
|
| 105 |
|
| 106 |
+
# NEW HELPER: Convert (user, bot) tuples into the "role/content" dictionaries Gradio needs
|
| 107 |
+
def format_history_for_gradio(history_tuples):
|
| 108 |
+
"""
|
| 109 |
+
Convert a list of (user_msg, bot_msg) tuples
|
| 110 |
+
into a list of dicts for 'type="messages"':
|
| 111 |
+
[
|
| 112 |
+
{"role": "user", "content": "foo"},
|
| 113 |
+
{"role": "assistant", "content": "bar"},
|
| 114 |
+
...
|
| 115 |
+
]
|
| 116 |
+
"""
|
| 117 |
+
messages = []
|
| 118 |
+
for user_msg, bot_msg in history_tuples:
|
| 119 |
+
messages.append({"role": "user", "content": user_msg})
|
| 120 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
| 121 |
+
return messages
|
| 122 |
+
|
| 123 |
def respond(message, chat_history, genre=None, use_full_memory=True):
|
| 124 |
"""Generate a response based on the current message and conversation history"""
|
| 125 |
system_message = get_enhanced_system_prompt(genre)
|
| 126 |
|
| 127 |
+
# Convert your existing (user, bot) history into a format for the API request
|
| 128 |
formatted_history = []
|
| 129 |
for user_msg, bot_msg in chat_history:
|
| 130 |
formatted_history.append({"role": "user", "content": user_msg})
|
|
|
|
| 132 |
|
| 133 |
api_messages = [{"role": "system", "content": system_message}]
|
| 134 |
|
| 135 |
+
# Use full memory or partial memory
|
| 136 |
if use_full_memory and formatted_history:
|
| 137 |
+
if len(formatted_history) > 20:
|
| 138 |
summary_instruction = create_story_summary(chat_history[:len(chat_history)-5])
|
| 139 |
if summary_instruction:
|
| 140 |
api_messages.append(summary_instruction)
|
|
|
|
| 149 |
for msg in formatted_history[-memory_length*2:]:
|
| 150 |
api_messages.append(msg)
|
| 151 |
|
| 152 |
+
# Add current user message
|
| 153 |
api_messages.append({"role": "user", "content": message})
|
| 154 |
|
| 155 |
+
# Special handling for story initialization
|
| 156 |
if not chat_history or message.lower() in ["start", "begin", "begin my adventure"]:
|
| 157 |
api_messages.append({
|
| 158 |
"role": "system",
|
|
|
|
| 171 |
delta = response_chunk.choices[0].delta.content
|
| 172 |
if delta:
|
| 173 |
bot_message += delta
|
| 174 |
+
# Keep storing your conversation as (user, bot) tuples
|
| 175 |
new_history = chat_history.copy()
|
| 176 |
new_history.append((message, bot_message))
|
| 177 |
+
# Yield in the new dictionary format for Gradio
|
| 178 |
+
yield format_history_for_gradio(new_history)
|
| 179 |
except Exception as e:
|
| 180 |
error_message = f"Story magic temporarily interrupted. Please try again. (Error: {str(e)})"
|
| 181 |
+
broken_history = chat_history + [(message, error_message)]
|
| 182 |
+
yield format_history_for_gradio(broken_history)
|
| 183 |
|
| 184 |
def save_story(chat_history):
|
| 185 |
"""Convert chat history to markdown for download"""
|
|
|
|
| 246 |
def update_starter_buttons(selected_genre):
|
| 247 |
# Grab up to 4 examples from the chosen genre
|
| 248 |
examples = get_examples_for_genre(selected_genre)
|
|
|
|
| 249 |
button_updates = []
|
| 250 |
for i in range(4):
|
| 251 |
if i < len(examples):
|
|
|
|
| 256 |
|
| 257 |
# Function that populates the msg with the chosen starter text, then calls respond
|
| 258 |
def pick_starter(starter_text, chat_history, selected_genre, memory_flag):
|
| 259 |
+
return starter_text # This goes into 'msg'
|
|
|
|
|
|
|
| 260 |
|
| 261 |
# Hook each starter button:
|
| 262 |
# 1) Put the chosen text into 'msg'
|
|
|
|
| 294 |
save_btn = gr.Button("Download My Story", variant="secondary")
|
| 295 |
story_output = gr.Markdown(visible=False)
|
| 296 |
|
|
|
|
| 297 |
save_btn.click(save_story, inputs=[chatbot], outputs=[story_output])
|
| 298 |
save_btn.click(
|
| 299 |
fn=lambda: True,
|