SamarthPujari commited on
Commit
41a8e13
·
verified ·
1 Parent(s): 8796502

Update Gradio_UI.py

Browse files
Files changed (1) hide show
  1. Gradio_UI.py +72 -85
Gradio_UI.py CHANGED
@@ -11,102 +11,89 @@ class GradioUI:
11
  self.search_tool = None
12
  self.doc_qa_tool = None
13
 
14
- # Try to detect tool types from dictionary-like access
15
- tools = getattr(agent, "tools", {})
16
 
17
- for tool in tools.values(): # ✅ Use `.values()` instead of indexing
18
- tool_name = getattr(tool, "__name__", None)
19
- if tool_name == "get_current_time_in_timezone":
 
20
  self.get_time_tool = tool
21
- elif tool_name == "get_current_weather":
22
  self.get_weather_tool = tool
23
- elif tool_name == "document_qna_tool":
24
  self.doc_qa_tool = tool
25
- elif "text-to-image" in str(tool):
26
  self.image_gen_tool = tool
27
- elif "DuckDuckGoSearchTool" in str(tool):
28
  self.search_tool = tool
29
 
30
- # Optional fallback with warnings
31
  assert self.get_time_tool, "Missing: get_current_time_in_timezone"
32
  assert self.get_weather_tool, "Missing: get_current_weather"
33
  assert self.doc_qa_tool, "Missing: document_qna_tool"
34
  assert self.image_gen_tool, "Missing: image generation tool"
35
- assert self.search_tool, "Missing: search tool"
36
 
37
  def launch(self):
38
  with gr.Blocks() as demo:
39
- gr.Markdown("# 🤖 Multi-Tool AI Agent")
40
- gr.Markdown("Use the tabs below to interact with the different tools:")
41
-
42
- with gr.Tabs():
43
-
44
- # Tab 1: Weather
45
- with gr.TabItem("🌦️ Weather"):
46
- place_input = gr.Textbox(label="Enter place name (e.g., London)")
47
- weather_output = gr.Textbox(label="Weather Info", interactive=False)
48
- get_weather_btn = gr.Button("Get Weather")
49
-
50
- def get_weather(place):
51
- if not place.strip():
52
- return "Please enter a valid place name."
53
- return self.get_weather_tool(place)
54
-
55
- get_weather_btn.click(get_weather, inputs=place_input, outputs=weather_output)
56
-
57
- # Tab 2: Local Time
58
- with gr.TabItem("🕒 Local Time"):
59
- timezone_input = gr.Textbox(label="Enter timezone (e.g., America/New_York)")
60
- time_output = gr.Textbox(label="Local Time", interactive=False)
61
- get_time_btn = gr.Button("Get Local Time")
62
-
63
- def get_time(tz):
64
- if not tz.strip():
65
- return "Please enter a valid timezone."
66
- return self.get_time_tool(tz)
67
-
68
- get_time_btn.click(get_time, inputs=timezone_input, outputs=time_output)
69
-
70
- # Tab 3: Image Generation
71
- with gr.TabItem("🎨 Image Generation"):
72
- image_prompt = gr.Textbox(label="Enter image description prompt", lines=2)
73
- image_output = gr.Image(label="Generated Image")
74
- gen_image_btn = gr.Button("Generate Image")
75
-
76
- def gen_image(prompt):
77
- if not prompt.strip():
78
- return None
79
- return self.image_gen_tool(prompt)
80
-
81
- gen_image_btn.click(gen_image, inputs=image_prompt, outputs=image_output)
82
-
83
- # Tab 4: Web Search
84
- with gr.TabItem("🔍 Web Search"):
85
- search_query = gr.Textbox(label="Enter search query")
86
- search_output = gr.Textbox(label="Search Result", interactive=False)
87
- search_btn = gr.Button("Search")
88
-
89
- def search(q):
90
- if not q.strip():
91
- return "Please enter a search query."
92
- return self.search_tool(q)
93
-
94
- search_btn.click(search, inputs=search_query, outputs=search_output)
95
-
96
- # Tab 5: Document Q&A
97
- with gr.TabItem("📄 Document Q&A"):
98
- pdf_upload = gr.File(label="Upload PDF", file_types=[".pdf"])
99
- question_input = gr.Textbox(label="Ask a question about the document")
100
- answer_output = gr.Textbox(label="Answer", interactive=False)
101
- docqa_btn = gr.Button("Get Answer")
102
-
103
- def doc_qa(pdf_file, question):
104
- if pdf_file is None:
105
- return "Please upload a PDF file."
106
- if not question.strip():
107
- return "Please enter a question."
108
- return self.doc_qa_tool(pdf_file.name, question)
109
-
110
- docqa_btn.click(doc_qa, inputs=[pdf_upload, question_input], outputs=answer_output)
111
-
112
- demo.launch()
 
