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

Update Gradio_UI.py

Browse files
Files changed (1) hide show
  1. Gradio_UI.py +77 -94
Gradio_UI.py CHANGED
@@ -4,99 +4,82 @@ class GradioUI:
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
- # 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
- # Get raw function name if possible
19
- raw_func = getattr(tool, "fn", None)
20
- raw_func_name = getattr(raw_func, "__name__", "").lower()
21
- tool_str = str(tool).lower()
22
-
23
- if "timezone" in raw_func_name or "get_current_time_in_timezone" in raw_func_name:
24
- self.get_time_tool = tool
25
- elif "weather" in raw_func_name or "get_current_weather" in raw_func_name:
26
- self.get_weather_tool = tool
27
- elif "document_qna_tool" in raw_func_name:
28
- self.doc_qa_tool = tool
29
- elif "text-to-image" in tool_str or "image" in raw_func_name:
30
- self.image_gen_tool = tool
31
- elif "duckduckgo" in tool_str or "search" in raw_func_name:
32
- self.search_tool = tool
33
-
34
- # Validate all tools are loaded
35
- assert self.get_time_tool, "Missing: get_current_time_in_timezone"
36
- assert self.get_weather_tool, "Missing: get_current_weather"
37
- assert self.doc_qa_tool, "Missing: document_qna_tool"
38
- assert self.image_gen_tool, "Missing: image generation tool"
39
- assert self.search_tool, "Missing: DuckDuckGoSearchTool"
40
-
41
  def launch(self):
42
  with gr.Blocks() as demo:
43
- gr.Markdown("# 🧠 Document Q&A + AI Agent Interface")
44
-
45
- with gr.Tab("📄 Document Q&A"):
46
- pdf_input = gr.File(label="Upload PDF", file_types=[".pdf"])
47
- question_input = gr.Textbox(label="Ask a question about the PDF")
48
- docqa_output = gr.Textbox(label="Answer")
49
- docqa_btn = gr.Button("Get Answer")
50
-
51
- with gr.Tab("🌦️ Weather"):
52
- place_input = gr.Textbox(label="Enter a city")
53
- weather_output = gr.Textbox(label="Weather Info")
54
- weather_btn = gr.Button("Get Weather")
55
-
56
- with gr.Tab("🕓 Time"):
57
- timezone_input = gr.Textbox(label="Enter timezone (e.g., 'Asia/Kolkata')")
58
- time_output = gr.Textbox(label="Current Time")
59
- time_btn = gr.Button("Get Time")
60
-
61
- with gr.Tab("🖼️ Image Generator"):
62
- prompt_input = gr.Textbox(label="Enter image prompt")
63
- image_output = gr.Image(label="Generated Image")
64
- image_btn = gr.Button("Generate Image")
65
-
66
- with gr.Tab("🔍 Web Search"):
67
- search_input = gr.Textbox(label="Enter search query")
68
- search_output = gr.Textbox(label="Search Result")
69
- search_btn = gr.Button("Search")
70
-
71
- # Actions
72
- docqa_btn.click(
73
- fn=lambda pdf, q: self.doc_qa_tool(pdf.name, q),
74
- inputs=[pdf_input, question_input],
75
- outputs=docqa_output
76
- )
77
-
78
- weather_btn.click(
79
- fn=lambda place: self.get_weather_tool(place),
80
- inputs=place_input,
81
- outputs=weather_output
82
- )
83
-
84
- time_btn.click(
85
- fn=lambda tz: self.get_time_tool(tz),
86
- inputs=timezone_input,
87
- outputs=time_output
88
- )
89
-
90
- image_btn.click(
91
- fn=lambda prompt: self.image_gen_tool(prompt),
92
- inputs=prompt_input,
93
- outputs=image_output
94
- )
95
-
96
- search_btn.click(
97
- fn=lambda query: self.search_tool(query),
98
- inputs=search_input,
99
- outputs=search_output
100
- )
101
-
102
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()