Bhaskar2611 commited on
Commit
edb8de4
·
verified ·
1 Parent(s): a66fbb9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -8
app.py CHANGED
@@ -67,6 +67,7 @@ from huggingface_hub import InferenceClient
67
 
68
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
69
 
 
70
  def respond(
71
  message,
72
  history: list[tuple[str, str]],
@@ -76,18 +77,22 @@ def respond(
76
  top_p,
77
  ):
78
  try:
 
79
  messages = [{"role": "system", "content": system_message}]
80
 
81
- for val in history:
82
- if val[0]:
83
- messages.append({"role": "user", "content": val[0]})
84
- if val[1]:
85
- messages.append({"role": "assistant", "content": val[1]})
 
86
 
 
87
  messages.append({"role": "user", "content": message})
88
 
89
  response = ""
90
 
 
91
  for message in client.chat_completion(
92
  messages,
93
  max_tokens=max_tokens,
@@ -98,10 +103,12 @@ def respond(
98
  token = message.choices[0].delta.content
99
  response += token
100
  yield response
101
- except Exception as e:
102
- # Catching any errors and displaying a fallback message instead
103
- yield "Sorry, something went wrong. Please try again."
104
 
 
 
 
 
 
105
  demo = gr.ChatInterface(
106
  respond,
107
  additional_inputs=[
@@ -116,7 +123,11 @@ demo = gr.ChatInterface(
116
  label="Top-p (nucleus sampling)",
117
  ),
118
  ],
 
 
 
119
  )
120
 
121
  if __name__ == "__main__":
122
  demo.launch()
 
 
67
 
68
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
69
 
70
+ # Function to respond to user input while maintaining chat history
71
  def respond(
72
  message,
73
  history: list[tuple[str, str]],
 
77
  top_p,
78
  ):
79
  try:
80
+ # Append system message at the start
81
  messages = [{"role": "system", "content": system_message}]
82
 
83
+ # Append the chat history
84
+ for user_msg, bot_reply in history:
85
+ if user_msg:
86
+ messages.append({"role": "user", "content": user_msg})
87
+ if bot_reply:
88
+ messages.append({"role": "assistant", "content": bot_reply})
89
 
90
+ # Add the latest user message
91
  messages.append({"role": "user", "content": message})
92
 
93
  response = ""
94
 
95
+ # Stream the response token by token to avoid loading delays
96
  for message in client.chat_completion(
97
  messages,
98
  max_tokens=max_tokens,
 
103
  token = message.choices[0].delta.content
104
  response += token
105
  yield response
 
 
 
106
 
107
+ except Exception:
108
+ # Suppress any error and maintain conversation without showing error icon or message
109
+ yield "Error handled silently." # You can replace this with "" to hide any message
110
+
111
+ # Gradio interface with preserved history and customizable chatbot behavior
112
  demo = gr.ChatInterface(
113
  respond,
114
  additional_inputs=[
 
123
  label="Top-p (nucleus sampling)",
124
  ),
125
  ],
126
+ # Enable to retain history and suppress errors
127
+ retain_history=True, # This will ensure the chat history is not lost between interactions
128
+ retry_on_error=False # Prevent showing the error message or retry button
129
  )
130
 
131
  if __name__ == "__main__":
132
  demo.launch()
133
+