Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -17,11 +17,6 @@ from huggingface_hub import InferenceClient
|
|
| 17 |
import inspect
|
| 18 |
import logging
|
| 19 |
import shutil
|
| 20 |
-
import soundfile as sf
|
| 21 |
-
from transformers import WhisperProcessor, WhisperForConditionalGeneration
|
| 22 |
-
from transformers import pipeline
|
| 23 |
-
import numpy as np
|
| 24 |
-
|
| 25 |
|
| 26 |
|
| 27 |
# Set up basic configuration for logging
|
|
@@ -33,7 +28,8 @@ llama_cloud_api_key = os.environ.get("LLAMA_CLOUD_API_KEY")
|
|
| 33 |
ACCOUNT_ID = os.environ.get("CLOUDFARE_ACCOUNT_ID")
|
| 34 |
API_TOKEN = os.environ.get("CLOUDFLARE_AUTH_TOKEN")
|
| 35 |
API_BASE_URL = "https://api.cloudflare.com/client/v4/accounts/a17f03e0f049ccae0c15cdcf3b9737ce/ai/run/"
|
| 36 |
-
|
|
|
|
| 37 |
|
| 38 |
print(f"ACCOUNT_ID: {ACCOUNT_ID}")
|
| 39 |
print(f"CLOUDFLARE_AUTH_TOKEN: {API_TOKEN[:5]}..." if API_TOKEN else "Not set")
|
|
@@ -402,20 +398,27 @@ 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 |
-
def
|
| 406 |
-
if audio is
|
| 407 |
-
|
| 408 |
-
transcribed_text = transcribe(audio)
|
| 409 |
-
print(f"Transcribed audio: {transcribed_text}")
|
| 410 |
-
message = transcribed_text
|
| 411 |
-
except Exception as e:
|
| 412 |
-
print(f"Error transcribing audio: {str(e)}")
|
| 413 |
-
return f"Error transcribing audio: {str(e)}", history
|
| 414 |
|
| 415 |
-
|
|
|
|
|
|
|
|
|
|
| 416 |
|
| 417 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 418 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 419 |
logging.info(f"User Query: {message}")
|
| 420 |
logging.info(f"Model Used: {model}")
|
| 421 |
logging.info(f"Selected Documents: {selected_docs}")
|
|
@@ -624,14 +627,6 @@ Write a detailed and complete response that answers the following user question:
|
|
| 624 |
|
| 625 |
logging.info("Finished generating response")
|
| 626 |
|
| 627 |
-
def transcribe(audio):
|
| 628 |
-
if audio is None:
|
| 629 |
-
return ""
|
| 630 |
-
|
| 631 |
-
headers = {"Authorization": f"Bearer {huggingface_token}"}
|
| 632 |
-
response = requests.post(WHISPER_API_URL, headers=headers, data=audio)
|
| 633 |
-
return response.json().get("text", "")
|
| 634 |
-
|
| 635 |
def vote(data: gr.LikeData):
|
| 636 |
if data.liked:
|
| 637 |
print(f"You upvoted this response: {data.value}")
|
|
@@ -659,7 +654,6 @@ def display_documents():
|
|
| 659 |
label="Select documents to query or delete"
|
| 660 |
)
|
| 661 |
|
| 662 |
-
# Define initial conversation
|
| 663 |
def initial_conversation():
|
| 664 |
return [
|
| 665 |
(None, "Welcome! I'm your AI assistant for web search and PDF analysis. Here's how you can use me:\n\n"
|
|
@@ -682,64 +676,60 @@ use_web_search = gr.Checkbox(label="Use Web Search", value=False)
|
|
| 682 |
|
| 683 |
custom_placeholder = "Ask a question (Note: You can toggle between Web Search and PDF Chat in Additional Inputs below)"
|
| 684 |
|
| 685 |
-
#
|
| 686 |
-
#
|
| 687 |
-
|
| 688 |
-
|
| 689 |
-
|
| 690 |
-
|
| 691 |
-
|
| 692 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 693 |
show_copy_button=True,
|
| 694 |
likeable=True,
|
| 695 |
layout="bubble",
|
| 696 |
height=400,
|
| 697 |
value=initial_conversation()
|
| 698 |
)
|
| 699 |
-
|
| 700 |
-
chat_interface = gr.ChatInterface(
|
| 701 |
-
fn=process_input,
|
| 702 |
-
chatbot=chatbot,
|
| 703 |
-
additional_inputs=[
|
| 704 |
-
gr.Dropdown(choices=MODELS, label="Select Model", value=MODELS[3]),
|
| 705 |
-
gr.Slider(minimum=0.1, maximum=1.0, value=0.2, step=0.1, label="Temperature"),
|
| 706 |
-
gr.Slider(minimum=1, maximum=5, value=1, step=1, label="Number of API Calls"),
|
| 707 |
-
gr.Checkbox(label="Use Web Search", value=True),
|
| 708 |
-
gr.CheckboxGroup(label="Select documents to query"),
|
| 709 |
-
audio_input
|
| 710 |
-
],
|
| 711 |
-
title="AI-powered PDF Chat and Web Search Assistant",
|
| 712 |
-
description="Chat with your PDFs, use web search to answer questions, or speak your query.",
|
| 713 |
-
theme=gr.themes.Soft(
|
| 714 |
-
primary_hue="orange",
|
| 715 |
-
secondary_hue="amber",
|
| 716 |
-
neutral_hue="gray",
|
| 717 |
-
font=[gr.themes.GoogleFont("Exo"), "ui-sans-serif", "system-ui", "sans-serif"]
|
| 718 |
-
).set(
|
| 719 |
-
body_background_fill_dark="#0c0505",
|
| 720 |
-
block_background_fill_dark="#0c0505",
|
| 721 |
-
block_border_width="1px",
|
| 722 |
-
block_title_background_fill_dark="#1b0f0f",
|
| 723 |
-
input_background_fill_dark="#140b0b",
|
| 724 |
-
button_secondary_background_fill_dark="#140b0b",
|
| 725 |
-
border_color_accent_dark="#1b0f0f",
|
| 726 |
-
border_color_primary_dark="#1b0f0f",
|
| 727 |
-
background_fill_secondary_dark="#0c0505",
|
| 728 |
-
color_accent_soft_dark="transparent",
|
| 729 |
-
code_background_fill_dark="#140b0b"
|
| 730 |
-
),
|
| 731 |
-
css=css,
|
| 732 |
-
examples=[
|
| 733 |
-
["Tell me about the contents of the uploaded PDFs."],
|
| 734 |
-
["What are the main topics discussed in the documents?"],
|
| 735 |
-
["Can you summarize the key points from the PDFs?"],
|
| 736 |
-
["What's the latest news about artificial intelligence?"]
|
| 737 |
-
],
|
| 738 |
-
cache_examples=False,
|
| 739 |
-
analytics_enabled=False,
|
| 740 |
-
textbox=gr.Textbox(placeholder="Ask a question about the uploaded PDFs or any topic", container=False, scale=7),
|
| 741 |
-
)
|
| 742 |
|
|
|
|
|
|
|
|
|
|
| 743 |
gr.Markdown("## Upload and Manage PDF Documents")
|
| 744 |
with gr.Row():
|
| 745 |
file_input = gr.Files(label="Upload your PDF documents", file_types=[".pdf"])
|
|
@@ -750,23 +740,25 @@ with gr.Blocks() as demo:
|
|
| 750 |
update_output = gr.Textbox(label="Update Status")
|
| 751 |
delete_button = gr.Button("Delete Selected Documents")
|
| 752 |
|
| 753 |
-
#
|
| 754 |
update_button.click(
|
| 755 |
update_vectors,
|
| 756 |
inputs=[file_input, parser_dropdown],
|
| 757 |
-
outputs=[update_output,
|
| 758 |
)
|
| 759 |
|
|
|
|
| 760 |
refresh_button.click(
|
| 761 |
refresh_documents,
|
| 762 |
inputs=[],
|
| 763 |
-
outputs=[
|
| 764 |
)
|
| 765 |
|
|
|
|
| 766 |
delete_button.click(
|
| 767 |
delete_documents,
|
| 768 |
-
inputs=[
|
| 769 |
-
outputs=[update_output,
|
| 770 |
)
|
| 771 |
|
| 772 |
gr.Markdown(
|
|
@@ -783,4 +775,4 @@ with gr.Blocks() as demo:
|
|
| 783 |
)
|
| 784 |
|
| 785 |
if __name__ == "__main__":
|
| 786 |
-
demo.launch(share=True)
|
|
|
|
| 17 |
import inspect
|
| 18 |
import logging
|
| 19 |
import shutil
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
|
| 22 |
# Set up basic configuration for logging
|
|
|
|
| 28 |
ACCOUNT_ID = os.environ.get("CLOUDFARE_ACCOUNT_ID")
|
| 29 |
API_TOKEN = os.environ.get("CLOUDFLARE_AUTH_TOKEN")
|
| 30 |
API_BASE_URL = "https://api.cloudflare.com/client/v4/accounts/a17f03e0f049ccae0c15cdcf3b9737ce/ai/run/"
|
| 31 |
+
API_URL = "https://api-inference.huggingface.co/models/openai/whisper-large-v3"
|
| 32 |
+
headers={"Authorization": f"Bearer {huggingface_token}"}
|
| 33 |
|
| 34 |
print(f"ACCOUNT_ID: {ACCOUNT_ID}")
|
| 35 |
print(f"CLOUDFLARE_AUTH_TOKEN: {API_TOKEN[:5]}..." if API_TOKEN else "Not set")
|
|
|
|
| 398 |
except Exception as e:
|
| 399 |
return f"An error occurred during summarization: {str(e)}"
|
| 400 |
|
| 401 |
+
def transcribe_audio(audio):
|
| 402 |
+
if audio is None:
|
| 403 |
+
return "No audio file provided."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 404 |
|
| 405 |
+
try:
|
| 406 |
+
sr, y = audio
|
| 407 |
+
y = y.astype(np.float32)
|
| 408 |
+
y /= np.max(np.abs(y))
|
| 409 |
|
| 410 |
+
response = requests.post(API_URL, headers=headers, json={"inputs": {"sampling_rate": sr, "raw": y.tolist()}})
|
| 411 |
+
response.raise_for_status()
|
| 412 |
+
return response.json()["text"]
|
| 413 |
+
except Exception as e:
|
| 414 |
+
return f"An error occurred during transcription: {str(e)}"
|
| 415 |
|
| 416 |
+
# Modify the existing respond function to handle both PDF and web search
|
| 417 |
+
def respond(message, history, model, temperature, num_calls, use_web_search, selected_docs, audio_input):
|
| 418 |
+
if audio_input is not None:
|
| 419 |
+
transcription = transcribe_audio(audio_input)
|
| 420 |
+
message = f"Transcription: {transcription}\n\nUser query: {message}"
|
| 421 |
+
|
| 422 |
logging.info(f"User Query: {message}")
|
| 423 |
logging.info(f"Model Used: {model}")
|
| 424 |
logging.info(f"Selected Documents: {selected_docs}")
|
|
|
|
| 627 |
|
| 628 |
logging.info("Finished generating response")
|
| 629 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 630 |
def vote(data: gr.LikeData):
|
| 631 |
if data.liked:
|
| 632 |
print(f"You upvoted this response: {data.value}")
|
|
|
|
| 654 |
label="Select documents to query or delete"
|
| 655 |
)
|
| 656 |
|
|
|
|
| 657 |
def initial_conversation():
|
| 658 |
return [
|
| 659 |
(None, "Welcome! I'm your AI assistant for web search and PDF analysis. Here's how you can use me:\n\n"
|
|
|
|
| 676 |
|
| 677 |
custom_placeholder = "Ask a question (Note: You can toggle between Web Search and PDF Chat in Additional Inputs below)"
|
| 678 |
|
| 679 |
+
# Update the demo interface
|
| 680 |
+
# Update the Gradio interface
|
| 681 |
+
demo = gr.ChatInterface(
|
| 682 |
+
respond,
|
| 683 |
+
additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=True, render=False),
|
| 684 |
+
additional_inputs=[
|
| 685 |
+
gr.Dropdown(choices=MODELS, label="Select Model", value=MODELS[3]),
|
| 686 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.2, step=0.1, label="Temperature"),
|
| 687 |
+
gr.Slider(minimum=1, maximum=5, value=1, step=1, label="Number of API Calls"),
|
| 688 |
+
gr.Checkbox(label="Use Web Search", value=True),
|
| 689 |
+
gr.CheckboxGroup(label="Select documents to query")
|
| 690 |
+
],
|
| 691 |
+
title="AI-powered PDF Chat and Web Search Assistant",
|
| 692 |
+
description="Chat with your PDFs or use web search to answer questions.",
|
| 693 |
+
theme=gr.themes.Soft(
|
| 694 |
+
primary_hue="orange",
|
| 695 |
+
secondary_hue="amber",
|
| 696 |
+
neutral_hue="gray",
|
| 697 |
+
font=[gr.themes.GoogleFont("Exo"), "ui-sans-serif", "system-ui", "sans-serif"]
|
| 698 |
+
).set(
|
| 699 |
+
body_background_fill_dark="#0c0505",
|
| 700 |
+
block_background_fill_dark="#0c0505",
|
| 701 |
+
block_border_width="1px",
|
| 702 |
+
block_title_background_fill_dark="#1b0f0f",
|
| 703 |
+
input_background_fill_dark="#140b0b",
|
| 704 |
+
button_secondary_background_fill_dark="#140b0b",
|
| 705 |
+
border_color_accent_dark="#1b0f0f",
|
| 706 |
+
border_color_primary_dark="#1b0f0f",
|
| 707 |
+
background_fill_secondary_dark="#0c0505",
|
| 708 |
+
color_accent_soft_dark="transparent",
|
| 709 |
+
code_background_fill_dark="#140b0b"
|
| 710 |
+
),
|
| 711 |
+
css=css,
|
| 712 |
+
examples=[
|
| 713 |
+
["Tell me about the contents of the uploaded PDFs."],
|
| 714 |
+
["What are the main topics discussed in the documents?"],
|
| 715 |
+
["Can you summarize the key points from the PDFs?"],
|
| 716 |
+
["What's the latest news about artificial intelligence?"]
|
| 717 |
+
],
|
| 718 |
+
cache_examples=False,
|
| 719 |
+
analytics_enabled=False,
|
| 720 |
+
textbox=gr.Textbox(placeholder="Ask a question about the uploaded PDFs or any topic", container=False, scale=7),
|
| 721 |
+
chatbot = gr.Chatbot(
|
| 722 |
show_copy_button=True,
|
| 723 |
likeable=True,
|
| 724 |
layout="bubble",
|
| 725 |
height=400,
|
| 726 |
value=initial_conversation()
|
| 727 |
)
|
| 728 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 729 |
|
| 730 |
+
# Add file upload functionality
|
| 731 |
+
# Add file upload functionality
|
| 732 |
+
with demo:
|
| 733 |
gr.Markdown("## Upload and Manage PDF Documents")
|
| 734 |
with gr.Row():
|
| 735 |
file_input = gr.Files(label="Upload your PDF documents", file_types=[".pdf"])
|
|
|
|
| 740 |
update_output = gr.Textbox(label="Update Status")
|
| 741 |
delete_button = gr.Button("Delete Selected Documents")
|
| 742 |
|
| 743 |
+
# Update both the output text and the document selector
|
| 744 |
update_button.click(
|
| 745 |
update_vectors,
|
| 746 |
inputs=[file_input, parser_dropdown],
|
| 747 |
+
outputs=[update_output, demo.additional_inputs[-1]] # Use the CheckboxGroup from additional_inputs
|
| 748 |
)
|
| 749 |
|
| 750 |
+
# Add the refresh button functionality
|
| 751 |
refresh_button.click(
|
| 752 |
refresh_documents,
|
| 753 |
inputs=[],
|
| 754 |
+
outputs=[demo.additional_inputs[-1]] # Use the CheckboxGroup from additional_inputs
|
| 755 |
)
|
| 756 |
|
| 757 |
+
# Add the delete button functionality
|
| 758 |
delete_button.click(
|
| 759 |
delete_documents,
|
| 760 |
+
inputs=[demo.additional_inputs[-1]], # Use the CheckboxGroup from additional_inputs
|
| 761 |
+
outputs=[update_output, demo.additional_inputs[-1]]
|
| 762 |
)
|
| 763 |
|
| 764 |
gr.Markdown(
|
|
|
|
| 775 |
)
|
| 776 |
|
| 777 |
if __name__ == "__main__":
|
| 778 |
+
demo.launch(share=True)
|