Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -136,30 +136,16 @@ def respond(
|
|
| 136 |
chat_history: List[Tuple[str, str]],
|
| 137 |
genre: Optional[str] = None,
|
| 138 |
use_full_memory: bool = True
|
| 139 |
-
) -> List[
|
| 140 |
"""Generate a response based on the current message and conversation history."""
|
| 141 |
if not message.strip():
|
| 142 |
-
return
|
| 143 |
-
|
| 144 |
-
# Format messages for API
|
| 145 |
-
formatted_messages = [{"role": "system", "content": get_enhanced_system_prompt(genre)}]
|
| 146 |
-
|
| 147 |
-
# Format chat history
|
| 148 |
-
if chat_history and use_full_memory:
|
| 149 |
-
for user_msg, bot_msg in chat_history[-MEMORY_WINDOW:]:
|
| 150 |
-
formatted_messages.extend([
|
| 151 |
-
{"role": "user", "content": str(user_msg)},
|
| 152 |
-
{"role": "assistant", "content": str(bot_msg)}
|
| 153 |
-
])
|
| 154 |
-
|
| 155 |
-
# Add current message
|
| 156 |
-
formatted_messages.append({"role": "user", "content": str(message)})
|
| 157 |
|
| 158 |
try:
|
| 159 |
-
#
|
| 160 |
api_messages = [{"role": "system", "content": get_enhanced_system_prompt(genre)}]
|
| 161 |
|
| 162 |
-
# Add chat history
|
| 163 |
if chat_history and use_full_memory:
|
| 164 |
for user_msg, bot_msg in chat_history[-MEMORY_WINDOW:]:
|
| 165 |
api_messages.extend([
|
|
@@ -172,17 +158,21 @@ def respond(
|
|
| 172 |
|
| 173 |
# Make API call with properly formatted messages
|
| 174 |
response = client.chat_completion(
|
| 175 |
-
messages=api_messages,
|
| 176 |
max_tokens=MAX_TOKENS,
|
| 177 |
temperature=TEMPERATURE,
|
| 178 |
top_p=TOP_P
|
| 179 |
)
|
| 180 |
|
|
|
|
| 181 |
bot_message = response.choices[0].message.content
|
| 182 |
-
|
|
|
|
|
|
|
| 183 |
|
| 184 |
except Exception as e:
|
| 185 |
-
|
|
|
|
| 186 |
|
| 187 |
def save_story(chat_history):
|
| 188 |
"""Convert chat history to markdown for download"""
|
|
@@ -275,17 +265,14 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 275 |
# 4) Connect each starter button:
|
| 276 |
for starter_button in starter_buttons:
|
| 277 |
starter_button.click(
|
| 278 |
-
|
|
|
|
| 279 |
inputs=[starter_button],
|
| 280 |
outputs=[chatbot],
|
| 281 |
queue=False
|
| 282 |
-
).success(
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
chat_history=h if h else [],
|
| 286 |
-
genre=g,
|
| 287 |
-
use_full_memory=m
|
| 288 |
-
),
|
| 289 |
inputs=[
|
| 290 |
starter_button, # The starter text
|
| 291 |
chatbot, # Current chat history
|
|
|
|
| 136 |
chat_history: List[Tuple[str, str]],
|
| 137 |
genre: Optional[str] = None,
|
| 138 |
use_full_memory: bool = True
|
| 139 |
+
) -> List[Tuple[str, str]]: # Changed return type to match Gradio's chatbot format
|
| 140 |
"""Generate a response based on the current message and conversation history."""
|
| 141 |
if not message.strip():
|
| 142 |
+
return chat_history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
|
| 144 |
try:
|
| 145 |
+
# Format messages for API - always start with system prompt
|
| 146 |
api_messages = [{"role": "system", "content": get_enhanced_system_prompt(genre)}]
|
| 147 |
|
| 148 |
+
# Add chat history in correct format
|
| 149 |
if chat_history and use_full_memory:
|
| 150 |
for user_msg, bot_msg in chat_history[-MEMORY_WINDOW:]:
|
| 151 |
api_messages.extend([
|
|
|
|
| 158 |
|
| 159 |
# Make API call with properly formatted messages
|
| 160 |
response = client.chat_completion(
|
| 161 |
+
messages=api_messages, # Use formatted messages
|
| 162 |
max_tokens=MAX_TOKENS,
|
| 163 |
temperature=TEMPERATURE,
|
| 164 |
top_p=TOP_P
|
| 165 |
)
|
| 166 |
|
| 167 |
+
# Extract bot message and append to history
|
| 168 |
bot_message = response.choices[0].message.content
|
| 169 |
+
new_history = list(chat_history)
|
| 170 |
+
new_history.append((str(message), str(bot_message)))
|
| 171 |
+
return new_history
|
| 172 |
|
| 173 |
except Exception as e:
|
| 174 |
+
# Return existing history plus error message
|
| 175 |
+
return chat_history + [(str(message), f"Story magic temporarily interrupted. Please try again. (Error: {str(e)})")]
|
| 176 |
|
| 177 |
def save_story(chat_history):
|
| 178 |
"""Convert chat history to markdown for download"""
|
|
|
|
| 265 |
# 4) Connect each starter button:
|
| 266 |
for starter_button in starter_buttons:
|
| 267 |
starter_button.click(
|
| 268 |
+
# Initial click creates empty chat history with starter message
|
| 269 |
+
fn=lambda x: [(str(x), "")],
|
| 270 |
inputs=[starter_button],
|
| 271 |
outputs=[chatbot],
|
| 272 |
queue=False
|
| 273 |
+
).success(
|
| 274 |
+
# Then process the message with respond function
|
| 275 |
+
fn=respond,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 276 |
inputs=[
|
| 277 |
starter_button, # The starter text
|
| 278 |
chatbot, # Current chat history
|