Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -402,13 +402,28 @@ def summarize_web_results(query: str, search_results: List[Dict[str, str]], conv
|
|
| 402 |
except Exception as e:
|
| 403 |
return f"An error occurred during summarization: {str(e)}"
|
| 404 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 405 |
# Modify the existing respond function to handle both PDF and web search
|
| 406 |
-
def respond(message, history, model, temperature, num_calls, use_web_search, selected_docs
|
| 407 |
-
if
|
| 408 |
# If audio input is provided, transcribe it
|
| 409 |
try:
|
| 410 |
-
|
| 411 |
-
print(f"Transcribed audio: {
|
|
|
|
| 412 |
except Exception as e:
|
| 413 |
print(f"Error transcribing audio: {str(e)}")
|
| 414 |
return f"Error transcribing audio: {str(e)}", history
|
|
@@ -621,11 +636,12 @@ Write a detailed and complete response that answers the following user question:
|
|
| 621 |
|
| 622 |
logging.info("Finished generating response")
|
| 623 |
|
| 624 |
-
def transcribe(
|
|
|
|
|
|
|
|
|
|
| 625 |
headers = {"Authorization": f"Bearer {huggingface_token}"}
|
| 626 |
-
|
| 627 |
-
data = f.read()
|
| 628 |
-
response = requests.post(WHISPER_API_URL, headers=headers, data=data)
|
| 629 |
return response.json().get("text", "")
|
| 630 |
|
| 631 |
def vote(data: gr.LikeData):
|
|
@@ -677,57 +693,60 @@ use_web_search = gr.Checkbox(label="Use Web Search", value=False)
|
|
| 677 |
|
| 678 |
custom_placeholder = "Ask a question (Note: You can toggle between Web Search and PDF Chat in Additional Inputs below)"
|
| 679 |
|
| 680 |
-
#
|
| 681 |
-
|
| 682 |
-
|
| 683 |
-
|
| 684 |
-
|
| 685 |
-
|
| 686 |
-
|
| 687 |
-
|
| 688 |
-
|
| 689 |
-
|
| 690 |
-
|
| 691 |
-
|
| 692 |
-
|
| 693 |
-
|
| 694 |
-
|
| 695 |
-
|
| 696 |
-
|
| 697 |
-
|
| 698 |
-
|
| 699 |
-
|
| 700 |
-
|
| 701 |
-
|
| 702 |
-
|
| 703 |
-
|
| 704 |
-
|
| 705 |
-
|
| 706 |
-
|
| 707 |
-
|
| 708 |
-
|
| 709 |
-
|
| 710 |
-
|
| 711 |
-
|
| 712 |
-
|
| 713 |
-
|
| 714 |
-
|
| 715 |
-
|
| 716 |
-
|
| 717 |
-
[
|
| 718 |
-
|
| 719 |
-
|
| 720 |
-
|
| 721 |
-
|
| 722 |
-
|
| 723 |
-
|
| 724 |
-
|
| 725 |
-
|
| 726 |
-
|
| 727 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 728 |
)
|
| 729 |
-
)
|
| 730 |
-
|
| 731 |
# Add file upload functionality
|
| 732 |
# Add file upload functionality
|
| 733 |
with demo:
|
|
|
|
| 402 |
except Exception as e:
|
| 403 |
return f"An error occurred during summarization: {str(e)}"
|
| 404 |
|
| 405 |
+
def process_input(message, history, audio, model, temperature, num_calls, use_web_search, selected_docs):
|
| 406 |
+
if audio is not None:
|
| 407 |
+
# If audio input is provided, transcribe it
|
| 408 |
+
try:
|
| 409 |
+
transcribed_text = transcribe(audio)
|
| 410 |
+
print(f"Transcribed audio: {transcribed_text}")
|
| 411 |
+
message = transcribed_text
|
| 412 |
+
except Exception as e:
|
| 413 |
+
print(f"Error transcribing audio: {str(e)}")
|
| 414 |
+
return f"Error transcribing audio: {str(e)}", history
|
| 415 |
+
|
| 416 |
+
# Now call the existing respond function with the message (either text or transcribed audio)
|
| 417 |
+
return respond(message, history, model, temperature, num_calls, use_web_search, selected_docs)
|
| 418 |
+
|
| 419 |
# Modify the existing respond function to handle both PDF and web search
|
| 420 |
+
def respond(message, history, model, temperature, num_calls, use_web_search, selected_docs):
|
| 421 |
+
if audio is not None:
|
| 422 |
# If audio input is provided, transcribe it
|
| 423 |
try:
|
| 424 |
+
transcribed_text = transcribe(audio)
|
| 425 |
+
print(f"Transcribed audio: {transcribed_text}")
|
| 426 |
+
message = transcribed_text
|
| 427 |
except Exception as e:
|
| 428 |
print(f"Error transcribing audio: {str(e)}")
|
| 429 |
return f"Error transcribing audio: {str(e)}", history
|
|
|
|
| 636 |
|
| 637 |
logging.info("Finished generating response")
|
| 638 |
|
| 639 |
+
def transcribe(audio):
|
| 640 |
+
if audio is None:
|
| 641 |
+
return ""
|
| 642 |
+
|
| 643 |
headers = {"Authorization": f"Bearer {huggingface_token}"}
|
| 644 |
+
response = requests.post(WHISPER_API_URL, headers=headers, data=audio)
|
|
|
|
|
|
|
| 645 |
return response.json().get("text", "")
|
| 646 |
|
| 647 |
def vote(data: gr.LikeData):
|
|
|
|
| 693 |
|
| 694 |
custom_placeholder = "Ask a question (Note: You can toggle between Web Search and PDF Chat in Additional Inputs below)"
|
| 695 |
|
| 696 |
+
# Create the Gradio interface
|
| 697 |
+
with gr.Blocks() as demo:
|
| 698 |
+
gr.Markdown("# AI-powered PDF Chat and Web Search Assistant")
|
| 699 |
+
|
| 700 |
+
audio_input = gr.Audio(sources=["microphone"], type="numpy", label="Speak your query")
|
| 701 |
+
|
| 702 |
+
chat_interface = gr.ChatInterface(
|
| 703 |
+
fn=process_input,
|
| 704 |
+
additional_inputs=[
|
| 705 |
+
gr.Dropdown(choices=MODELS, label="Select Model", value=MODELS[3]),
|
| 706 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.2, step=0.1, label="Temperature"),
|
| 707 |
+
gr.Slider(minimum=1, maximum=5, value=1, step=1, label="Number of API Calls"),
|
| 708 |
+
gr.Checkbox(label="Use Web Search", value=True),
|
| 709 |
+
gr.CheckboxGroup(label="Select documents to query"),
|
| 710 |
+
audio_input
|
| 711 |
+
],
|
| 712 |
+
title="AI-powered PDF Chat and Web Search Assistant",
|
| 713 |
+
description="Chat with your PDFs, use web search to answer questions, or speak your query.",
|
| 714 |
+
theme=gr.themes.Soft(
|
| 715 |
+
primary_hue="orange",
|
| 716 |
+
secondary_hue="amber",
|
| 717 |
+
neutral_hue="gray",
|
| 718 |
+
font=[gr.themes.GoogleFont("Exo"), "ui-sans-serif", "system-ui", "sans-serif"]
|
| 719 |
+
).set(
|
| 720 |
+
body_background_fill_dark="#0c0505",
|
| 721 |
+
block_background_fill_dark="#0c0505",
|
| 722 |
+
block_border_width="1px",
|
| 723 |
+
block_title_background_fill_dark="#1b0f0f",
|
| 724 |
+
input_background_fill_dark="#140b0b",
|
| 725 |
+
button_secondary_background_fill_dark="#140b0b",
|
| 726 |
+
border_color_accent_dark="#1b0f0f",
|
| 727 |
+
border_color_primary_dark="#1b0f0f",
|
| 728 |
+
background_fill_secondary_dark="#0c0505",
|
| 729 |
+
color_accent_soft_dark="transparent",
|
| 730 |
+
code_background_fill_dark="#140b0b"
|
| 731 |
+
),
|
| 732 |
+
css=css,
|
| 733 |
+
examples=[
|
| 734 |
+
["Tell me about the contents of the uploaded PDFs."],
|
| 735 |
+
["What are the main topics discussed in the documents?"],
|
| 736 |
+
["Can you summarize the key points from the PDFs?"],
|
| 737 |
+
["What's the latest news about artificial intelligence?"]
|
| 738 |
+
],
|
| 739 |
+
cache_examples=False,
|
| 740 |
+
analytics_enabled=False,
|
| 741 |
+
textbox=gr.Textbox(placeholder="Ask a question about the uploaded PDFs or any topic", container=False, scale=7),
|
| 742 |
+
chatbot = gr.Chatbot(
|
| 743 |
+
show_copy_button=True,
|
| 744 |
+
likeable=True,
|
| 745 |
+
layout="bubble",
|
| 746 |
+
height=400,
|
| 747 |
+
value=initial_conversation()
|
| 748 |
+
)
|
| 749 |
)
|
|
|
|
|
|
|
| 750 |
# Add file upload functionality
|
| 751 |
# Add file upload functionality
|
| 752 |
with demo:
|