PeterPinetree commited on
Commit
cd10677
·
verified ·
1 Parent(s): a33faa6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -31
app.py CHANGED
@@ -141,17 +141,11 @@ def respond(
141
  if not message.strip():
142
  return chat_history
143
 
144
- # Use the enhanced system prompt instead of the simple one
145
- system_message = get_enhanced_system_prompt(genre)
146
-
147
  # Format messages for the API
148
- formatted_messages = []
149
-
150
- # Add system message first
151
- formatted_messages.append({
152
  "role": "system",
153
- "content": system_message
154
- })
155
 
156
  # Add chat history
157
  if chat_history and use_full_memory:
@@ -168,32 +162,20 @@ def respond(
168
  })
169
 
170
  try:
171
- bot_message = ""
172
- current_history = list(chat_history)
173
-
174
- for response_chunk in client.chat_completion(
175
  formatted_messages,
176
  max_tokens=MAX_TOKENS,
177
- stream=True,
178
  temperature=TEMPERATURE,
179
  top_p=TOP_P
180
- ):
181
- if hasattr(response_chunk.choices[0].delta, 'content'):
182
- delta = response_chunk.choices[0].delta.content
183
- if delta:
184
- bot_message += delta
185
- if len(bot_message.strip()) >= MIN_RESPONSE_LENGTH:
186
- new_history = current_history + [(message, bot_message)]
187
- yield new_history
188
-
189
- if bot_message: # Ensure final message is yielded
190
- final_history = current_history + [(message, bot_message)]
191
- yield final_history
192
 
193
  except Exception as e:
194
  error_message = f"Story magic temporarily interrupted. Please try again. (Error: {str(e)})"
195
- error_history = current_history + [(message, error_message)]
196
- yield error_history
197
 
198
  def save_story(chat_history):
199
  """Convert chat history to markdown for download"""
@@ -224,7 +206,9 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
224
  type="messages",
225
  container=True,
226
  scale=1,
227
- min_width=800 # Ensure minimum width
 
 
228
  )
229
  msg = gr.Textbox(
230
  placeholder="Describe what you want to do next in the story...",
@@ -284,11 +268,11 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
284
  # 4) Connect each starter button:
285
  for starter_button in starter_buttons:
286
  starter_button.click(
287
- fn=lambda x: [(x, "")], # Changed to return properly formatted history
288
  inputs=[starter_button],
289
  outputs=[chatbot],
290
  queue=False
291
- ).then( # Chain the response after showing user message
292
  fn=respond,
293
  inputs=[starter_button, chatbot, genre, full_memory],
294
  outputs=[chatbot],
@@ -334,3 +318,4 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
334
  # Run the app
335
  if __name__ == "__main__":
336
  demo.launch(server_name="0.0.0.0", server_port=7860)
 
 
141
  if not message.strip():
142
  return chat_history
143
 
 
 
 
144
  # Format messages for the API
145
+ formatted_messages = [{
 
 
 
146
  "role": "system",
147
+ "content": get_enhanced_system_prompt(genre)
148
+ }]
149
 
150
  # Add chat history
151
  if chat_history and use_full_memory:
 
162
  })
163
 
164
  try:
165
+ response = client.chat_completion(
 
 
 
166
  formatted_messages,
167
  max_tokens=MAX_TOKENS,
 
168
  temperature=TEMPERATURE,
169
  top_p=TOP_P
170
+ )
171
+ bot_message = response.choices[0].message.content
172
+ new_history = list(chat_history) + [(message, bot_message)]
173
+ return new_history
 
 
 
 
 
 
 
 
174
 
175
  except Exception as e:
176
  error_message = f"Story magic temporarily interrupted. Please try again. (Error: {str(e)})"
177
+ error_history = list(chat_history) + [(message, error_message)]
178
+ return error_history
179
 
180
  def save_story(chat_history):
181
  """Convert chat history to markdown for download"""
 
206
  type="messages",
207
  container=True,
208
  scale=1,
209
+ min_width=800, # Ensure minimum width
210
+ value=[], # Initialize with empty list
211
+ render=True
212
  )
213
  msg = gr.Textbox(
214
  placeholder="Describe what you want to do next in the story...",
 
268
  # 4) Connect each starter button:
269
  for starter_button in starter_buttons:
270
  starter_button.click(
271
+ fn=lambda x: {"role": "user", "content": str(x)}, # Format as message dict
272
  inputs=[starter_button],
273
  outputs=[chatbot],
274
  queue=False
275
+ ).then(
276
  fn=respond,
277
  inputs=[starter_button, chatbot, genre, full_memory],
278
  outputs=[chatbot],
 
318
  # Run the app
319
  if __name__ == "__main__":
320
  demo.launch(server_name="0.0.0.0", server_port=7860)
321
+