Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,48 +3,31 @@ 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 |
-
# ✅
|
| 11 |
-
client = InferenceClient()
|
| 12 |
-
|
| 13 |
-
# ✅ Custom Tool: Image Generation
|
| 14 |
@tool
|
| 15 |
-
def
|
| 16 |
-
"""
|
| 17 |
-
|
| 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 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 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 |
-
|
| 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)
|
|
@@ -53,53 +36,12 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 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 |
-
|
| 75 |
-
if "results" not in geo_response or not geo_response["results"]:
|
| 76 |
-
return f"Error: Could not determine location for '{city}'."
|
| 77 |
-
|
| 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 |
-
|
| 85 |
-
if "daily" in weather_response and "snowfall_sum" in weather_response["daily"]:
|
| 86 |
-
snowfall = weather_response["daily"]["snowfall_sum"][0]
|
| 87 |
-
return f"Expected snowfall in {city} on {date}: {snowfall} cm."
|
| 88 |
-
|
| 89 |
-
return f"No snowfall data available for {city} on {date}."
|
| 90 |
-
|
| 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='
|
| 103 |
custom_role_conversions=None,
|
| 104 |
)
|
| 105 |
|
|
@@ -110,24 +52,18 @@ image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_co
|
|
| 110 |
with open("prompts.yaml", 'r') as stream:
|
| 111 |
prompt_templates = yaml.safe_load(stream)
|
| 112 |
|
| 113 |
-
# ✅ Create the AI Agent
|
| 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=
|
| 127 |
-
description=
|
| 128 |
prompt_templates=prompt_templates
|
| 129 |
)
|
| 130 |
|
| 131 |
# ✅ Launch the Gradio UI
|
| 132 |
GradioUI(agent).launch()
|
| 133 |
-
|
|
|
|
| 3 |
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
|
|
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
from Gradio_UI import GradioUI
|
| 8 |
|
| 9 |
+
# ✅ Custom Tool: Motivational Quote Generator
|
|
|
|
|
|
|
|
|
|
| 10 |
@tool
|
| 11 |
+
def get_motivational_quote(mood: str) -> str:
|
| 12 |
+
"""A tool that provides a motivational quote based on the user's mood.
|
| 13 |
+
Args:
|
| 14 |
+
mood: A string describing the user's current mood (e.g., 'happy', 'sad', 'tired').
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
"""
|
| 16 |
+
quotes = {
|
| 17 |
+
"happy": "Happiness is not something ready-made. It comes from your own actions. – Dalai Lama",
|
| 18 |
+
"sad": "Tough times never last, but tough people do. – Robert H. Schuller",
|
| 19 |
+
"tired": "Take a deep breath, you’ve got this. Rest, recharge, and rise again.",
|
| 20 |
+
"motivated": "The secret of getting ahead is getting started. – Mark Twain",
|
| 21 |
+
"stressed": "You are stronger than you think. Take it one step at a time."
|
| 22 |
+
}
|
| 23 |
+
return quotes.get(mood.lower(), "No specific quote for this mood, but remember, you're doing great!")
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
# ✅ Custom Tool: Fetch Current Time in a Given Timezone
|
| 26 |
@tool
|
| 27 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 28 |
+
"""A tool that fetches the current local time in a specified timezone.
|
| 29 |
+
Args:
|
| 30 |
+
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
"""
|
| 32 |
try:
|
| 33 |
tz = pytz.timezone(timezone)
|
|
|
|
| 36 |
except Exception as e:
|
| 37 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 38 |
|
| 39 |
+
# ✅ Load Essential Components
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
final_answer = FinalAnswerTool()
|
|
|
|
|
|
|
| 41 |
model = HfApiModel(
|
| 42 |
max_tokens=2096,
|
| 43 |
temperature=0.5,
|
| 44 |
+
model_id='https://wxknx1kg971u7k1n.us-east-1.aws.endpoints.huggingface.cloud', # Hugging Face model
|
| 45 |
custom_role_conversions=None,
|
| 46 |
)
|
| 47 |
|
|
|
|
| 52 |
with open("prompts.yaml", 'r') as stream:
|
| 53 |
prompt_templates = yaml.safe_load(stream)
|
| 54 |
|
| 55 |
+
# ✅ Create the AI Agent
|
| 56 |
agent = CodeAgent(
|
| 57 |
model=model,
|
| 58 |
+
tools=[final_answer, get_motivational_quote, get_current_time_in_timezone], # Added quote tool
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
max_steps=6,
|
| 60 |
verbosity_level=1,
|
| 61 |
grammar=None,
|
| 62 |
planning_interval=None,
|
| 63 |
+
name="MotivationAgent",
|
| 64 |
+
description="An AI agent that provides motivational quotes and time info.",
|
| 65 |
prompt_templates=prompt_templates
|
| 66 |
)
|
| 67 |
|
| 68 |
# ✅ Launch the Gradio UI
|
| 69 |
GradioUI(agent).launch()
|
|
|