Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -195,19 +195,28 @@ def clear_and_update_chat(message, history, use_web_search, model, temperature):
|
|
195 |
updated_history = chatbot_interface(message, history, use_web_search, model, temperature)
|
196 |
return "", updated_history # Return empty string to clear the input
|
197 |
|
198 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
199 |
with gr.Blocks() as demo:
|
200 |
-
|
201 |
is_generating = gr.State(False)
|
|
|
202 |
|
203 |
-
def protected_clear_and_update_chat(message, history, use_web_search, model, temperature, is_generating):
|
204 |
-
if is_generating:
|
205 |
-
return message, history, is_generating
|
206 |
-
is_generating = True
|
207 |
-
updated_message, updated_history = clear_and_update_chat(message, history, use_web_search, model, temperature)
|
208 |
-
is_generating = False
|
209 |
-
return updated_message, updated_history, is_generating
|
210 |
-
|
211 |
gr.Markdown("# AI-powered Web Search and PDF Chat Assistant")
|
212 |
|
213 |
with gr.Row():
|
@@ -225,8 +234,71 @@ with gr.Blocks() as demo:
|
|
225 |
with gr.Row():
|
226 |
model_dropdown = gr.Dropdown(choices=MODELS, label="Select Model", value=MODELS[1])
|
227 |
temperature_slider = gr.Slider(minimum=0.1, maximum=1.0, value=0.2, step=0.1, label="Temperature")
|
|
|
228 |
|
229 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
230 |
|
231 |
gr.Examples(
|
232 |
examples=[
|
@@ -238,13 +310,6 @@ with gr.Blocks() as demo:
|
|
238 |
inputs=msg,
|
239 |
)
|
240 |
|
241 |
-
submit.click(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 |
-
msg.submit(protected_clear_and_update_chat,
|
245 |
-
inputs=[msg, chatbot, use_web_search, model_dropdown, temperature_slider, is_generating],
|
246 |
-
outputs=[msg, chatbot, is_generating])
|
247 |
-
|
248 |
gr.Markdown(
|
249 |
"""
|
250 |
## How to use
|
@@ -252,8 +317,10 @@ with gr.Blocks() as demo:
|
|
252 |
2. Select the PDF parser (pypdf or llamaparse) and click "Upload Document" to update the vector store.
|
253 |
3. Ask questions in the textbox.
|
254 |
4. Toggle "Use Web Search" to switch between PDF chat and web search.
|
255 |
-
5. Adjust Temperature and
|
256 |
-
6. Click "
|
|
|
|
|
257 |
"""
|
258 |
)
|
259 |
|
|
|
195 |
updated_history = chatbot_interface(message, history, use_web_search, model, temperature)
|
196 |
return "", updated_history # Return empty string to clear the input
|
197 |
|
198 |
+
def retry_last_response(history):
|
199 |
+
if history:
|
200 |
+
last_user_message = history[-1][0]
|
201 |
+
return last_user_message, history[:-1]
|
202 |
+
return "", history
|
203 |
+
|
204 |
+
def undo_last_interaction(history):
|
205 |
+
if len(history) >= 1:
|
206 |
+
return history[:-1]
|
207 |
+
return history
|
208 |
+
|
209 |
+
def clear_conversation():
|
210 |
+
return []
|
211 |
+
|
212 |
+
def stop_generation():
|
213 |
+
global is_generating
|
214 |
+
is_generating = False
|
215 |
+
|
216 |
with gr.Blocks() as demo:
|
|
|
217 |
is_generating = gr.State(False)
|
218 |
+
stop_clicked = gr.State(False)
|
219 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
220 |
gr.Markdown("# AI-powered Web Search and PDF Chat Assistant")
|
221 |
|
222 |
with gr.Row():
|
|
|
234 |
with gr.Row():
|
235 |
model_dropdown = gr.Dropdown(choices=MODELS, label="Select Model", value=MODELS[1])
|
236 |
temperature_slider = gr.Slider(minimum=0.1, maximum=1.0, value=0.2, step=0.1, label="Temperature")
|
237 |
+
num_calls_slider = gr.Slider(minimum=1, maximum=5, value=3, step=1, label="Number of API Calls")
|
238 |
|
239 |
+
with gr.Row():
|
240 |
+
submit_btn = gr.Button("Send")
|
241 |
+
stop_btn = gr.Button("Stop", visible=False)
|
242 |
+
retry_btn = gr.Button("Retry")
|
243 |
+
undo_btn = gr.Button("Undo")
|
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],
|
272 |
+
outputs=[msg, chatbot, is_generating, stop_clicked],
|
273 |
+
show_progress=True
|
274 |
+
).then(
|
275 |
+
lambda: gr.update(visible=True),
|
276 |
+
None,
|
277 |
+
stop_btn
|
278 |
+
).then(
|
279 |
+
lambda: gr.update(visible=False),
|
280 |
+
None,
|
281 |
+
stop_btn
|
282 |
+
)
|
283 |
+
|
284 |
+
stop_btn.click(
|
285 |
+
lambda: (True, gr.update(visible=False)),
|
286 |
+
None,
|
287 |
+
[stop_clicked, stop_btn]
|
288 |
+
)
|
289 |
+
|
290 |
+
retry_btn.click(
|
291 |
+
retry_last_response,
|
292 |
+
inputs=[chatbot],
|
293 |
+
outputs=[msg, chatbot]
|
294 |
+
).then(
|
295 |
+
protected_generate_response,
|
296 |
+
inputs=[msg, chatbot, use_web_search, model_dropdown, temperature_slider, num_calls_slider, is_generating, stop_clicked],
|
297 |
+
outputs=[msg, chatbot, is_generating, stop_clicked]
|
298 |
+
)
|
299 |
+
|
300 |
+
undo_btn.click(undo_last_interaction, inputs=[chatbot], outputs=[chatbot])
|
301 |
+
clear_btn.click(clear_conversation, outputs=[chatbot])
|
302 |
|
303 |
gr.Examples(
|
304 |
examples=[
|
|
|
310 |
inputs=msg,
|
311 |
)
|
312 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
313 |
gr.Markdown(
|
314 |
"""
|
315 |
## How to use
|
|
|
317 |
2. Select the PDF parser (pypdf or llamaparse) and click "Upload Document" to update the vector store.
|
318 |
3. Ask questions in the textbox.
|
319 |
4. Toggle "Use Web Search" to switch between PDF chat and web search.
|
320 |
+
5. Adjust Temperature and Number of API Calls sliders to fine-tune the response generation.
|
321 |
+
6. Click "Send" or press Enter to get a response.
|
322 |
+
7. Use "Retry" to regenerate the last response, "Undo" to remove the last interaction, and "Clear" to reset the conversation.
|
323 |
+
8. Click "Stop" during generation to halt the process.
|
324 |
"""
|
325 |
)
|
326 |
|