Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,23 +1,74 @@
|
|
| 1 |
-
from smolagents import CodeAgent, HfApiModel, load_tool, tool
|
|
|
|
| 2 |
import requests
|
|
|
|
| 3 |
import yaml
|
|
|
|
| 4 |
from tools.final_answer import FinalAnswerTool
|
| 5 |
from Gradio_UI import GradioUI
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
# ✅ Custom Tool: Snowfall Forecast Lookup
|
| 8 |
@tool
|
| 9 |
def get_snowfall_forecast(city: str, date: str) -> str:
|
| 10 |
"""
|
| 11 |
-
|
| 12 |
|
| 13 |
-
|
| 14 |
-
city (str)
|
| 15 |
-
date (str)
|
| 16 |
|
| 17 |
Returns:
|
| 18 |
-
str: The expected snowfall in centimeters or an error message.
|
| 19 |
"""
|
| 20 |
try:
|
|
|
|
| 21 |
geo_url = f"https://geocoding-api.open-meteo.com/v1/search?name={city}&count=1&format=json"
|
| 22 |
geo_response = requests.get(geo_url).json()
|
| 23 |
|
|
@@ -27,6 +78,7 @@ def get_snowfall_forecast(city: str, date: str) -> str:
|
|
| 27 |
latitude = geo_response["results"][0]["latitude"]
|
| 28 |
longitude = geo_response["results"][0]["longitude"]
|
| 29 |
|
|
|
|
| 30 |
weather_url = f"https://api.open-meteo.com/v1/forecast?latitude={latitude}&longitude={longitude}&daily=snowfall_sum&timezone=auto&start_date={date}&end_date={date}"
|
| 31 |
weather_response = requests.get(weather_url).json()
|
| 32 |
|
|
@@ -39,3 +91,43 @@ def get_snowfall_forecast(city: str, date: str) -> str:
|
|
| 39 |
except Exception as e:
|
| 40 |
return f"Error retrieving snowfall forecast: {str(e)}"
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
| 2 |
+
import datetime
|
| 3 |
import requests
|
| 4 |
+
import pytz
|
| 5 |
import yaml
|
| 6 |
+
from huggingface_hub import InferenceClient
|
| 7 |
from tools.final_answer import FinalAnswerTool
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
+
# ✅ Initialize the Hugging Face Inference Client
|
| 11 |
+
client = InferenceClient()
|
| 12 |
+
|
| 13 |
+
# ✅ Custom Tool: Image Generation
|
| 14 |
+
@tool
|
| 15 |
+
def generate_image(description: str) -> str:
|
| 16 |
+
"""
|
| 17 |
+
Generates an image based on a given text description.
|
| 18 |
+
|
| 19 |
+
Arguments:
|
| 20 |
+
description: (str) A textual prompt describing the image to be generated.
|
| 21 |
+
|
| 22 |
+
Returns:
|
| 23 |
+
str: A URL pointing to the generated image or an error message if the generation fails.
|
| 24 |
+
"""
|
| 25 |
+
try:
|
| 26 |
+
model_id = "stabilityai/stable-diffusion-xl-base-1.0"
|
| 27 |
+
result = client.text_to_image(model=model_id, prompt=description)
|
| 28 |
+
|
| 29 |
+
if isinstance(result, dict) and 'url' in result:
|
| 30 |
+
return f"Here is your generated image: {result['url']}"
|
| 31 |
+
return "Error: Image generation failed. No URL returned."
|
| 32 |
+
|
| 33 |
+
except Exception as e:
|
| 34 |
+
return f"Error generating image: {str(e)}"
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
# ✅ Custom Tool: Fetch Current Time in a Given Timezone
|
| 38 |
+
@tool
|
| 39 |
+
def get_current_time_in_timezone(timezone: str) -> str:
|
| 40 |
+
"""
|
| 41 |
+
Fetches the current local time in a specified timezone.
|
| 42 |
+
|
| 43 |
+
Arguments:
|
| 44 |
+
timezone: (str) A valid timezone string (e.g., 'America/New_York').
|
| 45 |
+
|
| 46 |
+
Returns:
|
| 47 |
+
str: The current time in the given timezone or an error message.
|
| 48 |
+
"""
|
| 49 |
+
try:
|
| 50 |
+
tz = pytz.timezone(timezone)
|
| 51 |
+
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 52 |
+
return f"The current local time in {timezone} is: {local_time}"
|
| 53 |
+
except Exception as e:
|
| 54 |
+
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 55 |
+
|
| 56 |
+
|
| 57 |
# ✅ Custom Tool: Snowfall Forecast Lookup
|
| 58 |
@tool
|
| 59 |
def get_snowfall_forecast(city: str, date: str) -> str:
|
| 60 |
"""
|
| 61 |
+
Retrieves the expected snowfall for a given city on a specific date.
|
| 62 |
|
| 63 |
+
Arguments:
|
| 64 |
+
city: (str) The name of the city for which to retrieve the snowfall forecast.
|
| 65 |
+
date: (str) The date in YYYY-MM-DD format.
|
| 66 |
|
| 67 |
Returns:
|
| 68 |
+
str: The expected snowfall in centimeters, or an error message if data is unavailable.
|
| 69 |
"""
|
| 70 |
try:
|
| 71 |
+
# Get latitude & longitude for the city
|
| 72 |
geo_url = f"https://geocoding-api.open-meteo.com/v1/search?name={city}&count=1&format=json"
|
| 73 |
geo_response = requests.get(geo_url).json()
|
| 74 |
|
|
|
|
| 78 |
latitude = geo_response["results"][0]["latitude"]
|
| 79 |
longitude = geo_response["results"][0]["longitude"]
|
| 80 |
|
| 81 |
+
# Fetch snowfall data
|
| 82 |
weather_url = f"https://api.open-meteo.com/v1/forecast?latitude={latitude}&longitude={longitude}&daily=snowfall_sum&timezone=auto&start_date={date}&end_date={date}"
|
| 83 |
weather_response = requests.get(weather_url).json()
|
| 84 |
|
|
|
|
| 91 |
except Exception as e:
|
| 92 |
return f"Error retrieving snowfall forecast: {str(e)}"
|
| 93 |
|
| 94 |
+
|
| 95 |
+
# ✅ Load the Final Answer Tool
|
| 96 |
+
final_answer = FinalAnswerTool()
|
| 97 |
+
|
| 98 |
+
# ✅ Load the Language Model
|
| 99 |
+
model = HfApiModel(
|
| 100 |
+
max_tokens=2096,
|
| 101 |
+
temperature=0.5,
|
| 102 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
| 103 |
+
custom_role_conversions=None,
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
# ✅ Load Image Generation Tool from the Hub
|
| 107 |
+
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 108 |
+
|
| 109 |
+
# ✅ Load Prompt Templates
|
| 110 |
+
with open("prompts.yaml", 'r') as stream:
|
| 111 |
+
prompt_templates = yaml.safe_load(stream)
|
| 112 |
+
|
| 113 |
+
# ✅ Create the AI Agent with Tools
|
| 114 |
+
agent = CodeAgent(
|
| 115 |
+
model=model,
|
| 116 |
+
tools=[
|
| 117 |
+
final_answer,
|
| 118 |
+
generate_image,
|
| 119 |
+
get_current_time_in_timezone,
|
| 120 |
+
get_snowfall_forecast # Added snowfall forecast tool
|
| 121 |
+
],
|
| 122 |
+
max_steps=6,
|
| 123 |
+
verbosity_level=1,
|
| 124 |
+
grammar=None,
|
| 125 |
+
planning_interval=None,
|
| 126 |
+
name=None,
|
| 127 |
+
description=None,
|
| 128 |
+
prompt_templates=prompt_templates
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
# ✅ Launch the Gradio UI
|
| 132 |
+
GradioUI(agent).launch()
|
| 133 |
+
|