SamarthPujari commited on
Commit
2ceb868
·
verified ·
1 Parent(s): 97b77ac

Update Gradio_UI.py

Browse files
Files changed (1) hide show
  1. 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("Use the tabs below to interact with different tools:")
11
-
12
- with gr.Tabs():
13
-
14
- # Tab 1: Weather
15
- with gr.TabItem("Weather"):
16
- place_input = gr.Textbox(label="Enter place name (e.g., London)", placeholder="City or location")
17
- weather_output = gr.Textbox(label="Current Weather", interactive=False)
18
- get_weather_btn = gr.Button("Get Weather")
19
-
20
- def get_weather(place):
21
- if not place.strip():
22
- return "Please enter a valid place name."
23
- return self.agent.tools[1](place)
24
-
25
- get_weather_btn.click(get_weather, inputs=place_input, outputs=weather_output)
26
-
27
- # Tab 2: Local Time
28
- with gr.TabItem("Local Time"):
29
- timezone_input = gr.Textbox(label="Enter timezone (e.g., America/New_York)", placeholder="Timezone string")
30
- time_output = gr.Textbox(label="Current Time", interactive=False)
31
- get_time_btn = gr.Button("Get Local Time")
32
-
33
- def get_time(tz):
34
- if not tz.strip():
35
- return "Please enter a valid timezone."
36
- return self.agent.tools[0](tz)
37
-
38
- get_time_btn.click(get_time, inputs=timezone_input, outputs=time_output)
39
-
40
- # Tab 3: Image Generation
41
- with gr.TabItem("Image Generation"):
42
- image_prompt = gr.Textbox(label="Enter image description prompt", lines=2)
43
- image_output = gr.Image(label="Generated Image")
44
- gen_image_btn = gr.Button("Generate Image")
45
-
46
- def gen_image(prompt):
47
- if not prompt.strip():
48
- return None
49
- # The image generation tool might return an image URL or PIL.Image.
50
- # Adjust this depending on your tool's output format.
51
- result = self.agent.tools[2](prompt)
52
- return result
53
-
54
- gen_image_btn.click(gen_image, inputs=image_prompt, outputs=image_output)
55
-
56
- # Tab 4: Web Search
57
- with gr.TabItem("Web Search"):
58
- search_query = gr.Textbox(label="Enter search query")
59
- search_output = gr.Textbox(label="Search Results", interactive=False)
60
- search_btn = gr.Button("Search")
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)