Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,13 @@
|
|
| 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(arg1: str, arg2: int) -> str:
|
| 13 |
"""
|
|
@@ -20,7 +20,6 @@ def my_custom_tool(arg1: str, arg2: int) -> str:
|
|
| 20 |
Returns:
|
| 21 |
A string indicating the equivalent time in seconds.
|
| 22 |
"""
|
| 23 |
-
# Dictionary to convert different time units to seconds
|
| 24 |
time_units = {
|
| 25 |
"seconds": 1,
|
| 26 |
"minutes": 60,
|
|
@@ -28,59 +27,69 @@ def my_custom_tool(arg1: str, arg2: int) -> str:
|
|
| 28 |
"days": 86400
|
| 29 |
}
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
@tool
|
| 39 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 40 |
-
"""
|
|
|
|
|
|
|
| 41 |
Args:
|
| 42 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
|
|
|
|
|
|
|
|
|
| 43 |
"""
|
| 44 |
try:
|
| 45 |
-
# Create timezone object
|
| 46 |
tz = pytz.timezone(timezone)
|
| 47 |
-
# Get current time in that timezone
|
| 48 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 49 |
return f"The current local time in {timezone} is: {local_time}"
|
| 50 |
except Exception as e:
|
| 51 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 52 |
|
| 53 |
|
| 54 |
-
|
| 55 |
|
| 56 |
-
|
| 57 |
-
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 58 |
|
| 59 |
model = HfApiModel(
|
| 60 |
-
max_tokens=2096,
|
| 61 |
-
temperature=0.5,
|
| 62 |
-
model_id=
|
| 63 |
-
custom_role_conversions=None,
|
| 64 |
)
|
| 65 |
|
| 66 |
-
|
| 67 |
-
# Import tool from Hub
|
| 68 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 69 |
|
| 70 |
-
|
|
|
|
| 71 |
prompt_templates = yaml.safe_load(stream)
|
| 72 |
-
|
|
|
|
|
|
|
| 73 |
agent = CodeAgent(
|
| 74 |
model=model,
|
| 75 |
-
tools=[final_answer],
|
| 76 |
max_steps=6,
|
| 77 |
verbosity_level=1,
|
| 78 |
grammar=None,
|
| 79 |
planning_interval=None,
|
| 80 |
name=None,
|
| 81 |
description=None,
|
| 82 |
-
prompt_templates=prompt_templates
|
| 83 |
)
|
| 84 |
|
|
|
|
| 85 |
|
| 86 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import datetime
|
| 2 |
import requests
|
| 3 |
import pytz
|
| 4 |
import yaml
|
| 5 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
|
|
|
| 7 |
from Gradio_UI import GradioUI
|
| 8 |
|
| 9 |
+
# Custom Tools
|
| 10 |
+
|
| 11 |
@tool
|
| 12 |
def my_custom_tool(arg1: str, arg2: int) -> str:
|
| 13 |
"""
|
|
|
|
| 20 |
Returns:
|
| 21 |
A string indicating the equivalent time in seconds.
|
| 22 |
"""
|
|
|
|
| 23 |
time_units = {
|
| 24 |
"seconds": 1,
|
| 25 |
"minutes": 60,
|
|
|
|
| 27 |
"days": 86400
|
| 28 |
}
|
| 29 |
|
| 30 |
+
try:
|
| 31 |
+
unit = arg1.lower()
|
| 32 |
+
if unit in time_units:
|
| 33 |
+
total_seconds = arg2 * time_units[unit]
|
| 34 |
+
return f"{arg2} {arg1} is equal to {total_seconds} seconds."
|
| 35 |
+
else:
|
| 36 |
+
return "Invalid time unit. Please use 'seconds', 'minutes', 'hours', or 'days'."
|
| 37 |
+
except Exception as e:
|
| 38 |
+
return f"Error: {str(e)}"
|
| 39 |
+
|
| 40 |
|
| 41 |
@tool
|
| 42 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 43 |
+
"""
|
| 44 |
+
A tool that fetches the current local time in a specified timezone.
|
| 45 |
+
|
| 46 |
Args:
|
| 47 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 48 |
+
|
| 49 |
+
Returns:
|
| 50 |
+
A string with the local time in the given timezone.
|
| 51 |
"""
|
| 52 |
try:
|
|
|
|
| 53 |
tz = pytz.timezone(timezone)
|
|
|
|
| 54 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 55 |
return f"The current local time in {timezone} is: {local_time}"
|
| 56 |
except Exception as e:
|
| 57 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 58 |
|
| 59 |
|
| 60 |
+
# Model Configuration
|
| 61 |
|
| 62 |
+
final_answer = FinalAnswerTool()
|
|
|
|
| 63 |
|
| 64 |
model = HfApiModel(
|
| 65 |
+
max_tokens=2096,
|
| 66 |
+
temperature=0.5,
|
| 67 |
+
model_id="Qwen/Qwen2.5-Coder-32B-Instruct", # This model may be overloaded
|
| 68 |
+
custom_role_conversions=None,
|
| 69 |
)
|
| 70 |
|
| 71 |
+
# Import tool from Hugging Face Hub
|
|
|
|
| 72 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 73 |
|
| 74 |
+
# Load prompts from YAML file
|
| 75 |
+
with open("prompts.yaml", "r") as stream:
|
| 76 |
prompt_templates = yaml.safe_load(stream)
|
| 77 |
+
|
| 78 |
+
# Agent Setup
|
| 79 |
+
|
| 80 |
agent = CodeAgent(
|
| 81 |
model=model,
|
| 82 |
+
tools=[final_answer], # Add other tools here if needed
|
| 83 |
max_steps=6,
|
| 84 |
verbosity_level=1,
|
| 85 |
grammar=None,
|
| 86 |
planning_interval=None,
|
| 87 |
name=None,
|
| 88 |
description=None,
|
| 89 |
+
prompt_templates=prompt_templates,
|
| 90 |
)
|
| 91 |
|
| 92 |
+
# Launch the Gradio UI
|
| 93 |
|
| 94 |
+
if __name__ == "__main__":
|
| 95 |
+
GradioUI(agent).launch()
|