Shreyas094 commited on
Commit
d289c32
·
verified ·
1 Parent(s): cc9ec4e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -29
app.py CHANGED
@@ -87,29 +87,19 @@ def generate_chunked_response(prompt, model, max_tokens=1000, max_chunks=5, temp
87
  full_response = ""
88
  messages = [{"role": "user", "content": prompt}]
89
 
90
- for _ in range(max_chunks):
91
- try:
92
- chunk_response = ""
93
- for message in client.chat_completion(
94
- messages=messages,
95
- max_tokens=max_tokens,
96
- temperature=temperature,
97
- stream=True,
98
- ):
99
- chunk = message.choices[0].delta.content
100
- if chunk:
101
- chunk_response += chunk
102
- full_response += chunk
103
-
104
- if not chunk_response or chunk_response.endswith((".", "!", "?", "</s>", "[/INST]")):
105
- break
106
-
107
- messages.append({"role": "assistant", "content": chunk_response})
108
- messages.append({"role": "user", "content": "Continue"})
109
 
110
- except Exception as e:
111
- print(f"Error in generating response: {str(e)}")
112
- break
113
 
114
  # Clean up the response
115
  clean_response = re.sub(r'<s>\[INST\].*?\[/INST\]\s*', '', full_response, flags=re.DOTALL)
@@ -177,6 +167,9 @@ After writing the document, please provide a list of sources used in your respon
177
  return main_content, sources
178
 
179
  def chatbot_interface(message, history, use_web_search, model, temperature):
 
 
 
180
  if use_web_search:
181
  main_content, sources = get_response_with_search(message, model, temperature)
182
  formatted_response = f"{main_content}\n\nSources:\n{sources}"
@@ -184,11 +177,34 @@ def chatbot_interface(message, history, use_web_search, model, temperature):
184
  response = get_response_from_pdf(message, model, temperature)
185
  formatted_response = response
186
 
187
- history.append((message, formatted_response))
 
 
 
 
 
 
 
188
  return history
189
 
 
 
 
 
 
190
  # Gradio interface
191
  with gr.Blocks() as demo:
 
 
 
 
 
 
 
 
 
 
 
192
  gr.Markdown("# AI-powered Web Search and PDF Chat Assistant")
193
 
194
  with gr.Row():
@@ -219,12 +235,12 @@ with gr.Blocks() as demo:
219
  inputs=msg,
220
  )
221
 
222
- submit.click(chatbot_interface,
223
- inputs=[msg, chatbot, use_web_search, model_dropdown, temperature_slider],
224
- outputs=[chatbot])
225
- msg.submit(chatbot_interface,
226
- inputs=[msg, chatbot, use_web_search, model_dropdown, temperature_slider],
227
- outputs=[chatbot])
228
 
229
  gr.Markdown(
230
  """
 
87
  full_response = ""
88
  messages = [{"role": "user", "content": prompt}]
89
 
90
+ try:
91
+ for message in client.chat_completion(
92
+ messages=messages,
93
+ max_tokens=max_tokens,
94
+ temperature=temperature,
95
+ stream=True,
96
+ ):
97
+ chunk = message.choices[0].delta.content
98
+ if chunk:
99
+ full_response += chunk
 
 
 
 
 
 
 
 
 
100
 
101
+ except Exception as e:
102
+ print(f"Error in generating response: {str(e)}")
 
103
 
104
  # Clean up the response
105
  clean_response = re.sub(r'<s>\[INST\].*?\[/INST\]\s*', '', full_response, flags=re.DOTALL)
 
167
  return main_content, sources
168
 
169
  def chatbot_interface(message, history, use_web_search, model, temperature):
170
+ if not message.strip(): # Check if the message is empty or just whitespace
171
+ return history
172
+
173
  if use_web_search:
174
  main_content, sources = get_response_with_search(message, model, temperature)
175
  formatted_response = f"{main_content}\n\nSources:\n{sources}"
 
177
  response = get_response_from_pdf(message, model, temperature)
178
  formatted_response = response
179
 
180
+ # Check if the last message in history is the same as the current message
181
+ if history and history[-1][0] == message:
182
+ # Replace the last response instead of adding a new one
183
+ history[-1] = (message, formatted_response)
184
+ else:
185
+ # Add the new message-response pair
186
+ history.append((message, formatted_response))
187
+
188
  return history
189
 
190
+
191
+ def clear_and_update_chat(message, history, use_web_search, model, temperature):
192
+ updated_history = chatbot_interface(message, history, use_web_search, model, temperature)
193
+ return "", updated_history # Return empty string to clear the input
194
+
195
  # Gradio interface
196
  with gr.Blocks() as demo:
197
+
198
+ is_generating = gr.State(False)
199
+
200
+ def protected_clear_and_update_chat(message, history, use_web_search, model, temperature, is_generating):
201
+ if is_generating:
202
+ return message, history, is_generating
203
+ is_generating = True
204
+ updated_message, updated_history = clear_and_update_chat(message, history, use_web_search, model, temperature)
205
+ is_generating = False
206
+ return updated_message, updated_history, is_generating
207
+
208
  gr.Markdown("# AI-powered Web Search and PDF Chat Assistant")
209
 
210
  with gr.Row():
 
235
  inputs=msg,
236
  )
237
 
238
+ submit.click(protected_clear_and_update_chat,
239
+ inputs=[msg, chatbot, use_web_search, model_dropdown, temperature_slider, is_generating],
240
+ outputs=[msg, chatbot, is_generating])
241
+ msg.submit(protected_clear_and_update_chat,
242
+ inputs=[msg, chatbot, use_web_search, model_dropdown, temperature_slider, is_generating],
243
+ outputs=[msg, chatbot, is_generating])
244
 
245
  gr.Markdown(
246
  """