Reality123b commited on
Commit
3674c04
·
verified ·
1 Parent(s): d17badf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -17
app.py CHANGED
@@ -176,8 +176,12 @@ class XylariaChat:
176
  return f"Error generating response: {str(e)}"
177
 
178
  def create_interface(self):
179
- def streaming_response(message, chat_history, image):
180
- response_stream = self.get_response(message, image)
 
 
 
 
181
 
182
  # Handle errors in get_response
183
  if isinstance(response_stream, str):
@@ -185,7 +189,7 @@ class XylariaChat:
185
  updated_history = chat_history + [[message, response_stream]]
186
  yield "", updated_history, None
187
  return
188
-
189
  # Prepare for streaming response
190
  full_response = ""
191
  updated_history = chat_history + [[message, ""]]
@@ -199,14 +203,14 @@ class XylariaChat:
199
 
200
  # Update the last message in chat history with partial response
201
  updated_history[-1][1] = full_response
202
- yield "", updated_history, None
203
  except Exception as e:
204
  print(f"Streaming error: {e}")
205
  # Display error in the chat interface
206
  updated_history[-1][1] = f"Error during response: {e}"
207
  yield "", updated_history, None
208
  return
209
-
210
  # Update conversation history
211
  self.conversation_history.append(
212
  {"role": "user", "content": message}
@@ -214,7 +218,7 @@ class XylariaChat:
214
  self.conversation_history.append(
215
  {"role": "assistant", "content": full_response}
216
  )
217
-
218
  # Limit conversation history
219
  if len(self.conversation_history) > 10:
220
  self.conversation_history = self.conversation_history[-10:]
@@ -222,15 +226,15 @@ class XylariaChat:
222
  # Custom CSS for Inter font
223
  custom_css = """
224
  @import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap');
225
-
226
  body, .gradio-container {
227
  font-family: 'Inter', sans-serif !important;
228
  }
229
-
230
  .chatbot-container .message {
231
  font-family: 'Inter', sans-serif !important;
232
  }
233
-
234
  .gradio-container input,
235
  .gradio-container textarea,
236
  .gradio-container button {
@@ -245,9 +249,9 @@ class XylariaChat:
245
  label="Xylaria 1.4 Senoa (Qwen Model)",
246
  height=500,
247
  show_copy_button=True,
248
-
249
  )
250
-
251
  # Input row with improved layout and image upload
252
  with gr.Row():
253
  with gr.Column(scale=4):
@@ -256,7 +260,7 @@ class XylariaChat:
256
  placeholder="Type your message...",
257
  container=False
258
  )
259
-
260
  # Image upload as a separate button
261
  with gr.Row():
262
  img = gr.Image(
@@ -268,19 +272,19 @@ class XylariaChat:
268
  upload_btn = gr.Button("Upload Image")
269
 
270
  btn = gr.Button("Send", scale=1)
271
-
272
  # Clear history and memory buttons
273
  with gr.Row():
274
  clear = gr.Button("Clear Conversation")
275
  clear_memory = gr.Button("Clear Memory")
276
-
277
  # Image upload toggle
278
  upload_btn.click(
279
  fn=lambda: gr.update(visible=True),
280
  inputs=None,
281
  outputs=[img]
282
  )
283
-
284
  # Submit functionality with streaming and image support
285
  btn.click(
286
  fn=streaming_response,
@@ -292,7 +296,7 @@ class XylariaChat:
292
  inputs=[txt, chatbot, img],
293
  outputs=[txt, chatbot, img]
294
  )
295
-
296
  # Clear conversation history
297
  clear.click(
298
  fn=lambda: None,
@@ -300,7 +304,7 @@ class XylariaChat:
300
  outputs=[chatbot],
301
  queue=False
302
  )
303
-
304
  # Clear persistent memory and reset conversation
305
  clear_memory.click(
306
  fn=self.reset_conversation,
 
176
  return f"Error generating response: {str(e)}"
177
 
178
  def create_interface(self):
179
+ def streaming_response(message, chat_history, image_filepath):
180
+ # Check if an image was actually uploaded
181
+ if image_filepath:
182
+ response_stream = self.get_response(message, image_filepath)
183
+ else:
184
+ response_stream = self.get_response(message)
185
 
186
  # Handle errors in get_response
187
  if isinstance(response_stream, str):
 
189
  updated_history = chat_history + [[message, response_stream]]
190
  yield "", updated_history, None
191
  return
192
+
193
  # Prepare for streaming response
194
  full_response = ""
195
  updated_history = chat_history + [[message, ""]]
 
203
 
204
  # Update the last message in chat history with partial response
205
  updated_history[-1][1] = full_response
206
+ yield "", updated_history, None
207
  except Exception as e:
208
  print(f"Streaming error: {e}")
209
  # Display error in the chat interface
210
  updated_history[-1][1] = f"Error during response: {e}"
211
  yield "", updated_history, None
212
  return
213
+
214
  # Update conversation history
215
  self.conversation_history.append(
216
  {"role": "user", "content": message}
 
218
  self.conversation_history.append(
219
  {"role": "assistant", "content": full_response}
220
  )
221
+
222
  # Limit conversation history
223
  if len(self.conversation_history) > 10:
224
  self.conversation_history = self.conversation_history[-10:]
 
226
  # Custom CSS for Inter font
227
  custom_css = """
228
  @import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap');
229
+
230
  body, .gradio-container {
231
  font-family: 'Inter', sans-serif !important;
232
  }
233
+
234
  .chatbot-container .message {
235
  font-family: 'Inter', sans-serif !important;
236
  }
237
+
238
  .gradio-container input,
239
  .gradio-container textarea,
240
  .gradio-container button {
 
249
  label="Xylaria 1.4 Senoa (Qwen Model)",
250
  height=500,
251
  show_copy_button=True,
252
+
253
  )
254
+
255
  # Input row with improved layout and image upload
256
  with gr.Row():
257
  with gr.Column(scale=4):
 
260
  placeholder="Type your message...",
261
  container=False
262
  )
263
+
264
  # Image upload as a separate button
265
  with gr.Row():
266
  img = gr.Image(
 
272
  upload_btn = gr.Button("Upload Image")
273
 
274
  btn = gr.Button("Send", scale=1)
275
+
276
  # Clear history and memory buttons
277
  with gr.Row():
278
  clear = gr.Button("Clear Conversation")
279
  clear_memory = gr.Button("Clear Memory")
280
+
281
  # Image upload toggle
282
  upload_btn.click(
283
  fn=lambda: gr.update(visible=True),
284
  inputs=None,
285
  outputs=[img]
286
  )
287
+
288
  # Submit functionality with streaming and image support
289
  btn.click(
290
  fn=streaming_response,
 
296
  inputs=[txt, chatbot, img],
297
  outputs=[txt, chatbot, img]
298
  )
299
+
300
  # Clear conversation history
301
  clear.click(
302
  fn=lambda: None,
 
304
  outputs=[chatbot],
305
  queue=False
306
  )
307
+
308
  # Clear persistent memory and reset conversation
309
  clear_memory.click(
310
  fn=self.reset_conversation,