pratyushmaini commited on
Commit
856a204
·
1 Parent(s): 72c3fa0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -33
app.py CHANGED
@@ -9,32 +9,43 @@ model_list = {
9
  }
10
 
11
  def respond(message, history, system_message, max_tokens, temperature, top_p, selected_model):
12
- # Create an InferenceClient for the selected model
13
- client = InferenceClient(model_list.get(selected_model, "HuggingFaceH4/zephyr-7b-beta"))
14
-
15
- # Build conversation messages for the client
16
- messages = [{"role": "system", "content": system_message}]
17
- for user_msg, assistant_msg in history:
18
- if user_msg: # Only add non-empty messages
19
- messages.append({"role": "user", "content": user_msg})
20
- if assistant_msg: # Only add non-empty messages
21
- messages.append({"role": "assistant", "content": assistant_msg})
22
- messages.append({"role": "user", "content": message})
23
-
24
- response = ""
25
-
26
- # Stream the response from the client
27
- for token_message in client.chat_completion(
28
- messages,
29
- max_tokens=max_tokens,
30
- stream=True,
31
- temperature=temperature,
32
- top_p=top_p,
33
- ):
34
- token = token_message.choices[0].delta.content
35
- if token is not None: # Handle potential None values
36
- response += token
37
- yield response
 
 
 
 
 
 
 
 
 
 
 
38
 
39
  # Custom CSS for branding with blue, teal and gold accents
40
  css = """
@@ -174,7 +185,8 @@ with gr.Blocks(css=css) as demo:
174
  chatbot = gr.Chatbot(
175
  label="Conversation",
176
  show_label=True,
177
- avatar_images=("👤", "🔒"),
 
178
  height=500
179
  )
180
  with gr.Row():
@@ -206,7 +218,9 @@ with gr.Blocks(css=css) as demo:
206
  return "", history + [[user_message, None]]
207
 
208
  def bot(history, system_message, max_tokens, temperature, top_p, selected_model):
209
- # Get the last user message from history
 
 
210
  user_message = history[-1][0]
211
  # Call respond function with the message
212
  response_generator = respond(
@@ -223,7 +237,7 @@ with gr.Blocks(css=css) as demo:
223
  history[-1][1] = response
224
  yield history
225
 
226
- # Wire up the event chain
227
  user_input.submit(
228
  user,
229
  [user_input, chatbot],
@@ -232,7 +246,8 @@ with gr.Blocks(css=css) as demo:
232
  ).then(
233
  bot,
234
  [chatbot, system_message, max_tokens_slider, temperature_slider, top_p_slider, model_dropdown],
235
- [chatbot]
 
236
  )
237
 
238
  send_button.click(
@@ -243,11 +258,15 @@ with gr.Blocks(css=css) as demo:
243
  ).then(
244
  bot,
245
  [chatbot, system_message, max_tokens_slider, temperature_slider, top_p_slider, model_dropdown],
246
- [chatbot]
 
247
  )
248
 
249
- # Clear the chat history
250
- clear_button.click(lambda: None, None, chatbot, queue=False)
 
 
 
251
 
252
  if __name__ == "__main__":
253
  demo.launch()
 
9
  }
10
 
11
  def respond(message, history, system_message, max_tokens, temperature, top_p, selected_model):
12
+ try:
13
+ # Create an InferenceClient for the selected model
14
+ client = InferenceClient(model_list.get(selected_model, "HuggingFaceH4/zephyr-7b-beta"))
15
+
16
+ # Build conversation messages for the client
17
+ messages = [{"role": "system", "content": system_message}]
18
+ for user_msg, assistant_msg in history:
19
+ if user_msg: # Only add non-empty messages
20
+ messages.append({"role": "user", "content": user_msg})
21
+ if assistant_msg: # Only add non-empty messages
22
+ messages.append({"role": "assistant", "content": assistant_msg})
23
+ messages.append({"role": "user", "content": message})
24
+
25
+ response = ""
26
+
27
+ # Stream the response from the client
28
+ for token_message in client.chat_completion(
29
+ messages,
30
+ max_tokens=max_tokens,
31
+ stream=True,
32
+ temperature=temperature,
33
+ top_p=top_p,
34
+ ):
35
+ # Safe extraction of token with error handling
36
+ try:
37
+ token = token_message.choices[0].delta.content
38
+ if token is not None: # Handle potential None values
39
+ response += token
40
+ yield response
41
+ except (AttributeError, IndexError) as e:
42
+ # Handle cases where token structure might be different
43
+ print(f"Error extracting token: {e}")
44
+ continue
45
+ except Exception as e:
46
+ # Return error message if the model call fails
47
+ print(f"Error calling model API: {e}")
48
+ yield f"Sorry, there was an error: {str(e)}"
49
 
50
  # Custom CSS for branding with blue, teal and gold accents
51
  css = """
 
185
  chatbot = gr.Chatbot(
186
  label="Conversation",
187
  show_label=True,
188
+ # Removed avatar_images which may not be supported in some Gradio versions
189
+ elem_id="chatbot",
190
  height=500
191
  )
192
  with gr.Row():
 
218
  return "", history + [[user_message, None]]
219
 
220
  def bot(history, system_message, max_tokens, temperature, top_p, selected_model):
221
+ # Get the last user message from history (with error checking)
222
+ if not history or len(history) == 0:
223
+ return history
224
  user_message = history[-1][0]
225
  # Call respond function with the message
226
  response_generator = respond(
 
237
  history[-1][1] = response
238
  yield history
239
 
240
+ # Wire up the event chain - use queue=True for the bot responses
241
  user_input.submit(
242
  user,
243
  [user_input, chatbot],
 
246
  ).then(
247
  bot,
248
  [chatbot, system_message, max_tokens_slider, temperature_slider, top_p_slider, model_dropdown],
249
+ [chatbot],
250
+ queue=True
251
  )
252
 
253
  send_button.click(
 
258
  ).then(
259
  bot,
260
  [chatbot, system_message, max_tokens_slider, temperature_slider, top_p_slider, model_dropdown],
261
+ [chatbot],
262
+ queue=True
263
  )
264
 
265
+ # Clear the chat history - use a proper function instead of lambda
266
+ def clear_history():
267
+ return []
268
+
269
+ clear_button.click(clear_history, None, chatbot, queue=False)
270
 
271
  if __name__ == "__main__":
272
  demo.launch()