Spaces:
Runtime error
Runtime error
Update Gradio_UI.py
Browse files- Gradio_UI.py +82 -4
Gradio_UI.py
CHANGED
|
@@ -265,15 +265,16 @@ class GradioUI:
|
|
| 265 |
stored_messages = gr.State([])
|
| 266 |
file_uploads_log = gr.State([])
|
| 267 |
chatbot = gr.Chatbot(
|
| 268 |
-
label="
|
| 269 |
type="messages",
|
| 270 |
avatar_images=(
|
| 271 |
None,
|
| 272 |
"https://huggingface.co/datasets/agents-course/course-images/resolve/main/en/communication/Alfred.png",
|
| 273 |
),
|
| 274 |
-
|
| 275 |
-
|
| 276 |
)
|
|
|
|
| 277 |
# If an upload folder is provided, enable the upload feature
|
| 278 |
if self.file_upload_folder is not None:
|
| 279 |
upload_file = gr.File(label="Upload a file")
|
|
@@ -283,14 +284,91 @@ class GradioUI:
|
|
| 283 |
[upload_file, file_uploads_log],
|
| 284 |
[upload_status, file_uploads_log],
|
| 285 |
)
|
| 286 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 287 |
text_input.submit(
|
| 288 |
self.log_user_message,
|
| 289 |
[text_input, file_uploads_log],
|
| 290 |
[stored_messages, text_input],
|
| 291 |
).then(self.interact_with_agent, [stored_messages, chatbot], [chatbot])
|
| 292 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 293 |
demo.launch(debug=True, share=False, **kwargs)
|
| 294 |
|
| 295 |
|
|
|
|
| 296 |
__all__ = ["stream_to_gradio", "GradioUI"]
|
|
|
|
| 265 |
stored_messages = gr.State([])
|
| 266 |
file_uploads_log = gr.State([])
|
| 267 |
chatbot = gr.Chatbot(
|
| 268 |
+
label="๐ฌ Chat Window",
|
| 269 |
type="messages",
|
| 270 |
avatar_images=(
|
| 271 |
None,
|
| 272 |
"https://huggingface.co/datasets/agents-course/course-images/resolve/main/en/communication/Alfred.png",
|
| 273 |
),
|
| 274 |
+
scale=5, # Expands chat window
|
| 275 |
+
show_copy_button=True
|
| 276 |
)
|
| 277 |
+
|
| 278 |
# If an upload folder is provided, enable the upload feature
|
| 279 |
if self.file_upload_folder is not None:
|
| 280 |
upload_file = gr.File(label="Upload a file")
|
|
|
|
| 284 |
[upload_file, file_uploads_log],
|
| 285 |
[upload_status, file_uploads_log],
|
| 286 |
)
|
| 287 |
+
|
| 288 |
+
# ====== Chat Input with Placeholder Text & Enter Key Submission ====== #
|
| 289 |
+
text_input = gr.Textbox(
|
| 290 |
+
lines=1,
|
| 291 |
+
label="๐ฌ Chat Message",
|
| 292 |
+
placeholder="Type your message and press Enter to send...",
|
| 293 |
+
interactive=True,
|
| 294 |
+
)
|
| 295 |
+
|
| 296 |
+
# Submit text when "Enter" is pressed
|
| 297 |
text_input.submit(
|
| 298 |
self.log_user_message,
|
| 299 |
[text_input, file_uploads_log],
|
| 300 |
[stored_messages, text_input],
|
| 301 |
).then(self.interact_with_agent, [stored_messages, chatbot], [chatbot])
|
| 302 |
|
| 303 |
+
# ====== Sample Prompt Buttons Inside the Chat Window ====== #
|
| 304 |
+
with gr.Column(visible=True) as startup_buttons:
|
| 305 |
+
gr.Markdown("### ๐ค Get Started with a Quick Prompt")
|
| 306 |
+
with gr.Row():
|
| 307 |
+
btn1 = gr.Button("โ๏ธ Plan a Trip")
|
| 308 |
+
btn2 = gr.Button("๐ฝ๏ธ Find a Restaurant")
|
| 309 |
+
btn3 = gr.Button("๐ฑ Convert Currency")
|
| 310 |
+
|
| 311 |
+
# When a button is clicked, fill input box and hide buttons
|
| 312 |
+
def set_prompt(prompt):
|
| 313 |
+
return prompt, gr.update(visible=False) # Hide buttons
|
| 314 |
+
|
| 315 |
+
btn1.click(lambda: set_prompt("I want to plan a trip from Toronto to Paris in May 2025. Can you find flights and suggest an itinerary?"),
|
| 316 |
+
inputs=[], outputs=[text_input, startup_buttons])
|
| 317 |
+
btn2.click(lambda: set_prompt("I'm visiting New York next weekend. Can you recommend some top-rated restaurants for dinner?"),
|
| 318 |
+
inputs=[], outputs=[text_input, startup_buttons])
|
| 319 |
+
btn3.click(lambda: set_prompt("Convert 500 USD to EUR and show me the exchange rate."),
|
| 320 |
+
inputs=[], outputs=[text_input, startup_buttons])
|
| 321 |
+
|
| 322 |
+
# ====== Additional Sample Prompts (Dropdown or Grid) ====== #
|
| 323 |
+
with gr.Accordion("๐ Default Prompts", open=False):
|
| 324 |
+
with gr.Row():
|
| 325 |
+
pbtn1 = gr.Button("๐จ Find Hotels in Tokyo")
|
| 326 |
+
pbtn2 = gr.Button("๐ฆ๏ธ Check Weather in Paris")
|
| 327 |
+
pbtn3 = gr.Button("๐ Find Car Rentals")
|
| 328 |
+
pbtn4 = gr.Button("๐๏ธ Get Event Info")
|
| 329 |
+
|
| 330 |
+
# Button Clicks - Additional Prompts
|
| 331 |
+
pbtn1.click(lambda: "I need budget-friendly hotels in Tokyo for 5 nights. My budget is $100 per night.", inputs=[], outputs=text_input)
|
| 332 |
+
pbtn2.click(lambda: "What is the weather forecast for Paris next week?", inputs=[], outputs=text_input)
|
| 333 |
+
pbtn3.click(lambda: "Find me a car rental in Los Angeles for 3 days.", inputs=[], outputs=text_input)
|
| 334 |
+
pbtn4.click(lambda: "What major events are happening in Berlin this month?", inputs=[], outputs=text_input)
|
| 335 |
+
|
| 336 |
+
# ====== Toggle Checkboxes for Extra Info ====== #
|
| 337 |
+
with gr.Row():
|
| 338 |
+
show_smolagents_info = gr.Checkbox(label="What is SmolAgents?", value=False)
|
| 339 |
+
show_agent_capabilities = gr.Checkbox(label="Capabilities & Limitations", value=False)
|
| 340 |
+
|
| 341 |
+
# Expanding sections based on checkbox state
|
| 342 |
+
smolagents_info = gr.Markdown("""
|
| 343 |
+
## ๐ **What is SmolAgents?**
|
| 344 |
+
Unlike a basic chat AI that provides single-step answers, **SmolAgents** enable dynamic, multi-step **reasoning and decision-making**.
|
| 345 |
+
|
| 346 |
+
- ๐ **Modular & Flexible:** SmolAgents **call different tools** when needed, giving you **step-by-step insights** rather than just final answers.
|
| 347 |
+
- ๐ค **Real-time Adaptability:** The AI **adjusts its approach based on intermediate results**, making it **more powerful than standard chatbots**.
|
| 348 |
+
- ๐ **Build Quickly & Expand Easily:** SmolAgents provide a foundation for building **custom AI-powered workflows**, making it easy to **scale and adapt**.
|
| 349 |
+
|
| 350 |
+
๐น *Use this space to explore whatโs possible! This AI isn't just answeringโyou can see how it reasons between steps and improves results.*
|
| 351 |
+
""", visible=False)
|
| 352 |
+
|
| 353 |
+
agent_capabilities = gr.Markdown("""
|
| 354 |
+
## ๐ค **Capabilities & Limitations**
|
| 355 |
+
|
| 356 |
+
| **Tool Name** | **Function & API Used** | **Limitations** | **Possible Improvements** |
|
| 357 |
+
|--------------------------------------|----------------------------------------------------------------|----------------|---------------------------|
|
| 358 |
+
| `search_flights(departure, destination, date)` โ๏ธ | **Finds flights** using DuckDuckGo. Queries available flights from departure to destination on a given date. | May not have real-time pricing | Integrate a flight API like Skyscanner |
|
| 359 |
+
| `web_search(query)` ๐ | **Searches the web** using DuckDuckGo. Retrieves live data including news, events, and real-time updates. | Limited search results | Expand to use multiple search engines |
|
| 360 |
+
| `visit_webpage(url)` ๐ | **Scrapes web content**. Extracts ingredients and instructions from a recipe URL. | May not work on all websites | Add more scraping logic for better accuracy |
|
| 361 |
+
| `convert_currency(amount, from_currency, to_currency)` ๐ฑ | **Converts currency** via FreeCurrencyAPI. Fetches exchange rates and calculates conversions. | Requires API key, may have rate limits | Use a backup API in case of failures |
|
| 362 |
+
| `get_current_time_in_timezone(timezone)` โฐ | **Gets local time** in a specified timezone using the `pytz` library. | No offline functionality | Allow local fallback for time retrieval |
|
| 363 |
+
| `chat_with_ai(message)` ๐ฌ | **Maintains conversation memory**. Stores and retrieves chat history to provide context-aware responses. | Limited to 16,000 tokens | Implement memory chunking for longer conversations |
|
| 364 |
+
""", visible=False)
|
| 365 |
+
|
| 366 |
+
# Show/Hide sections based on checkbox state
|
| 367 |
+
show_smolagents_info.change(lambda x: gr.update(visible=x), show_smolagents_info, smolagents_info)
|
| 368 |
+
show_agent_capabilities.change(lambda x: gr.update(visible=x), show_agent_capabilities, agent_capabilities)
|
| 369 |
+
|
| 370 |
demo.launch(debug=True, share=False, **kwargs)
|
| 371 |
|
| 372 |
|
| 373 |
+
|
| 374 |
__all__ = ["stream_to_gradio", "GradioUI"]
|