SamarthPujari commited on
Commit
8b5ae06
·
verified ·
1 Parent(s): 6858d1d

Update Gradio_UI.py

Browse files
Files changed (1) hide show
  1. Gradio_UI.py +51 -23
Gradio_UI.py CHANGED
@@ -4,41 +4,72 @@ 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")
@@ -46,30 +77,27 @@ class GradioUI:
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
 
@@ -78,8 +106,8 @@ class GradioUI:
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()
 
4
  def __init__(self, agent):
5
  self.agent = agent
6
 
7
+ # Initialize tool references
8
+ self.get_time_tool = None
9
+ self.get_weather_tool = None
10
+ self.image_gen_tool = None
11
+ self.search_tool = None
12
+ self.doc_qa_tool = None
13
+
14
+ # Map tools by known types or names
15
+ for tool in self.agent.tools:
16
+ try:
17
+ tool_name = getattr(tool, "__name__", None)
18
+ if tool_name == "get_current_time_in_timezone":
19
+ self.get_time_tool = tool
20
+ elif tool_name == "get_current_weather":
21
+ self.get_weather_tool = tool
22
+ elif tool_name == "document_qna_tool":
23
+ self.doc_qa_tool = tool
24
+ elif "text-to-image" in str(tool):
25
+ self.image_gen_tool = tool
26
+ elif "DuckDuckGoSearchTool" in str(tool):
27
+ self.search_tool = tool
28
+ except Exception:
29
+ pass
30
+
31
+ # Fallback in case mapping fails
32
+ self.get_time_tool = self.get_time_tool or self.agent.tools[0]
33
+ self.get_weather_tool = self.get_weather_tool or self.agent.tools[1]
34
+ self.image_gen_tool = self.image_gen_tool or self.agent.tools[2]
35
+ self.search_tool = self.search_tool or self.agent.tools[3]
36
+ self.doc_qa_tool = self.doc_qa_tool or self.agent.tools[4]
37
+
38
  def launch(self):
39
  with gr.Blocks() as demo:
40
+ gr.Markdown("# 🤖 Multi-Tool AI Agent")
41
+ gr.Markdown("Use the tabs below to interact with the different tools:")
42
 
43
  with gr.Tabs():
44
 
45
  # Tab 1: Weather
46
+ with gr.TabItem("🌦️ Weather"):
47
+ place_input = gr.Textbox(label="Enter place name (e.g., London)")
48
+ weather_output = gr.Textbox(label="Weather Info", interactive=False)
49
  get_weather_btn = gr.Button("Get Weather")
50
 
51
  def get_weather(place):
52
  if not place.strip():
53
  return "Please enter a valid place name."
54
+ return self.get_weather_tool(place)
55
 
56
  get_weather_btn.click(get_weather, inputs=place_input, outputs=weather_output)
57
 
58
  # Tab 2: Local Time
59
+ with gr.TabItem("🕒 Local Time"):
60
+ timezone_input = gr.Textbox(label="Enter timezone (e.g., America/New_York)")
61
+ time_output = gr.Textbox(label="Local Time", interactive=False)
62
  get_time_btn = gr.Button("Get Local Time")
63
 
64
  def get_time(tz):
65
  if not tz.strip():
66
  return "Please enter a valid timezone."
67
+ return self.get_time_tool(tz)
68
 
69
  get_time_btn.click(get_time, inputs=timezone_input, outputs=time_output)
70
 
71
  # Tab 3: Image Generation
72
+ with gr.TabItem("🎨 Image Generation"):
73
  image_prompt = gr.Textbox(label="Enter image description prompt", lines=2)
74
  image_output = gr.Image(label="Generated Image")
75
  gen_image_btn = gr.Button("Generate Image")
 
77
  def gen_image(prompt):
78
  if not prompt.strip():
79
  return None
80
+ return self.image_gen_tool(prompt)
 
 
 
81
 
82
  gen_image_btn.click(gen_image, inputs=image_prompt, outputs=image_output)
83
 
84
  # Tab 4: Web Search
85
+ with gr.TabItem("🔍 Web Search"):
86
  search_query = gr.Textbox(label="Enter search query")
87
+ search_output = gr.Textbox(label="Search Result", interactive=False)
88
  search_btn = gr.Button("Search")
89
 
90
  def search(q):
91
  if not q.strip():
92
  return "Please enter a search query."
93
+ return self.search_tool(q)
94
 
95
  search_btn.click(search, inputs=search_query, outputs=search_output)
96
 
97
  # Tab 5: Document Q&A
98
+ with gr.TabItem("📄 Document Q&A"):
99
+ pdf_upload = gr.File(label="Upload PDF", file_types=[".pdf"])
100
+ question_input = gr.Textbox(label="Ask a question about the document")
101
  answer_output = gr.Textbox(label="Answer", interactive=False)
102
  docqa_btn = gr.Button("Get Answer")
103
 
 
106
  return "Please upload a PDF file."
107
  if not question.strip():
108
  return "Please enter a question."
109
+ return self.doc_qa_tool(pdf_file.name, question)
110
 
111
  docqa_btn.click(doc_qa, inputs=[pdf_upload, question_input], outputs=answer_output)
112
 
113
+ demo.launch()