Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,77 +1,98 @@
|
|
| 1 |
# app.py
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from smolagents import (
|
| 4 |
-
CodeAgent,
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
| 6 |
)
|
| 7 |
from tools.final_answer import FinalAnswerTool
|
| 8 |
-
from Gradio_UI import GradioUI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# ────────────────────────────────
|
| 11 |
-
# 1. Custom tools
|
| 12 |
# ────────────────────────────────
|
|
|
|
| 13 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 14 |
-
"""Return the current local time in
|
| 15 |
Args:
|
| 16 |
-
timezone:
|
| 17 |
"""
|
| 18 |
try:
|
| 19 |
tz = pytz.timezone(timezone)
|
| 20 |
-
|
|
|
|
| 21 |
except Exception as e:
|
| 22 |
return f"Error: {e}"
|
| 23 |
|
|
|
|
|
|
|
| 24 |
def my_custom_tool(arg1: str, arg2: int) -> str:
|
| 25 |
"""A playful demo tool.
|
| 26 |
Args:
|
| 27 |
arg1: any text
|
| 28 |
arg2: any integer
|
| 29 |
"""
|
| 30 |
-
return f"You sent {arg1!r} and {arg2}
|
|
|
|
| 31 |
|
| 32 |
# ────────────────────────────────
|
| 33 |
-
# 2.
|
| 34 |
# ────────────────────────────────
|
| 35 |
image_generation_tool = load_tool(
|
| 36 |
"agents-course/text-to-image",
|
| 37 |
trust_remote_code=True
|
| 38 |
)
|
| 39 |
-
duck_search_tool = DuckDuckGoSearchTool()
|
|
|
|
| 40 |
|
| 41 |
# ────────────────────────────────
|
| 42 |
-
# 3. LLM
|
| 43 |
# ────────────────────────────────
|
| 44 |
model = HfApiModel(
|
| 45 |
model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 46 |
-
max_tokens=
|
| 47 |
temperature=0.5,
|
| 48 |
)
|
| 49 |
|
|
|
|
| 50 |
# ────────────────────────────────
|
| 51 |
# 4. Prompt templates
|
| 52 |
# ────────────────────────────────
|
| 53 |
with open("prompts.yaml") as f:
|
| 54 |
prompt_templates = yaml.safe_load(f)
|
| 55 |
|
|
|
|
| 56 |
# ────────────────────────────────
|
| 57 |
-
# 5.
|
| 58 |
# ────────────────────────────────
|
| 59 |
agent = CodeAgent(
|
| 60 |
model=model,
|
| 61 |
tools=[
|
| 62 |
-
FinalAnswerTool(),
|
| 63 |
duck_search_tool,
|
| 64 |
image_generation_tool,
|
| 65 |
-
get_current_time_in_timezone,
|
| 66 |
-
my_custom_tool
|
| 67 |
],
|
| 68 |
max_steps=6,
|
| 69 |
verbosity_level=1,
|
| 70 |
-
prompt_templates=prompt_templates
|
| 71 |
)
|
| 72 |
|
|
|
|
| 73 |
# ────────────────────────────────
|
| 74 |
-
# 6. Launch
|
| 75 |
# ────────────────────────────────
|
| 76 |
if __name__ == "__main__":
|
| 77 |
GradioUI(agent).launch()
|
|
|
|
| 1 |
# app.py
|
| 2 |
+
"""
|
| 3 |
+
Fully‑working SmolAgents chat‑based agent for Hugging Face Spaces.
|
| 4 |
+
Paste this file into the root of your duplicated Space.
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
from smolagents import (
|
| 8 |
+
CodeAgent,
|
| 9 |
+
DuckDuckGoSearchTool,
|
| 10 |
+
HfApiModel,
|
| 11 |
+
load_tool,
|
| 12 |
+
tool, # decorator that auto‑wraps functions as Tool objects
|
| 13 |
)
|
| 14 |
from tools.final_answer import FinalAnswerTool
|
| 15 |
+
from Gradio_UI import GradioUI
|
| 16 |
+
|
| 17 |
+
import datetime
|
| 18 |
+
import pytz
|
| 19 |
+
import yaml
|
| 20 |
+
|
| 21 |
|
| 22 |
# ────────────────────────────────
|
| 23 |
+
# 1. Custom tools (decorated)
|
| 24 |
# ────────────────────────────────
|
| 25 |
+
@tool
|
| 26 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 27 |
+
"""Return the current local time in an IANA timezone.
|
| 28 |
Args:
|
| 29 |
+
timezone: e.g. 'Asia/Karachi'
|
| 30 |
"""
|
| 31 |
try:
|
| 32 |
tz = pytz.timezone(timezone)
|
| 33 |
+
now = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 34 |
+
return f"The current time in {timezone} is {now}"
|
| 35 |
except Exception as e:
|
| 36 |
return f"Error: {e}"
|
| 37 |
|
| 38 |
+
|
| 39 |
+
@tool
|
| 40 |
def my_custom_tool(arg1: str, arg2: int) -> str:
|
| 41 |
"""A playful demo tool.
|
| 42 |
Args:
|
| 43 |
arg1: any text
|
| 44 |
arg2: any integer
|
| 45 |
"""
|
| 46 |
+
return f"You sent {arg1!r} and {arg2}. Now build something cooler 😉"
|
| 47 |
+
|
| 48 |
|
| 49 |
# ────────────────────────────────
|
| 50 |
+
# 2. Hub & built‑in tools
|
| 51 |
# ────────────────────────────────
|
| 52 |
image_generation_tool = load_tool(
|
| 53 |
"agents-course/text-to-image",
|
| 54 |
trust_remote_code=True
|
| 55 |
)
|
| 56 |
+
duck_search_tool = DuckDuckGoSearchTool() # simple web search
|
| 57 |
+
|
| 58 |
|
| 59 |
# ────────────────────────────────
|
| 60 |
+
# 3. LLM backend
|
| 61 |
# ────────────────────────────────
|
| 62 |
model = HfApiModel(
|
| 63 |
model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 64 |
+
max_tokens=2048,
|
| 65 |
temperature=0.5,
|
| 66 |
)
|
| 67 |
|
| 68 |
+
|
| 69 |
# ────────────────────────────────
|
| 70 |
# 4. Prompt templates
|
| 71 |
# ────────────────────────────────
|
| 72 |
with open("prompts.yaml") as f:
|
| 73 |
prompt_templates = yaml.safe_load(f)
|
| 74 |
|
| 75 |
+
|
| 76 |
# ────────────────────────────────
|
| 77 |
+
# 5. Assemble the agent
|
| 78 |
# ────────────────────────────────
|
| 79 |
agent = CodeAgent(
|
| 80 |
model=model,
|
| 81 |
tools=[
|
| 82 |
+
FinalAnswerTool(), # ← keep this first
|
| 83 |
duck_search_tool,
|
| 84 |
image_generation_tool,
|
| 85 |
+
get_current_time_in_timezone,
|
| 86 |
+
my_custom_tool,
|
| 87 |
],
|
| 88 |
max_steps=6,
|
| 89 |
verbosity_level=1,
|
| 90 |
+
prompt_templates=prompt_templates,
|
| 91 |
)
|
| 92 |
|
| 93 |
+
|
| 94 |
# ────────────────────────────────
|
| 95 |
+
# 6. Launch Gradio
|
| 96 |
# ────────────────────────────────
|
| 97 |
if __name__ == "__main__":
|
| 98 |
GradioUI(agent).launch()
|