11
  self.search_tool = None
12
  self.doc_qa_tool = None
13
 
14
+ # Ensure agent.tools is iterable
15
+ tools = agent.tools if isinstance(agent.tools, (list, tuple)) else list(agent.tools)
16
 
17
+ for tool in tools:
18
+ # Match tools based on description (from docstring) or fallback string
19
+ desc = getattr(tool, "description", "") or str(tool)
20
+ if "current local time" in desc or "timezone" in desc:
21
  self.get_time_tool = tool
22
+ elif "weather" in desc.lower():
23
  self.get_weather_tool = tool
24
+ elif "question" in desc.lower() and "pdf" in desc.lower():
25
  self.doc_qa_tool = tool
26
+ elif "text-to-image" in desc.lower() or "image" in str(tool).lower():
27
  self.image_gen_tool = tool
28
+ elif "duckduckgo" in str(tool).lower() or "search" in desc.lower():
29
  self.search_tool = tool
30
 
31
+ # Validate all tools are loaded
32
  assert self.get_time_tool, "Missing: get_current_time_in_timezone"
33
  assert self.get_weather_tool, "Missing: get_current_weather"
34
  assert self.doc_qa_tool, "Missing: document_qna_tool"
35
  assert self.image_gen_tool, "Missing: image generation tool"
36
+ assert self.search_tool, "Missing: DuckDuckGoSearchTool"
37
 
38
  def launch(self):
39
  with gr.Blocks() as demo:
40
+ gr.Markdown("# 🧠 Document Q&A + AI Agent Interface")
41
+
42
+ with gr.Tab("📄 Document Q&A"):
43
+ pdf_input = gr.File(label="Upload PDF", file_types=[".pdf"])
44
+ question_input = gr.Textbox(label="Ask a question about the PDF")
45
+ docqa_output = gr.Textbox(label="Answer")
46
+ docqa_btn = gr.Button("Get Answer")
47
+
48
+ with gr.Tab("🌦️ Weather"):
49
+ place_input = gr.Textbox(label="Enter a city")
50
+ weather_output = gr.Textbox(label="Weather Info")
51
+ weather_btn = gr.Button("Get Weather")
52
+
53
+ with gr.Tab("🕓 Time"):
54
+ timezone_input = gr.Textbox(label="Enter timezone (e.g., 'Asia/Kolkata')")
55
+ time_output = gr.Textbox(label="Current Time")
56
+ time_btn = gr.Button("Get Time")
57
+
58
+ with gr.Tab("🖼️ Image Generator"):
59
+ prompt_input = gr.Textbox(label="Enter image prompt")
60
+ image_output = gr.Image(label="Generated Image")
61
+ image_btn = gr.Button("Generate Image")
62
+
63
+ with gr.Tab("🔍 Web Search"):
64
+ search_input = gr.Textbox(label="Enter search query")
65
+ search_output = gr.Textbox(label="Search Result")
66
+ search_btn = gr.Button("Search")
67
+
68
+ # Actions
69
+ docqa_btn.click(
70
+ fn=lambda pdf, q: self.doc_qa_tool(pdf.name, q),
71
+ inputs=[pdf_input, question_input],
72
+ outputs=docqa_output
73
+ )
74
+
75
+ weather_btn.click(
76
+ fn=lambda place: self.get_weather_tool(place),
77
+ inputs=place_input,
78
+ outputs=weather_output
79
+ )
80
+
81
+ time_btn.click(
82
+ fn=lambda tz: self.get_time_tool(tz),
83
+ inputs=timezone_input,
84
+ outputs=time_output
85
+ )
86
+
87
+ image_btn.click(
88
+ fn=lambda prompt: self.image_gen_tool(prompt),
89
+ inputs=prompt_input,
90
+ outputs=image_output
91
+ )
92
+
93
+ search_btn.click(
94
+ fn=lambda query: self.search_tool(query),
95
+ inputs=search_input,
96
+ outputs=search_output
97
+ )
98
+
99
+ demo.launch()