Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,61 +1,91 @@
|
|
| 1 |
-
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
-
from tools.final_answer import FinalAnswerTool
|
| 7 |
-
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
-
#
|
| 11 |
@tool
|
| 12 |
-
def my_custom_tool(
|
| 13 |
-
|
| 14 |
-
|
| 15 |
Args:
|
| 16 |
-
|
| 17 |
-
|
| 18 |
"""
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
@tool
|
| 22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 23 |
"""A tool that fetches the current local time in a specified timezone.
|
|
|
|
| 24 |
Args:
|
| 25 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 26 |
"""
|
| 27 |
try:
|
| 28 |
# Create timezone object
|
| 29 |
tz = pytz.timezone(timezone)
|
|
|
|
| 30 |
# Get current time in that timezone
|
| 31 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
|
|
|
| 32 |
return f"The current local time in {timezone} is: {local_time}"
|
| 33 |
except Exception as e:
|
| 34 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 35 |
|
| 36 |
-
|
| 37 |
final_answer = FinalAnswerTool()
|
| 38 |
|
| 39 |
# 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:
|
| 40 |
-
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 41 |
-
|
| 42 |
model = HfApiModel(
|
| 43 |
-
max_tokens=2096,
|
| 44 |
-
temperature=0.5,
|
| 45 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct'
|
| 46 |
-
custom_role_conversions=None,
|
| 47 |
)
|
| 48 |
|
| 49 |
-
|
| 50 |
# Import tool from Hub
|
| 51 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 52 |
|
| 53 |
with open("prompts.yaml", 'r') as stream:
|
| 54 |
prompt_templates = yaml.safe_load(stream)
|
| 55 |
-
|
| 56 |
agent = CodeAgent(
|
| 57 |
model=model,
|
| 58 |
-
tools=[final_answer],
|
| 59 |
max_steps=6,
|
| 60 |
verbosity_level=1,
|
| 61 |
grammar=None,
|
|
@@ -65,5 +95,4 @@ agent = CodeAgent(
|
|
| 65 |
prompt_templates=prompt_templates
|
| 66 |
)
|
| 67 |
|
| 68 |
-
|
| 69 |
GradioUI(agent).launch()
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
+
from tools.final_answer import FinalAnswerTool
|
|
|
|
| 7 |
from Gradio_UI import GradioUI
|
| 8 |
|
| 9 |
+
# Updated with a useful function that provides information about a location
|
| 10 |
@tool
|
| 11 |
+
def my_custom_tool(location: str, details_required: str = "general") -> str:
|
| 12 |
+
"""A tool that fetches information about a specified location.
|
| 13 |
+
|
| 14 |
Args:
|
| 15 |
+
location: The name of the location to get information about
|
| 16 |
+
details_required: The type of information required (general, weather, population)
|
| 17 |
"""
|
| 18 |
+
try:
|
| 19 |
+
# This is a simple implementation - in a real scenario you might use an API
|
| 20 |
+
if details_required.lower() == "weather":
|
| 21 |
+
# Simulate a weather API call
|
| 22 |
+
weather_conditions = ["sunny", "cloudy", "rainy", "snowy", "windy"]
|
| 23 |
+
import random
|
| 24 |
+
condition = random.choice(weather_conditions)
|
| 25 |
+
temp = random.randint(0, 35) # Celsius
|
| 26 |
+
return f"Current weather in {location}: {condition}, {temp}°C"
|
| 27 |
+
|
| 28 |
+
elif details_required.lower() == "population":
|
| 29 |
+
# Simulate population data lookup
|
| 30 |
+
# In real implementation, you would fetch from a database or API
|
| 31 |
+
populations = {
|
| 32 |
+
"new york": "8.4 million",
|
| 33 |
+
"tokyo": "14 million",
|
| 34 |
+
"london": "9 million",
|
| 35 |
+
"paris": "2.1 million",
|
| 36 |
+
"sydney": "5.3 million"
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
location_lower = location.lower()
|
| 40 |
+
if location_lower in populations:
|
| 41 |
+
return f"The population of {location} is approximately {populations[location_lower]}."
|
| 42 |
+
else:
|
| 43 |
+
return f"Population data for {location} not found in the database."
|
| 44 |
+
|
| 45 |
+
else: # general information
|
| 46 |
+
return f"Here is general information about {location}. This is a placeholder - in a real implementation, this would fetch actual data about the location."
|
| 47 |
+
|
| 48 |
+
except Exception as e:
|
| 49 |
+
return f"Error fetching information for '{location}': {str(e)}"
|
| 50 |
|
| 51 |
@tool
|
| 52 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 53 |
"""A tool that fetches the current local time in a specified timezone.
|
| 54 |
+
|
| 55 |
Args:
|
| 56 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 57 |
"""
|
| 58 |
try:
|
| 59 |
# Create timezone object
|
| 60 |
tz = pytz.timezone(timezone)
|
| 61 |
+
|
| 62 |
# Get current time in that timezone
|
| 63 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 64 |
+
|
| 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 |
final_answer = FinalAnswerTool()
|
| 70 |
|
| 71 |
# 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:
|
| 72 |
+
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
|
|
|
| 73 |
model = HfApiModel(
|
| 74 |
+
max_tokens=2096,
|
| 75 |
+
temperature=0.5,
|
| 76 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct', # it is possible that this model may be overloaded
|
| 77 |
+
custom_role_conversions=None,
|
| 78 |
)
|
| 79 |
|
|
|
|
| 80 |
# Import tool from Hub
|
| 81 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 82 |
|
| 83 |
with open("prompts.yaml", 'r') as stream:
|
| 84 |
prompt_templates = yaml.safe_load(stream)
|
| 85 |
+
|
| 86 |
agent = CodeAgent(
|
| 87 |
model=model,
|
| 88 |
+
tools=[final_answer, my_custom_tool, get_current_time_in_timezone, image_generation_tool], # add your tools here (don't remove final answer)
|
| 89 |
max_steps=6,
|
| 90 |
verbosity_level=1,
|
| 91 |
grammar=None,
|
|
|
|
| 95 |
prompt_templates=prompt_templates
|
| 96 |
)
|
| 97 |
|
|
|
|
| 98 |
GradioUI(agent).launch()
|