Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,25 +7,28 @@ from tools.final_answer import FinalAnswerTool
|
|
| 7 |
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
-
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 11 |
@tool
|
| 12 |
-
def get_nairobi_weather() -> str:
|
| 13 |
-
"""
|
|
|
|
|
|
|
|
|
|
| 14 |
# Nairobi coordinates
|
| 15 |
lat, lon = -1.286389, 36.817223
|
| 16 |
api_key = OPEN_WEATHER_TOKEN # Replace with your own key
|
| 17 |
|
| 18 |
try:
|
| 19 |
-
|
| 20 |
-
url = f"https://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&appid={api_key}&units=metric"
|
| 21 |
response = requests.get(url)
|
| 22 |
response.raise_for_status()
|
| 23 |
data = response.json()
|
| 24 |
|
| 25 |
-
#
|
|
|
|
|
|
|
| 26 |
report = (
|
| 27 |
-
f"
|
| 28 |
-
f"🌡️ Temperature: {data['main']['temp']}°C
|
| 29 |
f"☁️ Conditions: {data['weather'][0]['description'].title()}\n"
|
| 30 |
f"💧 Humidity: {data['main']['humidity']}%\n"
|
| 31 |
f"🌬️ Wind: {data['wind']['speed']} m/s\n"
|
|
@@ -34,53 +37,20 @@ def get_nairobi_weather() -> str:
|
|
| 34 |
return report
|
| 35 |
|
| 36 |
except requests.exceptions.RequestException as e:
|
| 37 |
-
return f"⚠️ Error fetching data: {str(e)}"
|
| 38 |
except KeyError:
|
| 39 |
return "⚠️ Error processing weather data"
|
| 40 |
|
| 41 |
-
#
|
| 42 |
-
with gr.Blocks(title="Nairobi Weather") as demo:
|
| 43 |
-
gr.Markdown("# 🌤️ Current Weather in Nairobi")
|
| 44 |
-
with gr.Row():
|
| 45 |
-
weather_output = gr.Textbox(label="Weather Report", interactive=False)
|
| 46 |
-
refresh_btn = gr.Button("Refresh Data")
|
| 47 |
-
refresh_btn.click(fn=get_nairobi_weather, outputs=weather_output)
|
| 48 |
-
|
| 49 |
-
# Display initial data
|
| 50 |
-
demo.load(fn=get_nairobi_weather, outputs=weather_output)
|
| 51 |
-
|
| 52 |
-
demo.launch()
|
| 53 |
-
|
| 54 |
-
@tool
|
| 55 |
-
def get_current_time_in_timezone(timezone: str) -> str:
|
| 56 |
-
"""A tool that fetches the current local time in a specified timezone.
|
| 57 |
-
Args:
|
| 58 |
-
timezone: A string representing a valid timezone (e.g., 'Kenya/Nairobi').
|
| 59 |
-
"""
|
| 60 |
-
try:
|
| 61 |
-
# Create timezone object
|
| 62 |
-
tz = pytz.timezone(timezone)
|
| 63 |
-
# Get current time in that timezone
|
| 64 |
-
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 65 |
-
return f"The current local time in {timezone} is: {local_time}"
|
| 66 |
-
except Exception as e:
|
| 67 |
-
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 68 |
-
|
| 69 |
-
|
| 70 |
final_answer = FinalAnswerTool()
|
| 71 |
|
| 72 |
-
# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
|
| 73 |
-
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 74 |
-
|
| 75 |
model = HfApiModel(
|
| 76 |
-
max_tokens=2096,
|
| 77 |
-
temperature=0.5,
|
| 78 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
| 79 |
-
custom_role_conversions=None,
|
| 80 |
)
|
| 81 |
|
| 82 |
-
|
| 83 |
-
# Import tool from Hub
|
| 84 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 85 |
|
| 86 |
with open("prompts.yaml", 'r') as stream:
|
|
@@ -88,7 +58,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 88 |
|
| 89 |
agent = CodeAgent(
|
| 90 |
model=model,
|
| 91 |
-
tools=[final_answer
|
| 92 |
max_steps=6,
|
| 93 |
verbosity_level=1,
|
| 94 |
grammar=None,
|
|
@@ -98,5 +68,4 @@ agent = CodeAgent(
|
|
| 98 |
prompt_templates=prompt_templates
|
| 99 |
)
|
| 100 |
|
| 101 |
-
|
| 102 |
GradioUI(agent).launch()
|
|
|
|
| 7 |
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
|
|
|
| 10 |
@tool
|
| 11 |
+
def get_nairobi_weather(units: Optional[str] = "metric") -> str:
|
| 12 |
+
"""A tool that fetches current weather data for Nairobi, Kenya.
|
| 13 |
+
Args:
|
| 14 |
+
units: (Optional) Temperature units - 'metric' (Celsius), 'imperial' (Fahrenheit), or 'standard' (Kelvin). Defaults to 'metric'.
|
| 15 |
+
"""
|
| 16 |
# Nairobi coordinates
|
| 17 |
lat, lon = -1.286389, 36.817223
|
| 18 |
api_key = OPEN_WEATHER_TOKEN # Replace with your own key
|
| 19 |
|
| 20 |
try:
|
| 21 |
+
url = f"https://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&appid={api_key}&units={units}"
|
|
|
|
| 22 |
response = requests.get(url)
|
| 23 |
response.raise_for_status()
|
| 24 |
data = response.json()
|
| 25 |
|
| 26 |
+
# Get current time in Nairobi
|
| 27 |
+
nairobi_time = datetime.datetime.now(pytz.timezone('Africa/Nairobi')).strftime("%Y-%m-%d %H:%M:%S")
|
| 28 |
+
|
| 29 |
report = (
|
| 30 |
+
f"🌤️ Current Weather in Nairobi, Kenya ({nairobi_time}):\n"
|
| 31 |
+
f"🌡️ Temperature: {data['main']['temp']}°{'C' if units == 'metric' else 'F' if units == 'imperial' else 'K'}\n"
|
| 32 |
f"☁️ Conditions: {data['weather'][0]['description'].title()}\n"
|
| 33 |
f"💧 Humidity: {data['main']['humidity']}%\n"
|
| 34 |
f"🌬️ Wind: {data['wind']['speed']} m/s\n"
|
|
|
|
| 37 |
return report
|
| 38 |
|
| 39 |
except requests.exceptions.RequestException as e:
|
| 40 |
+
return f"⚠️ Error fetching weather data: {str(e)}"
|
| 41 |
except KeyError:
|
| 42 |
return "⚠️ Error processing weather data"
|
| 43 |
|
| 44 |
+
# Your existing setup
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
final_answer = FinalAnswerTool()
|
| 46 |
|
|
|
|
|
|
|
|
|
|
| 47 |
model = HfApiModel(
|
| 48 |
+
max_tokens=2096,
|
| 49 |
+
temperature=0.5,
|
| 50 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
| 51 |
+
custom_role_conversions=None,
|
| 52 |
)
|
| 53 |
|
|
|
|
|
|
|
| 54 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 55 |
|
| 56 |
with open("prompts.yaml", 'r') as stream:
|
|
|
|
| 58 |
|
| 59 |
agent = CodeAgent(
|
| 60 |
model=model,
|
| 61 |
+
tools=[final_answer, get_current_time_in_timezone, get_nairobi_weather, image_generation_tool], # Added weather tool
|
| 62 |
max_steps=6,
|
| 63 |
verbosity_level=1,
|
| 64 |
grammar=None,
|
|
|
|
| 68 |
prompt_templates=prompt_templates
|
| 69 |
)
|
| 70 |
|
|
|
|
| 71 |
GradioUI(agent).launch()
|