| import gradio as gr |
| import os |
|
|
| class GradioUI: |
| def __init__(self, agent): |
| self.agent = agent |
|
|
| def process_input(self, query: str, pdf_file: gr.File = None): |
| """ |
| Processes user input, which can include a text query and optionally a PDF file. |
| If a PDF is uploaded, it assumes the query is a question about it and calls |
| the Document Q&A tool directly. Otherwise, it sends the query to the agent.run() method. |
| Args: |
| query (str): The text query from the user. |
| pdf_file (gr.File, optional): The uploaded PDF file object provided by Gradio. Defaults to None. |
| Returns: |
| str: The response from the agent or tool. |
| """ |
| |
| if pdf_file is not None and query and query.strip(): |
| print(f"PDF file uploaded: {pdf_file.name}") |
| print(f"Query (assumed question for PDF): {query}") |
| try: |
| |
| |
| |
| |
| print("Detected PDF upload and query. Calling document_qna_tool...") |
| |
| response = self.agent.tools["document_qna_tool"](pdf_file.name, query) |
| print("Document Q&A tool finished.") |
| |
| |
| |
| |
| |
| |
| |
| |
| return response |
|
|
| except IndexError: |
| return "Error: Document Q&A tool not found at the expected index (4). Check agent tool setup in app.py." |
| except Exception as e: |
| import traceback |
| print(f"Error during Document Q&A tool execution: {e}") |
| print("Full Traceback:") |
| print(traceback.format_exc()) |
| return f"An error occurred during Document Q&A: {str(e)}" |
|
|
| |
| elif query and query.strip(): |
| print(f"No PDF file or query is for general task. Processing with agent.run(): {query}") |
| try: |
| |
| response = self.agent.run(query) |
| print("Agent.run finished for general query.") |
| return response |
| except Exception as e: |
| print(f"Error during agent.run: {e}") |
| return f"An error occurred while processing your request: {str(e)}" |
|
|
| |
| elif pdf_file is not None: |
| |
| |
| |
| |
| |
| |
| |
| |
| return "Please enter a question in the textbox to ask about the uploaded PDF." |
| else: |
| return "Please enter a request or upload a document for analysis." |
|
|
|
|
| def launch(self): |
| """ |
| Launches the Gradio user interface with input components stacked vertically. |
| """ |
| with gr.Blocks() as demo: |
| gr.Markdown("# Multi-Tool AI Agent with Document Upload") |
| gr.Markdown( |
| "Enter your request and optionally upload a PDF document. " |
| "If you upload a PDF, the text box should contain your question about the document. " |
| "Otherwise, use the text box for general requests (weather, search, etc.)." |
| ) |
|
|
| |
| |
| query_input = gr.Textbox( |
| label="Enter your request or question:", |
| 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.", |
| lines=3, |
| interactive=True |
| ) |
|
|
| |
| pdf_upload = gr.File( |
| label="Upload PDF (Optional - for Document Q&A)", |
| file_types=[".pdf"], |
| interactive=True, |
| |
| ) |
|
|
| |
| submit_btn = gr.Button("Submit") |
|
|
| |
| agent_output = gr.Textbox( |
| label="Agent's Response:", |
| interactive=False, |
| lines=10, |
| autoscroll=True |
| ) |
|
|
| |
| |
| submit_btn.click( |
| fn=self.process_input, |
| inputs=[query_input, pdf_upload], |
| outputs=agent_output |
| ) |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
|
|
| |
| |
| |
| demo.launch(share=False, inline=False) |