Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,57 +1,38 @@
|
|
| 1 |
-
from
|
| 2 |
-
import
|
| 3 |
-
import pytz
|
| 4 |
-
import yaml
|
| 5 |
-
from tools.final_answer import FinalAnswerTool
|
| 6 |
-
from Gradio_UI import GradioUI
|
| 7 |
|
| 8 |
-
# Example custom tool
|
| 9 |
@tool
|
| 10 |
-
def
|
| 11 |
-
"""
|
| 12 |
-
Args:
|
| 13 |
-
arg1: the first argument
|
| 14 |
-
arg2: the second argument
|
| 15 |
-
"""
|
| 16 |
-
return f"My tool received arg1={arg1} and arg2={arg2}"
|
| 17 |
|
| 18 |
-
# Time tool
|
| 19 |
-
@tool
|
| 20 |
-
def get_current_time_in_timezone(timezone: str) -> str:
|
| 21 |
-
"""Get current local time in a specified timezone.
|
| 22 |
Args:
|
| 23 |
-
|
|
|
|
|
|
|
| 24 |
"""
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
return f"The current local time in {timezone} is: {local_time}"
|
| 29 |
-
except Exception as e:
|
| 30 |
-
return f"Error: {str(e)}"
|
| 31 |
|
| 32 |
-
|
| 33 |
-
final_answer = FinalAnswerTool()
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
|
| 43 |
-
with open("prompts.yaml", "r") as stream:
|
| 44 |
-
prompt_templates = yaml.safe_load(stream)
|
| 45 |
|
| 46 |
-
|
| 47 |
-
agent = CodeAgent(
|
| 48 |
-
model=model,
|
| 49 |
-
tools=[final_answer, my_custom_tool, get_current_time_in_timezone],
|
| 50 |
-
max_steps=6,
|
| 51 |
-
verbosity_level=1,
|
| 52 |
-
name="my_agent",
|
| 53 |
-
prompt_templates=prompt_templates,
|
| 54 |
-
)
|
| 55 |
|
| 56 |
-
# Launch Gradio UI
|
| 57 |
-
GradioUI(agent).launch()
|
|
|
|
| 1 |
+
from typing import Optional
|
| 2 |
+
from smolagents import CodeAgent, HfApiModel, tool
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
|
|
|
| 4 |
@tool
|
| 5 |
+
def get_travel_duration(start_location: str, destination_location: str, transportation_mode: Optional[str] = None) -> str:
|
| 6 |
+
"""Gets the travel time between two places.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
Args:
|
| 9 |
+
start_location: the place from which you start your ride
|
| 10 |
+
destination_location: the place of arrival
|
| 11 |
+
transportation_mode: The transportation mode, in 'driving', 'walking', 'bicycling', or 'transit'. Defaults to 'driving'.
|
| 12 |
"""
|
| 13 |
+
import os # All imports are placed within the function, to allow for sharing to Hub.
|
| 14 |
+
import googlemaps
|
| 15 |
+
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
gmaps = googlemaps.Client(os.getenv("GMAPS_API_KEY"))
|
|
|
|
| 18 |
|
| 19 |
+
if transportation_mode is None:
|
| 20 |
+
transportation_mode = "driving"
|
| 21 |
+
try:
|
| 22 |
+
directions_result = gmaps.directions(
|
| 23 |
+
start_location,
|
| 24 |
+
destination_location,
|
| 25 |
+
mode=transportation_mode,
|
| 26 |
+
departure_time=datetime(2025, 6, 6, 11, 0), # At 11, date far in the future
|
| 27 |
+
)
|
| 28 |
+
if len(directions_result) == 0:
|
| 29 |
+
return "No way found between these places with the required transportation mode."
|
| 30 |
+
return directions_result[0]["legs"][0]["duration"]["text"]
|
| 31 |
+
except Exception as e:
|
| 32 |
+
print(e)
|
| 33 |
+
return e
|
| 34 |
|
| 35 |
+
agent = CodeAgent(tools=[get_travel_duration], model=HfApiModel(), additional_authorized_imports=["datetime"])
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
agent.run("Can you give me a nice one-day trip around Paris with a few locations and the times? Could be in the city or outside, but should fit in one day. I'm travelling only with a rented bicycle.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
|
|
|
|
|