Update app.py
Browse files
app.py
CHANGED
@@ -1,27 +1,26 @@
|
|
1 |
-
|
2 |
import datetime
|
|
|
3 |
import pytz
|
4 |
import yaml
|
5 |
from tools.final_answer import FinalAnswerTool
|
6 |
from Gradio_UI import GradioUI
|
7 |
-
from transformers import pipeline
|
8 |
|
9 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
-
# Tool
|
12 |
-
web_search_tool = DuckDuckGoSearchTool()
|
13 |
-
|
14 |
-
# Tool 2: Summarization Tool
|
15 |
-
summarizer = pipeline("summarization")
|
16 |
-
|
17 |
-
def summarize_text(text: str) -> str:
|
18 |
-
"""Summarizes a given text into key points."""
|
19 |
-
summary = summarizer(text, max_length=100, min_length=30, do_sample=False)
|
20 |
-
return summary[0]['summary_text']
|
21 |
-
|
22 |
-
# Tool 3: Timezone Checker
|
23 |
def get_current_time_in_timezone(timezone: str) -> str:
|
24 |
-
"""Fetches the current local time in a specified timezone.
|
|
|
|
|
|
|
25 |
try:
|
26 |
tz = pytz.timezone(timezone)
|
27 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
@@ -29,12 +28,10 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
29 |
except Exception as e:
|
30 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
31 |
|
32 |
-
#
|
33 |
-
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
34 |
-
|
35 |
-
# === Defining the AI Model ===
|
36 |
final_answer = FinalAnswerTool()
|
37 |
|
|
|
38 |
model = HfApiModel(
|
39 |
max_tokens=2096,
|
40 |
temperature=0.5,
|
@@ -42,23 +39,25 @@ model = HfApiModel(
|
|
42 |
custom_role_conversions=None,
|
43 |
)
|
44 |
|
45 |
-
#
|
|
|
|
|
|
|
46 |
with open("prompts.yaml", 'r') as stream:
|
47 |
prompt_templates = yaml.safe_load(stream)
|
48 |
|
49 |
-
#
|
50 |
agent = CodeAgent(
|
51 |
model=model,
|
52 |
-
tools=[final_answer,
|
53 |
max_steps=6,
|
54 |
verbosity_level=1,
|
55 |
grammar=None,
|
56 |
planning_interval=None,
|
57 |
-
name="
|
58 |
-
description="An
|
59 |
prompt_templates=prompt_templates
|
60 |
)
|
61 |
|
62 |
-
# Launch
|
63 |
GradioUI(agent).launch()
|
64 |
-
|
|
|
1 |
+
ffrom 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 |
+
# Example custom tool
|
10 |
+
def my_custom_tool(arg1: str, arg2: int) -> str:
|
11 |
+
"""A tool that does nothing yet.
|
12 |
+
Args:
|
13 |
+
arg1: The first argument.
|
14 |
+
arg2: The second argument.
|
15 |
+
"""
|
16 |
+
return "What magic will you build?"
|
17 |
|
18 |
+
# Tool to get current time in a specified timezone
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
def get_current_time_in_timezone(timezone: str) -> str:
|
20 |
+
"""Fetches the current local time in a specified timezone.
|
21 |
+
Args:
|
22 |
+
timezone: A valid timezone string (e.g., 'America/New_York').
|
23 |
+
"""
|
24 |
try:
|
25 |
tz = pytz.timezone(timezone)
|
26 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
|
|
28 |
except Exception as e:
|
29 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
30 |
|
31 |
+
# Load final answer tool
|
|
|
|
|
|
|
32 |
final_answer = FinalAnswerTool()
|
33 |
|
34 |
+
# Define the model
|
35 |
model = HfApiModel(
|
36 |
max_tokens=2096,
|
37 |
temperature=0.5,
|
|
|
39 |
custom_role_conversions=None,
|
40 |
)
|
41 |
|
42 |
+
# Import an image generation tool from Hugging Face Hub
|
43 |
+
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
44 |
+
|
45 |
+
# Load prompt templates
|
46 |
with open("prompts.yaml", 'r') as stream:
|
47 |
prompt_templates = yaml.safe_load(stream)
|
48 |
|
49 |
+
# Create the Agent with the defined tools
|
50 |
agent = CodeAgent(
|
51 |
model=model,
|
52 |
+
tools=[final_answer, DuckDuckGoSearchTool(), get_current_time_in_timezone, image_generation_tool],
|
53 |
max_steps=6,
|
54 |
verbosity_level=1,
|
55 |
grammar=None,
|
56 |
planning_interval=None,
|
57 |
+
name="My First SmolAgent",
|
58 |
+
description="An AI assistant capable of searching, fetching time, and generating images.",
|
59 |
prompt_templates=prompt_templates
|
60 |
)
|
61 |
|
62 |
+
# Launch UI
|
63 |
GradioUI(agent).launch()
|
|