Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
@tool
|
| 10 |
+
def my_custom_tool(arg1: str, arg2: int) -> str:
|
| 11 |
+
"""A custom tool that returns a string message.
|
| 12 |
+
|
| 13 |
+
Args:
|
| 14 |
+
arg1: A string input representing a message or concept (e.g., "AI-Powered Web3 Capital Markets").
|
| 15 |
+
arg2: An integer input representing a multiplier or repetition count (e.g., 3).
|
| 16 |
+
|
| 17 |
+
Returns:
|
| 18 |
+
A string message that creatively uses the provided arguments.
|
| 19 |
+
"""
|
| 20 |
+
return f"What magic will you build? {arg1} repeated {arg2} times: {arg1 * arg2}"
|
| 21 |
+
|
| 22 |
+
@tool
|
| 23 |
+
def get_current_time_in_timezone(timezone: str) -> str:
|
| 24 |
+
"""A tool that fetches the current local time in a specified timezone.
|
| 25 |
+
|
| 26 |
+
Args:
|
| 27 |
+
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 28 |
+
|
| 29 |
+
Returns:
|
| 30 |
+
A string with the current local time in the specified timezone or an error message.
|
| 31 |
+
"""
|
| 32 |
+
try:
|
| 33 |
+
# Create timezone object
|
| 34 |
+
tz = pytz.timezone(timezone)
|
| 35 |
+
# Get current time in that timezone
|
| 36 |
+
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 37 |
+
return f"The current local time in {timezone} is: {local_time}"
|
| 38 |
+
except pytz.UnknownTimeZoneError:
|
| 39 |
+
return f"Error: '{timezone}' is not a valid timezone."
|
| 40 |
+
except Exception as e:
|
| 41 |
+
# Log the error or re-raise it if necessary
|
| 42 |
+
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 43 |
+
|
| 44 |
+
# Initialize the FinalAnswerTool
|
| 45 |
+
final_answer = FinalAnswerTool()
|
| 46 |
+
|
| 47 |
+
# Initialize the model
|
| 48 |
+
model = HfApiModel(
|
| 49 |
+
max_tokens=2096,
|
| 50 |
+
temperature=0.5,
|
| 51 |
+
model_id='deepseek-ai/DeepSeek-R1-Distill-Qwen-32B',
|
| 52 |
+
custom_role_conversions=None,
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
# Load the image generation tool from the Hub
|
| 56 |
+
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 57 |
+
|
| 58 |
+
# Load prompt templates from the YAML file
|
| 59 |
+
try:
|
| 60 |
+
with open("prompts.yaml", 'r') as stream:
|
| 61 |
+
prompt_templates = yaml.safe_load(stream)
|
| 62 |
+
if not isinstance(prompt_templates, dict):
|
| 63 |
+
raise ValueError("The YAML file does not contain a dictionary.")
|
| 64 |
+
except FileNotFoundError:
|
| 65 |
+
raise FileNotFoundError("The file 'prompts.yaml' was not found.")
|
| 66 |
+
except yaml.YAMLError as e:
|
| 67 |
+
raise yaml.YAMLError(f"Error parsing 'prompts.yaml': {str(e)}")
|
| 68 |
+
|
| 69 |
+
# Create the agent with the specified tools
|
| 70 |
+
agent = CodeAgent(
|
| 71 |
+
model=model,
|
| 72 |
+
tools=[image_generation_tool, get_current_time_in_timezone, final_answer, DuckDuckGoSearchTool()], # Add or remove tools here
|
| 73 |
+
max_steps=6,
|
| 74 |
+
verbosity_level=1,
|
| 75 |
+
grammar=None, # Ensure this is intentional
|
| 76 |
+
planning_interval=None, # Ensure this is intentional
|
| 77 |
+
name=None, # Ensure this is intentional
|
| 78 |
+
description=None, # Ensure this is intentional
|
| 79 |
+
prompt_templates=prompt_templates
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
# Launch the Gradio UI
|
| 83 |
+
GradioUI(agent).launch()
|