Shreyas094 commited on
Commit
a04fb1d
·
verified ·
1 Parent(s): d8d3738

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -28
app.py CHANGED
@@ -79,15 +79,13 @@ def update_vectors(files, parser):
79
  return f"Vector store updated successfully. Processed {total_chunks} chunks from {len(files)} files using {parser}."
80
 
81
  def generate_chunked_response(prompt, model, max_tokens=1000, num_calls=3, temperature=0.2):
82
- client = InferenceClient(
83
- model,
84
- token=huggingface_token,
85
- )
86
-
87
  full_responses = []
88
  messages = [{"role": "user", "content": prompt}]
89
 
90
  for _ in range(num_calls):
 
 
91
  try:
92
  response = ""
93
  for message in client.chat_completion(
@@ -96,6 +94,8 @@ def generate_chunked_response(prompt, model, max_tokens=1000, num_calls=3, tempe
96
  temperature=temperature,
97
  stream=True,
98
  ):
 
 
99
  if message.choices and message.choices[0].delta and message.choices[0].delta.content:
100
  chunk = message.choices[0].delta.content
101
  response += chunk
@@ -103,8 +103,10 @@ def generate_chunked_response(prompt, model, max_tokens=1000, num_calls=3, tempe
103
  except Exception as e:
104
  print(f"Error in generating response: {str(e)}")
105
 
106
- # Combine and clean up the responses
107
  combined_response = " ".join(full_responses)
 
 
108
  clean_response = re.sub(r'<s>\[INST\].*?\[/INST\]\s*', '', combined_response, flags=re.DOTALL)
109
  clean_response = clean_response.replace("Using the following context:", "").strip()
110
  clean_response = clean_response.replace("Using the following context from the PDF documents:", "").strip()
@@ -244,28 +246,29 @@ with gr.Blocks() as demo:
244
  clear_btn = gr.Button("Clear")
245
 
246
  def protected_generate_response(message, history, use_web_search, model, temperature, num_calls, is_generating, stop_clicked):
247
- if is_generating:
248
- return message, history, is_generating, stop_clicked
249
- is_generating = True
250
- stop_clicked = False
251
-
252
- try:
253
- if use_web_search:
254
- main_content, sources = get_response_with_search(message, model, num_calls=num_calls, temperature=temperature)
255
- formatted_response = f"{main_content}\n\nSources:\n{sources}"
256
- else:
257
- response = get_response_from_pdf(message, model, num_calls=num_calls, temperature=temperature)
258
- formatted_response = response
259
-
260
- if not stop_clicked:
261
- history.append((message, formatted_response))
262
- except Exception as e:
263
- print(f"Error generating response: {str(e)}")
264
- history.append((message, "I'm sorry, but I encountered an error while generating the response. Please try again."))
265
-
266
- is_generating = False
267
- return "", history, is_generating, stop_clicked
268
-
 
269
  submit_btn.click(
270
  protected_generate_response,
271
  inputs=[msg, chatbot, use_web_search, model_dropdown, temperature_slider, num_calls_slider, is_generating, stop_clicked],
 
79
  return f"Vector store updated successfully. Processed {total_chunks} chunks from {len(files)} files using {parser}."
80
 
81
  def generate_chunked_response(prompt, model, max_tokens=1000, num_calls=3, temperature=0.2):
82
+ client = InferenceClient(model, token=huggingface_token)
 
 
 
 
83
  full_responses = []
84
  messages = [{"role": "user", "content": prompt}]
85
 
86
  for _ in range(num_calls):
87
+ if stop_clicked: # Check if stop was clicked
88
+ break
89
  try:
90
  response = ""
91
  for message in client.chat_completion(
 
94
  temperature=temperature,
95
  stream=True,
96
  ):
97
+ if stop_clicked: # Check if stop was clicked
98
+ break
99
  if message.choices and message.choices[0].delta and message.choices[0].delta.content:
100
  chunk = message.choices[0].delta.content
101
  response += chunk
 
103
  except Exception as e:
104
  print(f"Error in generating response: {str(e)}")
105
 
106
+ # Combine all responses into a single string
107
  combined_response = " ".join(full_responses)
108
+
109
+ # Clean the combined response
110
  clean_response = re.sub(r'<s>\[INST\].*?\[/INST\]\s*', '', combined_response, flags=re.DOTALL)
111
  clean_response = clean_response.replace("Using the following context:", "").strip()
112
  clean_response = clean_response.replace("Using the following context from the PDF documents:", "").strip()
 
246
  clear_btn = gr.Button("Clear")
247
 
248
  def protected_generate_response(message, history, use_web_search, model, temperature, num_calls, is_generating, stop_clicked):
249
+ if is_generating:
250
+ return message, history, is_generating, stop_clicked
251
+ is_generating = True
252
+ stop_clicked = False
253
+
254
+ try:
255
+ if use_web_search:
256
+ main_content, sources = get_response_with_search(message, model, num_calls=num_calls, temperature=temperature)
257
+ formatted_response = f"{main_content}\n\nSources:\n{sources}"
258
+ else:
259
+ response = get_response_from_pdf(message, model, num_calls=num_calls, temperature=temperature)
260
+ formatted_response = response
261
+
262
+ if not stop_clicked:
263
+ # Only append the final, combined response to the history
264
+ history.append((message, formatted_response))
265
+ except Exception as e:
266
+ print(f"Error generating response: {str(e)}")
267
+ history.append((message, "I'm sorry, but I encountered an error while generating the response. Please try again."))
268
+
269
+ is_generating = False
270
+ return "", history, is_generating, stop_clicked
271
+
272
  submit_btn.click(
273
  protected_generate_response,
274
  inputs=[msg, chatbot, use_web_search, model_dropdown, temperature_slider, num_calls_slider, is_generating, stop_clicked],