Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,6 +8,97 @@ from tools.final_answer import FinalAnswerTool
|
|
8 |
from Gradio_UI import GradioUI
|
9 |
|
10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
|
13 |
final_answer = FinalAnswerTool()
|
@@ -32,7 +123,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
32 |
|
33 |
agent = CodeAgent(
|
34 |
model=model,
|
35 |
-
tools=[final_answer,
|
36 |
max_steps=6,
|
37 |
verbosity_level=1,
|
38 |
grammar=None,
|
|
|
8 |
from Gradio_UI import GradioUI
|
9 |
|
10 |
|
11 |
+
@tool
|
12 |
+
def get_stock_price(stock_symbol: str) -> str:
|
13 |
+
"""A tool that fetches the current stock price for given stock symbol.
|
14 |
+
|
15 |
+
Args:
|
16 |
+
stock_symbol: A string representing a valid NYSE (USA) stock ticker symbol (e.g., "AAPL" for Apple Inc., Consumer Electronics, Market Cap $3,674.40B).
|
17 |
+
"""
|
18 |
+
import yfinance as yf
|
19 |
+
stock_symbol = stock_symbol.upper() # Ensure uppercase
|
20 |
+
stock = yf.Ticker(stock_symbol + ".NYSE")
|
21 |
+
data = stock.history(period='5d') # Get last 5 days to handle non-trading days
|
22 |
+
|
23 |
+
if data.empty:
|
24 |
+
return {"error": f"No data found for {stock_symbol}."}
|
25 |
+
|
26 |
+
latest_data = data.iloc[-1]
|
27 |
+
current_price = round(latest_data['Close'], 2)
|
28 |
+
|
29 |
+
return current_price
|
30 |
+
|
31 |
+
|
32 |
+
@tool
|
33 |
+
def get_current_time_in_timezone(timezone: str) -> str:
|
34 |
+
"""A tool that fetches the current local time in a specified timezone.
|
35 |
+
|
36 |
+
Args:
|
37 |
+
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
38 |
+
|
39 |
+
Returns:
|
40 |
+
A string displaying the local date and time in the specified timezone.
|
41 |
+
"""
|
42 |
+
try:
|
43 |
+
# Validate timezone
|
44 |
+
if timezone not in pytz.all_timezones:
|
45 |
+
return f"❌ Error: '{timezone}' is not a valid timezone. Please provide a correct timezone (e.g., 'America/New_York')."
|
46 |
+
|
47 |
+
# Get the current time in the specified timezone
|
48 |
+
tz = pytz.timezone(timezone)
|
49 |
+
local_time = datetime.datetime.now(tz)
|
50 |
+
|
51 |
+
# Format the output with day, date, and time
|
52 |
+
formatted_time = local_time.strftime("%A, %Y-%m-%d %H:%M:%S %Z")
|
53 |
+
|
54 |
+
return f"🕒 The current local time in **{timezone}** is: **{formatted_time}**"
|
55 |
+
|
56 |
+
except Exception as e:
|
57 |
+
return f"⚠️ Error fetching time for timezone '{timezone}': {str(e)}"
|
58 |
+
|
59 |
+
@tool
|
60 |
+
def get_weather(location: str = None, zipcode: int = None) -> str:
|
61 |
+
"""Fetches the current weather for a specified location or zip code.
|
62 |
+
|
63 |
+
Args:
|
64 |
+
location: A string representing a valid city name or region (e.g., 'New York').
|
65 |
+
zipcode: An integer representing a valid zip code (e.g., 10001 for NYC).
|
66 |
+
|
67 |
+
Returns:
|
68 |
+
A string describing the weather conditions, temperature, and humidity.
|
69 |
+
"""
|
70 |
+
temperature = data["main"]["temp"]
|
71 |
+
conditions = data["weather"][0]["description"]
|
72 |
+
humidity = data["main"]["humidity"]
|
73 |
+
|
74 |
+
return (f"🌤️ Weather in {location or zipcode} is: {conditions.capitalize()}, "
|
75 |
+
f"Temperature: {temperature}°C, Humidity: {humidity}%")
|
76 |
+
|
77 |
+
#return f"The current weather in {location,zipcode} is: {temperature,}"
|
78 |
+
|
79 |
+
|
80 |
+
@tool
|
81 |
+
def getMarsWeather() -> str:
|
82 |
+
"""
|
83 |
+
A tool that fetches the current weather on Mars using NASA's InSight API.
|
84 |
+
Returns:
|
85 |
+
A string containing the sol (Martian day), average temperature, and wind speed on Mars.
|
86 |
+
"""
|
87 |
+
insightURL = 'https://api.nasa.gov/insight_weather/?api_key={}&feedtype=json&ver=1.0'.format(NASA_API_KEY)
|
88 |
+
response = requests.get(insightURL)
|
89 |
+
marsWeatherRaw = response.json()
|
90 |
+
firstKey = next(iter(marsWeatherRaw))
|
91 |
+
marsWeather = {
|
92 |
+
'sol': firstKey,
|
93 |
+
'temperature': marsWeatherRaw[firstKey]['AT']['av'],
|
94 |
+
'wind_speed': marsWeatherRaw[firstKey]['HWS']['av']
|
95 |
+
}
|
96 |
+
outputStr = "The temperature on Mars on Sol {sol} is {temperature} C and wind speeds of {wind_speed} m/sec.".format(
|
97 |
+
sol=marsWeather['sol'],
|
98 |
+
temperature=marsWeather['temperature'],
|
99 |
+
wind_speed=marsWeather['wind_speed'])
|
100 |
+
|
101 |
+
return outputStr
|
102 |
|
103 |
|
104 |
final_answer = FinalAnswerTool()
|
|
|
123 |
|
124 |
agent = CodeAgent(
|
125 |
model=model,
|
126 |
+
tools=[final_answer,get_weather,image_generation_tool,get_current_time_in_timezone,get_stock_price,DuckDuckGoSearchTool()], ## add your tools here (don't remove final answer)
|
127 |
max_steps=6,
|
128 |
verbosity_level=1,
|
129 |
grammar=None,
|