Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
| 2 |
import datetime
|
| 3 |
import requests
|
|
@@ -40,13 +41,14 @@ def calculator(a: int, b: int) -> int:
|
|
| 40 |
"""
|
| 41 |
return int(a) * int(b)
|
| 42 |
|
| 43 |
-
#
|
| 44 |
CITY_COORDS = {
|
| 45 |
"new york": (40.7128, -74.0060),
|
| 46 |
"paris": (48.8566, 2.3522),
|
| 47 |
"london": (51.5074, -0.1278),
|
| 48 |
"tokyo": (35.6762, 139.6503),
|
| 49 |
}
|
|
|
|
| 50 |
@tool
|
| 51 |
def get_weather(city: str) -> str:
|
| 52 |
"""Get current temperature and humidity for a known city (New York, Paris, London, Tokyo).
|
|
@@ -74,56 +76,181 @@ def get_weather(city: str) -> str:
|
|
| 74 |
except Exception as e:
|
| 75 |
return f"Weather fetch failed: {e}"
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
# -------------------------
|
| 78 |
# Required final answer tool
|
| 79 |
# -------------------------
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
# -------------------------
|
| 83 |
-
# Model
|
| 84 |
# -------------------------
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
# -------------------------
|
| 93 |
# Pre-made tools from Hub / smolagents
|
| 94 |
# -------------------------
|
| 95 |
-
# DuckDuckGo web search (no key)
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
# -------------------------
|
| 101 |
-
#
|
| 102 |
# -------------------------
|
| 103 |
-
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
# -------------------------
|
| 107 |
# Agent with tools registered
|
| 108 |
# -------------------------
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
| 3 |
import datetime
|
| 4 |
import requests
|
|
|
|
| 41 |
"""
|
| 42 |
return int(a) * int(b)
|
| 43 |
|
| 44 |
+
# Simple weather tool using Open-Meteo (no key needed)
|
| 45 |
CITY_COORDS = {
|
| 46 |
"new york": (40.7128, -74.0060),
|
| 47 |
"paris": (48.8566, 2.3522),
|
| 48 |
"london": (51.5074, -0.1278),
|
| 49 |
"tokyo": (35.6762, 139.6503),
|
| 50 |
}
|
| 51 |
+
|
| 52 |
@tool
|
| 53 |
def get_weather(city: str) -> str:
|
| 54 |
"""Get current temperature and humidity for a known city (New York, Paris, London, Tokyo).
|
|
|
|
| 76 |
except Exception as e:
|
| 77 |
return f"Weather fetch failed: {e}"
|
| 78 |
|
| 79 |
+
# -------------------------
|
| 80 |
+
# Simple image generation tool (fallback)
|
| 81 |
+
# -------------------------
|
| 82 |
+
@tool
|
| 83 |
+
def simple_image_description(prompt: str) -> str:
|
| 84 |
+
"""Generate a detailed description of an image based on the prompt.
|
| 85 |
+
This is a fallback when actual image generation is not available.
|
| 86 |
+
Args:
|
| 87 |
+
prompt: Description of the image to generate
|
| 88 |
+
"""
|
| 89 |
+
return f"Image description: {prompt}. Note: This is a text description as image generation is currently unavailable. To enable actual image generation, please add your Hugging Face token to the space settings."
|
| 90 |
+
|
| 91 |
# -------------------------
|
| 92 |
# Required final answer tool
|
| 93 |
# -------------------------
|
| 94 |
+
try:
|
| 95 |
+
final_answer = FinalAnswerTool()
|
| 96 |
+
print("✓ Final answer tool loaded")
|
| 97 |
+
except Exception as e:
|
| 98 |
+
print(f"✗ Error loading final answer tool: {e}")
|
| 99 |
+
# Create a fallback
|
| 100 |
+
@tool
|
| 101 |
+
def final_answer(answer: str) -> str:
|
| 102 |
+
"""Provide the final answer to the user."""
|
| 103 |
+
return answer
|
| 104 |
|
| 105 |
# -------------------------
|
| 106 |
+
# Model setup with error handling
|
| 107 |
# -------------------------
|
| 108 |
+
try:
|
| 109 |
+
model = HfApiModel(
|
| 110 |
+
max_tokens=2096,
|
| 111 |
+
temperature=0.5,
|
| 112 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
| 113 |
+
custom_role_conversions=None,
|
| 114 |
+
)
|
| 115 |
+
print("✓ Model loaded successfully")
|
| 116 |
+
except Exception as e:
|
| 117 |
+
print(f"✗ Error loading model: {e}")
|
| 118 |
+
# Try a fallback model
|
| 119 |
+
try:
|
| 120 |
+
model = HfApiModel(
|
| 121 |
+
max_tokens=1024,
|
| 122 |
+
temperature=0.5,
|
| 123 |
+
model_id='microsoft/DialoGPT-medium',
|
| 124 |
+
custom_role_conversions=None,
|
| 125 |
+
)
|
| 126 |
+
print("✓ Fallback model loaded")
|
| 127 |
+
except Exception as e2:
|
| 128 |
+
print(f"✗ Fallback model also failed: {e2}")
|
| 129 |
+
raise e2
|
| 130 |
|
| 131 |
# -------------------------
|
| 132 |
# Pre-made tools from Hub / smolagents
|
| 133 |
# -------------------------
|
| 134 |
+
# DuckDuckGo web search (no key) - with error handling
|
| 135 |
+
try:
|
| 136 |
+
duck_search = DuckDuckGoSearchTool()
|
| 137 |
+
print("✓ DuckDuckGo search tool loaded")
|
| 138 |
+
except Exception as e:
|
| 139 |
+
print(f"✗ Error loading DuckDuckGo tool: {e}")
|
| 140 |
+
duck_search = None
|
| 141 |
+
|
| 142 |
+
# Text-to-image tool - with proper authentication handling
|
| 143 |
+
image_generation_tool = None
|
| 144 |
+
hf_token = os.getenv("HF_TOKEN") or os.getenv("HUGGINGFACE_HUB_TOKEN")
|
| 145 |
+
|
| 146 |
+
if hf_token:
|
| 147 |
+
try:
|
| 148 |
+
# Set the token for huggingface_hub
|
| 149 |
+
from huggingface_hub import login
|
| 150 |
+
login(token=hf_token)
|
| 151 |
+
|
| 152 |
+
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 153 |
+
print("✓ Image generation tool loaded successfully")
|
| 154 |
+
except Exception as e:
|
| 155 |
+
print(f"✗ Failed to load image generation tool: {e}")
|
| 156 |
+
print(" Using fallback image description tool instead")
|
| 157 |
+
image_generation_tool = simple_image_description
|
| 158 |
+
else:
|
| 159 |
+
print("! No HF_TOKEN found - using fallback image description tool")
|
| 160 |
+
print(" To enable real image generation, add your Hugging Face token in Space settings")
|
| 161 |
+
image_generation_tool = simple_image_description
|
| 162 |
|
| 163 |
# -------------------------
|
| 164 |
+
# Load prompts with error handling
|
| 165 |
# -------------------------
|
| 166 |
+
prompt_templates = {}
|
| 167 |
+
try:
|
| 168 |
+
if os.path.exists("prompts.yaml"):
|
| 169 |
+
with open("prompts.yaml", 'r') as stream:
|
| 170 |
+
prompt_templates = yaml.safe_load(stream)
|
| 171 |
+
print("✓ Prompts loaded from prompts.yaml")
|
| 172 |
+
else:
|
| 173 |
+
print("! prompts.yaml not found, using default prompts")
|
| 174 |
+
prompt_templates = {
|
| 175 |
+
"system_prompt": """You are a helpful AI assistant with access to various tools.
|
| 176 |
+
|
| 177 |
+
Available tools:
|
| 178 |
+
- calculator: For mathematical operations
|
| 179 |
+
- get_weather: For weather information in major cities
|
| 180 |
+
- get_current_time_in_timezone: For time information in different timezones
|
| 181 |
+
- duck_search: For web searches (if available)
|
| 182 |
+
- simple_image_description or image_generation_tool: For image-related requests
|
| 183 |
+
- final_answer: To provide your final response
|
| 184 |
+
|
| 185 |
+
When users request image generation:
|
| 186 |
+
1. If image_generation_tool is available, use it to create actual images
|
| 187 |
+
2. If only simple_image_description is available, provide detailed descriptions
|
| 188 |
+
|
| 189 |
+
Always try to use the most appropriate tool for the task and provide helpful responses."""
|
| 190 |
+
}
|
| 191 |
+
except Exception as e:
|
| 192 |
+
print(f"✗ Error loading prompts: {e}")
|
| 193 |
+
prompt_templates = {}
|
| 194 |
+
|
| 195 |
+
# -------------------------
|
| 196 |
+
# Build tools list
|
| 197 |
+
# -------------------------
|
| 198 |
+
tools_list = [
|
| 199 |
+
final_answer,
|
| 200 |
+
calculator,
|
| 201 |
+
get_current_time_in_timezone,
|
| 202 |
+
get_weather,
|
| 203 |
+
my_custom_tool,
|
| 204 |
+
image_generation_tool, # This will be either the real tool or the fallback
|
| 205 |
+
]
|
| 206 |
+
|
| 207 |
+
if duck_search:
|
| 208 |
+
tools_list.append(duck_search)
|
| 209 |
+
|
| 210 |
+
print(f"✓ Loaded {len(tools_list)} tools")
|
| 211 |
|
| 212 |
# -------------------------
|
| 213 |
# Agent with tools registered
|
| 214 |
# -------------------------
|
| 215 |
+
try:
|
| 216 |
+
agent = CodeAgent(
|
| 217 |
+
model=model,
|
| 218 |
+
tools=tools_list,
|
| 219 |
+
max_steps=10,
|
| 220 |
+
verbosity_level=2,
|
| 221 |
+
grammar=None,
|
| 222 |
+
planning_interval=None,
|
| 223 |
+
name=None,
|
| 224 |
+
description=None,
|
| 225 |
+
prompt_templates=prompt_templates
|
| 226 |
+
)
|
| 227 |
+
print("✓ Agent created successfully")
|
| 228 |
+
except Exception as e:
|
| 229 |
+
print(f"✗ Error creating agent: {e}")
|
| 230 |
+
raise e
|
| 231 |
+
|
| 232 |
+
# -------------------------
|
| 233 |
+
# Launch UI
|
| 234 |
+
# -------------------------
|
| 235 |
+
try:
|
| 236 |
+
ui = GradioUI(agent)
|
| 237 |
+
print("✓ UI created successfully")
|
| 238 |
+
ui.launch()
|
| 239 |
+
except Exception as e:
|
| 240 |
+
print(f"✗ Error launching UI: {e}")
|
| 241 |
+
# Fallback: create a simple Gradio interface
|
| 242 |
+
import gradio as gr
|
| 243 |
+
|
| 244 |
+
def chat_function(message):
|
| 245 |
+
try:
|
| 246 |
+
return agent.run(message)
|
| 247 |
+
except Exception as e:
|
| 248 |
+
return f"Error: {str(e)}"
|
| 249 |
+
|
| 250 |
+
demo = gr.Interface(
|
| 251 |
+
fn=chat_function,
|
| 252 |
+
inputs=gr.Textbox(label="Your message"),
|
| 253 |
+
outputs=gr.Textbox(label="Agent response"),
|
| 254 |
+
title="AI Agent"
|
| 255 |
+
)
|
| 256 |
+
demo.launch()
|