Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,11 +3,17 @@ import datetime
|
|
| 3 |
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
|
|
|
|
|
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
|
|
|
|
|
|
| 7 |
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
|
|
|
| 10 |
# 1 - Timezone tool
|
|
|
|
| 11 |
@tool
|
| 12 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 13 |
"""A tool that fetches the current local time in a specified timezone.
|
|
@@ -28,7 +34,7 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 28 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 29 |
|
| 30 |
|
| 31 |
-
# 2
|
| 32 |
@tool
|
| 33 |
def generate_image_from_text(prompt: str) -> str:
|
| 34 |
"""A tool that generates an image based on a text description.
|
|
@@ -46,9 +52,10 @@ def generate_image_from_text(prompt: str) -> str:
|
|
| 46 |
# Handle any errors that occur during image generation
|
| 47 |
return f"Error generating image: {str(e)}"
|
| 48 |
|
| 49 |
-
|
| 50 |
-
#
|
| 51 |
-
|
|
|
|
| 52 |
|
| 53 |
@tool
|
| 54 |
def search_web(query: str) -> str:
|
|
@@ -58,7 +65,7 @@ def search_web(query: str) -> str:
|
|
| 58 |
"""
|
| 59 |
try:
|
| 60 |
# Execute the search query using DuckDuckGo
|
| 61 |
-
search_results =
|
| 62 |
|
| 63 |
# Format and return the search results
|
| 64 |
return f"Search results for '{query}':\n\n{search_results}"
|
|
@@ -66,12 +73,38 @@ def search_web(query: str) -> str:
|
|
| 66 |
# Handle any errors that occur during the search
|
| 67 |
return f"Error searching the web: {str(e)}"
|
| 68 |
|
| 69 |
-
|
| 70 |
-
#
|
| 71 |
final_answer = FinalAnswerTool()
|
| 72 |
|
| 73 |
|
| 74 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
# If the agent does not answer, the model is overloaded
|
| 76 |
# Alternative endpoint: 'https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 77 |
model = HfApiModel(
|
|
@@ -80,26 +113,34 @@ model = HfApiModel(
|
|
| 80 |
model_id='Qwen/Qwen2.5-Coder-32B-Instruct', # Using Qwen 2.5 Coder model
|
| 81 |
custom_role_conversions=None,
|
| 82 |
)
|
|
|
|
| 83 |
|
| 84 |
-
# Load External Tools
|
| 85 |
-
# Import the image generation tool from Hugging Face Hub
|
| 86 |
-
# This tool will be used by the generate_image_from_text function
|
| 87 |
-
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
-
# Load Prompt Templates
|
| 91 |
# Load prompt templates from YAML file for consistent agent responses
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
|
| 96 |
-
#
|
|
|
|
| 97 |
agent = CodeAgent(
|
| 98 |
model=model,
|
| 99 |
tools=[
|
| 100 |
get_current_time_in_timezone, # Tool 1: Time zone tool
|
| 101 |
generate_image_from_text, # Tool 2: Image generation tool
|
| 102 |
search_web, # Tool 3: Web search tool
|
|
|
|
| 103 |
final_answer # Required final answer tool
|
| 104 |
],
|
| 105 |
max_steps=6, # Maximum number of reasoning steps
|
|
@@ -110,7 +151,9 @@ agent = CodeAgent(
|
|
| 110 |
description=None, # No custom agent description
|
| 111 |
prompt_templates=prompt_templates # Using loaded prompt templates
|
| 112 |
)
|
|
|
|
| 113 |
|
| 114 |
-
#
|
| 115 |
-
|
| 116 |
-
|
|
|
|
|
|
| 3 |
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
+
import os
|
| 7 |
+
import sys
|
| 8 |
from tools.final_answer import FinalAnswerTool
|
| 9 |
+
from tools.web_search import DuckDuckGoSearchTool
|
| 10 |
+
from tools.visit_webpage import VisitWebpageTool
|
| 11 |
|
| 12 |
from Gradio_UI import GradioUI
|
| 13 |
|
| 14 |
+
|
| 15 |
# 1 - Timezone tool
|
| 16 |
+
|
| 17 |
@tool
|
| 18 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 19 |
"""A tool that fetches the current local time in a specified timezone.
|
|
|
|
| 34 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 35 |
|
| 36 |
|
| 37 |
+
# 2- Image generation
|
| 38 |
@tool
|
| 39 |
def generate_image_from_text(prompt: str) -> str:
|
| 40 |
"""A tool that generates an image based on a text description.
|
|
|
|
| 52 |
# Handle any errors that occur during image generation
|
| 53 |
return f"Error generating image: {str(e)}"
|
| 54 |
|
| 55 |
+
|
| 56 |
+
# 3 - WEB SEARCH TOOL - Using the existing DuckDuckGoSearchTool class
|
| 57 |
+
# Initialize the DuckDuckGo search tool from the tools directory
|
| 58 |
+
web_search_tool = DuckDuckGoSearchTool()
|
| 59 |
|
| 60 |
@tool
|
| 61 |
def search_web(query: str) -> str:
|
|
|
|
| 65 |
"""
|
| 66 |
try:
|
| 67 |
# Execute the search query using DuckDuckGo
|
| 68 |
+
search_results = web_search_tool(query)
|
| 69 |
|
| 70 |
# Format and return the search results
|
| 71 |
return f"Search results for '{query}':\n\n{search_results}"
|
|
|
|
| 73 |
# Handle any errors that occur during the search
|
| 74 |
return f"Error searching the web: {str(e)}"
|
| 75 |
|
| 76 |
+
# FINAL ANSWER TOOL (REQUIRED)
|
| 77 |
+
# Initialize the final answer tool - required for the agent to provide final answers
|
| 78 |
final_answer = FinalAnswerTool()
|
| 79 |
|
| 80 |
|
| 81 |
+
# VISIT WEBPAGE TOOL - From tools directory
|
| 82 |
+
# Initialize the visit webpage tool
|
| 83 |
+
visit_webpage = VisitWebpageTool()
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
# TOOL INITIALIZATION AND VERIFICATION
|
| 87 |
+
# Ensure all tools are properly loaded and print status
|
| 88 |
+
print("Initializing tools...")
|
| 89 |
+
try:
|
| 90 |
+
# Import the image generation tool from Hugging Face Hub
|
| 91 |
+
# This tool will be used by the generate_image_from_text function
|
| 92 |
+
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 93 |
+
print("✓ Image generation tool loaded successfully")
|
| 94 |
+
except Exception as e:
|
| 95 |
+
print(f"✗ Failed to load image generation tool: {str(e)}")
|
| 96 |
+
# Provide a fallback if the image generation tool fails to load
|
| 97 |
+
def image_generation_tool(prompt):
|
| 98 |
+
return f"[MOCK IMAGE] Generated from prompt: {prompt}"
|
| 99 |
+
|
| 100 |
+
print("✓ Time zone tool initialized")
|
| 101 |
+
print("✓ Web search tool initialized")
|
| 102 |
+
print("✓ Visit webpage tool initialized")
|
| 103 |
+
print("✓ Final answer tool initialized")
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
# MODEL CONFIGURATION
|
| 107 |
+
print("Configuring model...")
|
| 108 |
# If the agent does not answer, the model is overloaded
|
| 109 |
# Alternative endpoint: 'https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 110 |
model = HfApiModel(
|
|
|
|
| 113 |
model_id='Qwen/Qwen2.5-Coder-32B-Instruct', # Using Qwen 2.5 Coder model
|
| 114 |
custom_role_conversions=None,
|
| 115 |
)
|
| 116 |
+
print("✓ Model configured successfully")
|
| 117 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
+
# LOAD PROMPT TEMPLATES
|
| 120 |
+
print("Loading prompt templates...")
|
| 121 |
+
# Create an upload directory if it doesn't exist
|
| 122 |
+
os.makedirs("uploads", exist_ok=True)
|
| 123 |
|
|
|
|
| 124 |
# Load prompt templates from YAML file for consistent agent responses
|
| 125 |
+
try:
|
| 126 |
+
with open("prompts.yaml", 'r') as stream:
|
| 127 |
+
prompt_templates = yaml.safe_load(stream)
|
| 128 |
+
print("✓ Prompt templates loaded successfully")
|
| 129 |
+
except Exception as e:
|
| 130 |
+
print(f"✗ Failed to load prompt templates: {str(e)}")
|
| 131 |
+
# Provide default empty templates if loading fails
|
| 132 |
+
prompt_templates = {}
|
| 133 |
|
| 134 |
|
| 135 |
+
# AGENT CONFIGURATION
|
| 136 |
+
print("Configuring agent...")
|
| 137 |
agent = CodeAgent(
|
| 138 |
model=model,
|
| 139 |
tools=[
|
| 140 |
get_current_time_in_timezone, # Tool 1: Time zone tool
|
| 141 |
generate_image_from_text, # Tool 2: Image generation tool
|
| 142 |
search_web, # Tool 3: Web search tool
|
| 143 |
+
visit_webpage, # Tool 4: Visit webpage tool (added from tools directory)
|
| 144 |
final_answer # Required final answer tool
|
| 145 |
],
|
| 146 |
max_steps=6, # Maximum number of reasoning steps
|
|
|
|
| 151 |
description=None, # No custom agent description
|
| 152 |
prompt_templates=prompt_templates # Using loaded prompt templates
|
| 153 |
)
|
| 154 |
+
print("✓ Agent configured successfully")
|
| 155 |
|
| 156 |
+
# LAUNCH THE GRADIO UI
|
| 157 |
+
print("Launching Gradio UI...")
|
| 158 |
+
# Start the Gradio interface with our configured agent and file upload directory
|
| 159 |
+
GradioUI(agent, file_upload_folder="uploads").launch(debug=True)
|