Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -125,8 +125,9 @@ def create_story_summary(chat_history):
|
|
125 |
return None
|
126 |
|
127 |
story_text = ""
|
128 |
-
for
|
129 |
-
|
|
|
130 |
|
131 |
summary_instruction = {
|
132 |
"role": "system",
|
@@ -134,8 +135,8 @@ def create_story_summary(chat_history):
|
|
134 |
}
|
135 |
return summary_instruction
|
136 |
|
137 |
-
# Modified function for proper Gradio format (
|
138 |
-
def respond(message: str, chat_history: List[
|
139 |
"""Generate a response based on the current message and conversation history."""
|
140 |
if not message.strip():
|
141 |
return "", chat_history
|
@@ -145,9 +146,11 @@ def respond(message: str, chat_history: List[Tuple[str, str]], genre: Optional[s
|
|
145 |
api_messages = [{"role": "system", "content": get_enhanced_system_prompt(genre)}]
|
146 |
logging.debug(f"System Message: {api_messages[0]}")
|
147 |
|
148 |
-
# Add chat history - convert from
|
149 |
if chat_history and use_full_memory:
|
150 |
-
|
|
|
|
|
151 |
api_messages.extend([
|
152 |
{"role": "user", "content": str(user_msg)},
|
153 |
{"role": "assistant", "content": str(bot_msg)}
|
@@ -172,14 +175,14 @@ def respond(message: str, chat_history: List[Tuple[str, str]], genre: Optional[s
|
|
172 |
bot_message = response.choices[0].message.content
|
173 |
logging.debug(f"Bot Response: {bot_message[:100]}...") # First 100 chars
|
174 |
|
175 |
-
# Update history using
|
176 |
-
updated_history = chat_history + [
|
177 |
return "", updated_history
|
178 |
|
179 |
except Exception as e:
|
180 |
logging.error("Error in respond function", exc_info=True)
|
181 |
error_msg = f"Story magic temporarily interrupted. Please try again. (Error: {str(e)})"
|
182 |
-
return "", chat_history + [
|
183 |
|
184 |
def save_story(chat_history):
|
185 |
"""Convert chat history to markdown and return as downloadable file"""
|
@@ -187,9 +190,11 @@ def save_story(chat_history):
|
|
187 |
return None
|
188 |
|
189 |
story_text = "# My Interactive Adventure\n\n"
|
190 |
-
|
191 |
-
|
192 |
-
|
|
|
|
|
193 |
|
194 |
# Return as a file for download
|
195 |
return story_text
|
@@ -216,17 +221,11 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
216 |
> **Tip:** The more detail you provide, the deeper the story becomes.
|
217 |
""")
|
218 |
|
219 |
-
# Remove the old status message and description
|
220 |
-
# with gr.Row():
|
221 |
-
# status_message = gr.Markdown("Ready to begin your adventure...", visible=True)
|
222 |
-
# gr.Markdown("Create a completely unique literary world...")
|
223 |
-
|
224 |
-
# Get avatar URL and continue with rest of interface
|
225 |
wizard_avatar = get_storyteller_avatar_url()
|
226 |
|
227 |
with gr.Row():
|
228 |
with gr.Column(scale=3):
|
229 |
-
# Chat window + user input - USING
|
230 |
chatbot = gr.Chatbot(
|
231 |
height=500,
|
232 |
bubble_full_width=True,
|
@@ -235,7 +234,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
235 |
container=True,
|
236 |
scale=1,
|
237 |
min_width=800,
|
238 |
-
value=[], # Empty list
|
239 |
render=True
|
240 |
)
|
241 |
msg = gr.Textbox(
|
@@ -300,7 +299,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
300 |
|
301 |
except Exception as e:
|
302 |
error_msg = f"Story magic temporarily interrupted. Please try again. (Error: {str(e)})"
|
303 |
-
return "", history + [
|
304 |
|
305 |
# Simplified button connections
|
306 |
for starter_button in starter_buttons:
|
|
|
125 |
return None
|
126 |
|
127 |
story_text = ""
|
128 |
+
for i in range(0, len(chat_history), 2):
|
129 |
+
if i+1 < len(chat_history):
|
130 |
+
story_text += f"User: {chat_history[i]}\nStory: {chat_history[i+1]}\n\n"
|
131 |
|
132 |
summary_instruction = {
|
133 |
"role": "system",
|
|
|
135 |
}
|
136 |
return summary_instruction
|
137 |
|
138 |
+
# Modified function for proper Gradio format (lists)
|
139 |
+
def respond(message: str, chat_history: List[str], genre: Optional[str] = None, use_full_memory: bool = True) -> Tuple[str, List[str]]:
|
140 |
"""Generate a response based on the current message and conversation history."""
|
141 |
if not message.strip():
|
142 |
return "", chat_history
|
|
|
146 |
api_messages = [{"role": "system", "content": get_enhanced_system_prompt(genre)}]
|
147 |
logging.debug(f"System Message: {api_messages[0]}")
|
148 |
|
149 |
+
# Add chat history - convert from lists to API format
|
150 |
if chat_history and use_full_memory:
|
151 |
+
# Take pairs of messages (user, bot) from history
|
152 |
+
history_pairs = list(zip(chat_history[::2], chat_history[1::2]))
|
153 |
+
for user_msg, bot_msg in history_pairs[-MEMORY_WINDOW:]:
|
154 |
api_messages.extend([
|
155 |
{"role": "user", "content": str(user_msg)},
|
156 |
{"role": "assistant", "content": str(bot_msg)}
|
|
|
175 |
bot_message = response.choices[0].message.content
|
176 |
logging.debug(f"Bot Response: {bot_message[:100]}...") # First 100 chars
|
177 |
|
178 |
+
# Update history using list format [user_msg, bot_msg, user_msg, bot_msg, ...]
|
179 |
+
updated_history = chat_history + [message, bot_message]
|
180 |
return "", updated_history
|
181 |
|
182 |
except Exception as e:
|
183 |
logging.error("Error in respond function", exc_info=True)
|
184 |
error_msg = f"Story magic temporarily interrupted. Please try again. (Error: {str(e)})"
|
185 |
+
return "", chat_history + [message, error_msg]
|
186 |
|
187 |
def save_story(chat_history):
|
188 |
"""Convert chat history to markdown and return as downloadable file"""
|
|
|
190 |
return None
|
191 |
|
192 |
story_text = "# My Interactive Adventure\n\n"
|
193 |
+
# Process in pairs (user message, bot response)
|
194 |
+
for i in range(0, len(chat_history), 2):
|
195 |
+
if i+1 < len(chat_history):
|
196 |
+
story_text += f"**Player:** {chat_history[i]}\n\n"
|
197 |
+
story_text += f"**Story:** {chat_history[i+1]}\n\n---\n\n"
|
198 |
|
199 |
# Return as a file for download
|
200 |
return story_text
|
|
|
221 |
> **Tip:** The more detail you provide, the deeper the story becomes.
|
222 |
""")
|
223 |
|
|
|
|
|
|
|
|
|
|
|
|
|
224 |
wizard_avatar = get_storyteller_avatar_url()
|
225 |
|
226 |
with gr.Row():
|
227 |
with gr.Column(scale=3):
|
228 |
+
# Chat window + user input - USING LIST FORMAT
|
229 |
chatbot = gr.Chatbot(
|
230 |
height=500,
|
231 |
bubble_full_width=True,
|
|
|
234 |
container=True,
|
235 |
scale=1,
|
236 |
min_width=800,
|
237 |
+
value=[], # Empty list for messages
|
238 |
render=True
|
239 |
)
|
240 |
msg = gr.Textbox(
|
|
|
299 |
|
300 |
except Exception as e:
|
301 |
error_msg = f"Story magic temporarily interrupted. Please try again. (Error: {str(e)})"
|
302 |
+
return "", history + [starter_text, error_msg]
|
303 |
|
304 |
# Simplified button connections
|
305 |
for starter_button in starter_buttons:
|