Spaces:
Running
Running
Update app.py
Browse files
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
|
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 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
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],
|