Spaces:
Running
Running
Updated app.py
#9
by zalawar - opened
app.py
CHANGED
|
@@ -1,64 +1,102 @@
|
|
| 1 |
-
|
| 2 |
import datetime
|
| 3 |
-
import
|
|
|
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
-
from tools.final_answer import FinalAnswerTool
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
|
|
|
|
|
|
| 10 |
@tool
|
| 11 |
-
def my_custom_tool(arg1:str, arg2:int)-> str:
|
| 12 |
-
|
| 13 |
-
|
|
|
|
| 14 |
Args:
|
| 15 |
-
arg1:
|
| 16 |
-
arg2:
|
| 17 |
"""
|
| 18 |
-
return "
|
| 19 |
|
|
|
|
|
|
|
| 20 |
@tool
|
| 21 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 22 |
-
"""
|
|
|
|
|
|
|
| 23 |
Args:
|
| 24 |
-
timezone:
|
| 25 |
"""
|
| 26 |
try:
|
| 27 |
-
# Create timezone object
|
| 28 |
tz = pytz.timezone(timezone)
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
return
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
|
|
|
|
| 36 |
final_answer = FinalAnswerTool()
|
|
|
|
|
|
|
|
|
|
| 37 |
model = HfApiModel(
|
| 38 |
-
max_tokens=2096,
|
| 39 |
-
temperature=0.5,
|
| 40 |
-
model_id=
|
| 41 |
-
custom_role_conversions=None,
|
| 42 |
)
|
| 43 |
|
| 44 |
|
| 45 |
-
#
|
| 46 |
-
image_generation_tool = load_tool(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
with open("prompts.yaml",
|
| 49 |
prompt_templates = yaml.safe_load(stream)
|
| 50 |
-
|
|
|
|
|
|
|
| 51 |
agent = CodeAgent(
|
| 52 |
model=model,
|
| 53 |
-
tools=[
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
verbosity_level=1,
|
| 56 |
-
grammar=None,
|
| 57 |
planning_interval=None,
|
| 58 |
-
name=
|
| 59 |
-
description=
|
| 60 |
-
prompt_templates=prompt_templates
|
| 61 |
)
|
| 62 |
|
| 63 |
|
| 64 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
import datetime
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
|
| 5 |
import pytz
|
| 6 |
import yaml
|
|
|
|
| 7 |
|
| 8 |
+
from smolagents import (
|
| 9 |
+
CodeAgent,
|
| 10 |
+
DuckDuckGoSearchTool,
|
| 11 |
+
HfApiModel,
|
| 12 |
+
load_tool,
|
| 13 |
+
tool,
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
from tools.final_answer import FinalAnswerTool
|
| 17 |
from Gradio_UI import GradioUI
|
| 18 |
|
| 19 |
+
|
| 20 |
+
# 1. Custom tool
|
| 21 |
@tool
|
| 22 |
+
def my_custom_tool(arg1: str, arg2: int) -> str:
|
| 23 |
+
"""
|
| 24 |
+
Demonstration tool that combines two inputs.
|
| 25 |
+
|
| 26 |
Args:
|
| 27 |
+
arg1: A text message.
|
| 28 |
+
arg2: An integer value.
|
| 29 |
"""
|
| 30 |
+
return f"Message: {arg1}. Number received: {arg2}."
|
| 31 |
|
| 32 |
+
|
| 33 |
+
# 2. Time-zone tool
|
| 34 |
@tool
|
| 35 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 36 |
+
"""
|
| 37 |
+
Get current local time in a specified timezone.
|
| 38 |
+
|
| 39 |
Args:
|
| 40 |
+
timezone: Timezone such as Asia/Riyadh.
|
| 41 |
"""
|
| 42 |
try:
|
|
|
|
| 43 |
tz = pytz.timezone(timezone)
|
| 44 |
+
local_time = datetime.datetime.now(tz)
|
| 45 |
+
|
| 46 |
+
return (
|
| 47 |
+
f"Current time in {timezone}: "
|
| 48 |
+
f"{local_time.strftime('%Y-%m-%d %H:%M:%S')}"
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
except pytz.exceptions.UnknownTimeZoneError:
|
| 52 |
+
return f"Unknown timezone: {timezone}"
|
| 53 |
|
| 54 |
|
| 55 |
+
# 3. Final answer tool
|
| 56 |
final_answer = FinalAnswerTool()
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
# 4. Hugging Face model
|
| 60 |
model = HfApiModel(
|
| 61 |
+
max_tokens=2096,
|
| 62 |
+
temperature=0.5,
|
| 63 |
+
model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
|
|
|
| 64 |
)
|
| 65 |
|
| 66 |
|
| 67 |
+
# 5. Load image-generation tool
|
| 68 |
+
image_generation_tool = load_tool(
|
| 69 |
+
"agents-course/text-to-image",
|
| 70 |
+
trust_remote_code=True,
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
# 6. Load prompts
|
| 75 |
+
BASE_DIR = Path(__file__).resolve().parent
|
| 76 |
|
| 77 |
+
with open(BASE_DIR / "prompts.yaml", "r", encoding="utf-8") as stream:
|
| 78 |
prompt_templates = yaml.safe_load(stream)
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
# 7. Create agent
|
| 82 |
agent = CodeAgent(
|
| 83 |
model=model,
|
| 84 |
+
tools=[
|
| 85 |
+
final_answer,
|
| 86 |
+
my_custom_tool,
|
| 87 |
+
get_current_time_in_timezone,
|
| 88 |
+
DuckDuckGoSearchTool(),
|
| 89 |
+
image_generation_tool,
|
| 90 |
+
],
|
| 91 |
+
max_steps=8,
|
| 92 |
verbosity_level=1,
|
|
|
|
| 93 |
planning_interval=None,
|
| 94 |
+
name="First Agent",
|
| 95 |
+
description="A general-purpose AI agent with search, time and image tools.",
|
| 96 |
+
prompt_templates=prompt_templates,
|
| 97 |
)
|
| 98 |
|
| 99 |
|
| 100 |
+
# 8. Launch application
|
| 101 |
+
if __name__ == "__main__":
|
| 102 |
+
GradioUI(agent).launch()
|