Spaces:
Sleeping
Sleeping
Update Gradio_UI.py
Browse files- Gradio_UI.py +90 -43
Gradio_UI.py
CHANGED
|
@@ -1,52 +1,101 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
|
| 3 |
class GradioUI:
|
| 4 |
def __init__(self, agent):
|
| 5 |
# The agent object passed from app.py
|
| 6 |
self.agent = agent
|
| 7 |
|
| 8 |
-
def
|
| 9 |
"""
|
| 10 |
-
Processes
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
"""
|
| 13 |
-
# Check if
|
| 14 |
-
if not query
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
def launch(self):
|
| 30 |
"""
|
| 31 |
-
Launches the Gradio user interface for the agent.
|
| 32 |
"""
|
| 33 |
# Use gr.Blocks for a more flexible layout
|
| 34 |
with gr.Blocks() as demo:
|
| 35 |
-
gr.Markdown("# Multi-Tool AI Agent")
|
| 36 |
gr.Markdown(
|
| 37 |
-
"Enter your request
|
| 38 |
-
"
|
| 39 |
-
"
|
| 40 |
)
|
| 41 |
|
| 42 |
-
#
|
| 43 |
query_input = gr.Textbox(
|
| 44 |
-
label="Enter your request
|
| 45 |
-
placeholder="e.g., What is the weather in London? Search for the latest news about AI.
|
| 46 |
lines=3, # Provides more space for typing longer queries
|
| 47 |
interactive=True # User can type in this box
|
| 48 |
)
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
# Single output field to display the agent's final response
|
| 51 |
agent_output = gr.Textbox(
|
| 52 |
label="Agent's Response:",
|
|
@@ -55,29 +104,27 @@ class GradioUI:
|
|
| 55 |
autoscroll=True # Automatically scrolls to the latest output
|
| 56 |
)
|
| 57 |
|
| 58 |
-
# Button to trigger the
|
| 59 |
-
submit_btn = gr.Button("Submit
|
| 60 |
|
| 61 |
-
# Link the button click event to the
|
| 62 |
-
#
|
| 63 |
-
# The return value of the function will be displayed in agent_output.
|
| 64 |
submit_btn.click(
|
| 65 |
-
fn=self.
|
| 66 |
-
inputs=query_input,
|
| 67 |
-
outputs=agent_output
|
| 68 |
)
|
| 69 |
|
| 70 |
-
# Optional
|
| 71 |
-
# You would need to add example queries
|
| 72 |
# examples = [
|
| 73 |
-
# ["What is the
|
| 74 |
-
# ["
|
| 75 |
-
#
|
| 76 |
-
#
|
| 77 |
-
# #
|
| 78 |
-
# # ["What is the main topic of the document?", "path/to/your/default.pdf"] # Example with file input if you re-add it
|
| 79 |
# ]
|
| 80 |
-
# gr.Examples(examples=examples, inputs=query_input)
|
| 81 |
|
| 82 |
|
| 83 |
# Launch the Gradio interface
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import os # Import os to potentially handle file paths if needed, though Gradio provides the path
|
| 3 |
|
| 4 |
class GradioUI:
|
| 5 |
def __init__(self, agent):
|
| 6 |
# The agent object passed from app.py
|
| 7 |
self.agent = agent
|
| 8 |
|
| 9 |
+
def process_input(self, query: str, pdf_file: gr.File = None):
|
| 10 |
"""
|
| 11 |
+
Processes user input, which can include a text query and optionally a PDF file.
|
| 12 |
+
If a PDF is uploaded, it assumes the query is a question about it and calls
|
| 13 |
+
the Document Q&A tool directly. Otherwise, it sends the query to the agent.run() method.
|
| 14 |
+
|
| 15 |
+
Args:
|
| 16 |
+
query (str): The text query from the user.
|
| 17 |
+
pdf_file (gr.File, optional): The uploaded PDF file object provided by Gradio. Defaults to None.
|
| 18 |
+
|
| 19 |
+
Returns:
|
| 20 |
+
str: The response from the agent or tool.
|
| 21 |
"""
|
| 22 |
+
# Check if a PDF file is uploaded AND there is a text query (assumed to be the question)
|
| 23 |
+
if pdf_file is not None and query and query.strip():
|
| 24 |
+
print(f"PDF file uploaded: {pdf_file.name}")
|
| 25 |
+
print(f"Query (assumed question for PDF): {query}")
|
| 26 |
+
try:
|
| 27 |
+
# --- Call the Document Q&A tool directly ---
|
| 28 |
+
# We assume the document_qna_tool is at index 4 as per your app.py initialization
|
| 29 |
+
# Gradio's gr.File object has a .name attribute which is the file path
|
| 30 |
+
# The document_qna_tool expects a file path string.
|
| 31 |
+
print("Detected PDF upload and query. Calling document_qna_tool...")
|
| 32 |
+
# The tool signature is document_qna_tool(pdf_path: str, question: str)
|
| 33 |
+
response = self.agent.tools[4](pdf_file.name, query)
|
| 34 |
+
print("Document Q&A tool finished.")
|
| 35 |
+
# Clean up the uploaded file after processing (optional but good practice)
|
| 36 |
+
# if os.path.exists(pdf_file.name):
|
| 37 |
+
# os.remove(pdf_file.name)
|
| 38 |
+
return response
|
| 39 |
+
|
| 40 |
+
except IndexError:
|
| 41 |
+
return "Error: Document Q&A tool not found at the expected index (4). Check agent tool setup in app.py."
|
| 42 |
+
except Exception as e:
|
| 43 |
+
print(f"Error during Document Q&A tool execution: {e}")
|
| 44 |
+
# Clean up the uploaded file on error too
|
| 45 |
+
# if os.path.exists(pdf_file.name):
|
| 46 |
+
# os.remove(pdf_file.name)
|
| 47 |
+
return f"An error occurred during Document Q&A: {str(e)}"
|
| 48 |
+
|
| 49 |
+
# If no PDF file, or query is empty when file is present, handle as a general agent query
|
| 50 |
+
elif query and query.strip():
|
| 51 |
+
print(f"No PDF file or query is for general task. Processing with agent.run(): {query}")
|
| 52 |
+
try:
|
| 53 |
+
# --- Call the agent's run method for general queries ---
|
| 54 |
+
response = self.agent.run(query)
|
| 55 |
+
print("Agent.run finished for general query.")
|
| 56 |
+
return response
|
| 57 |
+
except Exception as e:
|
| 58 |
+
print(f"Error during agent.run: {e}")
|
| 59 |
+
return f"An error occurred while processing your request: {str(e)}"
|
| 60 |
+
|
| 61 |
+
# Handle cases where only a PDF is uploaded without a question, or no input at all
|
| 62 |
+
elif pdf_file is not None:
|
| 63 |
+
# Clean up the uploaded file if no question was provided
|
| 64 |
+
# if os.path.exists(pdf_file.name):
|
| 65 |
+
# os.remove(pdf_file.name)
|
| 66 |
+
return "Please enter a question in the textbox to ask about the uploaded PDF."
|
| 67 |
+
else:
|
| 68 |
+
return "Please enter a request or upload a document for analysis."
|
| 69 |
+
|
| 70 |
|
| 71 |
def launch(self):
|
| 72 |
"""
|
| 73 |
+
Launches the Gradio user interface for the agent with file upload capability.
|
| 74 |
"""
|
| 75 |
# Use gr.Blocks for a more flexible layout
|
| 76 |
with gr.Blocks() as demo:
|
| 77 |
+
gr.Markdown("# Multi-Tool AI Agent with Document Upload")
|
| 78 |
gr.Markdown(
|
| 79 |
+
"Enter your request and optionally upload a PDF document. "
|
| 80 |
+
"If you upload a PDF, the text box should contain your question about the document. "
|
| 81 |
+
"Otherwise, use the text box for general requests (weather, search, etc.)."
|
| 82 |
)
|
| 83 |
|
| 84 |
+
# Text input for the user's query or question
|
| 85 |
query_input = gr.Textbox(
|
| 86 |
+
label="Enter your request or question:",
|
| 87 |
+
placeholder="e.g., What is the weather in London? Search for the latest news about AI. What does this document say about [topic]? Summarize the document.",
|
| 88 |
lines=3, # Provides more space for typing longer queries
|
| 89 |
interactive=True # User can type in this box
|
| 90 |
)
|
| 91 |
|
| 92 |
+
# File upload component specifically for PDF documents
|
| 93 |
+
pdf_upload = gr.File(
|
| 94 |
+
label="Upload a PDF Document (Optional - for Document Q&A)",
|
| 95 |
+
file_types=[".pdf"], # Restrict file types to PDF
|
| 96 |
+
interactive=True # User can upload files
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
# Single output field to display the agent's final response
|
| 100 |
agent_output = gr.Textbox(
|
| 101 |
label="Agent's Response:",
|
|
|
|
| 104 |
autoscroll=True # Automatically scrolls to the latest output
|
| 105 |
)
|
| 106 |
|
| 107 |
+
# Button to trigger the processing function
|
| 108 |
+
submit_btn = gr.Button("Submit")
|
| 109 |
|
| 110 |
+
# Link the button click event to the process_input function
|
| 111 |
+
# Pass both query_input (text) and pdf_upload (file) as inputs
|
|
|
|
| 112 |
submit_btn.click(
|
| 113 |
+
fn=self.process_input,
|
| 114 |
+
inputs=[query_input, pdf_upload], # This list specifies all inputs
|
| 115 |
+
outputs=agent_output # This specifies the output where the return value goes
|
| 116 |
)
|
| 117 |
|
| 118 |
+
# Optional examples - adjust these based on your tools and whether you have default files
|
| 119 |
+
# You would need to add example queries and potentially example files if you use them.
|
| 120 |
# examples = [
|
| 121 |
+
# ["What is the time in Berlin?"],
|
| 122 |
+
# ["Generate an image of a robot cooking pasta."],
|
| 123 |
+
# # Example for document Q&A - requires a default file path accessible by the tool
|
| 124 |
+
# # For file examples, you need to provide the path to the example file(s)
|
| 125 |
+
# # ["Summarize the introduction section", "path/to/your/sample_document.pdf"]
|
|
|
|
| 126 |
# ]
|
| 127 |
+
# gr.Examples(examples=examples, inputs=[query_input, pdf_upload]) # Examples take same inputs as function
|
| 128 |
|
| 129 |
|
| 130 |
# Launch the Gradio interface
|