Spaces:
Sleeping
Sleeping
Update Gradio_UI.py
Browse files- 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("#
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|