Spaces:
Sleeping
Sleeping
Update Gradio_UI.py
Browse files- Gradio_UI.py +77 -76
Gradio_UI.py
CHANGED
|
@@ -2,84 +2,85 @@ import gradio as gr
|
|
| 2 |
|
| 3 |
class GradioUI:
|
| 4 |
def __init__(self, agent):
|
|
|
|
| 5 |
self.agent = agent
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
def launch(self):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
with gr.Blocks() as demo:
|
| 9 |
gr.Markdown("# Multi-Tool AI Agent")
|
| 10 |
-
gr.Markdown(
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
#
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
def search(q):
|
| 63 |
-
if not q.strip():
|
| 64 |
-
return "Please enter a search query."
|
| 65 |
-
return self.agent.tools[3](q)
|
| 66 |
-
|
| 67 |
-
search_btn.click(search, inputs=search_query, outputs=search_output)
|
| 68 |
-
|
| 69 |
-
# Tab 5: Document Q&A
|
| 70 |
-
with gr.TabItem("Document Q&A"):
|
| 71 |
-
pdf_upload = gr.File(label="Upload PDF Document", file_types=[".pdf"])
|
| 72 |
-
question_input = gr.Textbox(label="Enter your question about the document")
|
| 73 |
-
answer_output = gr.Textbox(label="Answer", interactive=False)
|
| 74 |
-
docqa_btn = gr.Button("Get Answer")
|
| 75 |
-
|
| 76 |
-
def doc_qa(pdf_file, question):
|
| 77 |
-
if pdf_file is None:
|
| 78 |
-
return "Please upload a PDF file."
|
| 79 |
-
if not question.strip():
|
| 80 |
-
return "Please enter a question."
|
| 81 |
-
return self.agent.tools[4](pdf_file.name, question)
|
| 82 |
-
|
| 83 |
-
docqa_btn.click(doc_qa, inputs=[pdf_upload, question_input], outputs=answer_output)
|
| 84 |
-
|
| 85 |
-
demo.launch()
|
|
|
|
| 2 |
|
| 3 |
class GradioUI:
|
| 4 |
def __init__(self, agent):
|
| 5 |
+
# The agent object passed from app.py
|
| 6 |
self.agent = agent
|
| 7 |
|
| 8 |
+
def process_query_with_agent(self, query):
|
| 9 |
+
"""
|
| 10 |
+
Processes the user query by passing it to the agent's run method.
|
| 11 |
+
The agent will then decide which tool(s) to use based on the query.
|
| 12 |
+
"""
|
| 13 |
+
# Check if the query is empty or only whitespace
|
| 14 |
+
if not query or not query.strip():
|
| 15 |
+
return "Please enter your request for the agent."
|
| 16 |
+
|
| 17 |
+
print(f"User query received by Gradio UI: {query}")
|
| 18 |
+
try:
|
| 19 |
+
# Call the agent's run method with the user's query.
|
| 20 |
+
# The agent internally figures out tool selection and execution.
|
| 21 |
+
response = self.agent.run(query)
|
| 22 |
+
print(f"Agent execution finished. Response generated.")
|
| 23 |
+
return response
|
| 24 |
+
except Exception as e:
|
| 25 |
+
# Catch any exceptions during the agent's run
|
| 26 |
+
print(f"Error during agent execution: {e}")
|
| 27 |
+
return f"An error occurred while processing your request: {str(e)}"
|
| 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 below. The agent will use its available tools "
|
| 38 |
+
"(Weather, Time, Search, Image Generation, Document Q&A, etc.) "
|
| 39 |
+
"to understand and fulfill your request."
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
# Single input field for the user's natural language request
|
| 43 |
+
query_input = gr.Textbox(
|
| 44 |
+
label="Enter your request here:",
|
| 45 |
+
placeholder="e.g., What is the weather in London? Search for the latest news about AI. Tell me the time in New York. Generate an image of a cat wearing a hat. What does this document say about [topic]? (For Document Q&A, ensure the PDF is handled by your app.py's tool setup if needed)",
|
| 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:",
|
| 53 |
+
interactive=False, # User cannot type in the output box
|
| 54 |
+
lines=10, # Provides more space to display potentially long responses
|
| 55 |
+
autoscroll=True # Automatically scrolls to the latest output
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
# Button to trigger the agent when clicked
|
| 59 |
+
submit_btn = gr.Button("Submit to Agent")
|
| 60 |
+
|
| 61 |
+
# Link the button click event to the process_query_with_agent function
|
| 62 |
+
# The value from query_input will be passed as the 'query' argument.
|
| 63 |
+
# The return value of the function will be displayed in agent_output.
|
| 64 |
+
submit_btn.click(
|
| 65 |
+
fn=self.process_query_with_agent,
|
| 66 |
+
inputs=query_input,
|
| 67 |
+
outputs=agent_output
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
# Optional: Add examples to show users what they can ask
|
| 71 |
+
# You would need to add example queries relevant to your tools
|
| 72 |
+
# examples = [
|
| 73 |
+
# ["What is the weather in Tokyo?"],
|
| 74 |
+
# ["Tell me the current time in Paris."],
|
| 75 |
+
# ["Generate an image of a space dog."],
|
| 76 |
+
# ["Search for the history of the internet."],
|
| 77 |
+
# # Add an example for document Q&A if you have a default document loaded
|
| 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
|
| 84 |
+
# Setting share=True creates a public URL (useful for demos, be mindful of security/costs)
|
| 85 |
+
# Setting inline=False opens the app in a new browser tab
|
| 86 |
+
demo.launch(share=False, inline=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|