Spaces:
Sleeping
Sleeping
Update Gradio_UI.py
Browse files- 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 |
-
#
|
| 15 |
-
tools =
|
| 16 |
|
| 17 |
-
for tool in tools
|
| 18 |
-
|
| 19 |
-
|
|
|
|
| 20 |
self.get_time_tool = tool
|
| 21 |
-
elif
|
| 22 |
self.get_weather_tool = tool
|
| 23 |
-
elif
|
| 24 |
self.doc_qa_tool = tool
|
| 25 |
-
elif "text-to-image" in str(tool):
|
| 26 |
self.image_gen_tool = tool
|
| 27 |
-
elif "
|
| 28 |
self.search_tool = tool
|
| 29 |
|
| 30 |
-
#
|
| 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:
|
| 36 |
|
| 37 |
def launch(self):
|
| 38 |
with gr.Blocks() as demo:
|
| 39 |
-
gr.Markdown("#
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 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 |
-
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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|