Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,81 +1,94 @@
|
|
| 1 |
-
from smolagents import CodeAgent, HfApiModel,
|
|
|
|
|
|
|
| 2 |
import datetime
|
| 3 |
import pytz
|
| 4 |
import yaml
|
| 5 |
from tools.final_answer import FinalAnswerTool
|
| 6 |
from Gradio_UI import GradioUI
|
|
|
|
| 7 |
|
| 8 |
-
# ----------
|
| 9 |
-
# Custom tools
|
| 10 |
-
# -------------------------
|
| 11 |
@tool
|
| 12 |
def my_custom_tool(arg1: str, arg2: int) -> str:
|
| 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: A
|
| 25 |
"""
|
| 26 |
try:
|
| 27 |
tz = pytz.timezone(timezone)
|
| 28 |
-
|
| 29 |
-
return f"The current local time in {timezone} is
|
| 30 |
except Exception as e:
|
| 31 |
-
return f"Error
|
| 32 |
|
| 33 |
-
# ----------
|
| 34 |
-
# Final answer tool
|
| 35 |
-
# -------------------------
|
| 36 |
final_answer = FinalAnswerTool()
|
| 37 |
|
| 38 |
-
# ----------
|
| 39 |
-
# Model setup
|
| 40 |
-
# -------------------------
|
| 41 |
model = HfApiModel(
|
| 42 |
-
model_id=
|
| 43 |
max_tokens=2096,
|
| 44 |
temperature=0.5
|
| 45 |
)
|
| 46 |
|
| 47 |
-
# ----------
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
# ----------
|
| 57 |
-
# Load prompt templates
|
| 58 |
-
# -------------------------
|
| 59 |
with open("prompts.yaml", "r") as f:
|
| 60 |
prompt_templates = yaml.safe_load(f)
|
| 61 |
|
| 62 |
-
# ----------
|
| 63 |
-
# Agent setup
|
| 64 |
-
# -------------------------
|
| 65 |
agent = CodeAgent(
|
| 66 |
model=model,
|
| 67 |
tools=[
|
| 68 |
final_answer,
|
| 69 |
-
generate_image,
|
| 70 |
get_current_time_in_timezone,
|
| 71 |
-
my_custom_tool
|
| 72 |
],
|
| 73 |
max_steps=6,
|
| 74 |
verbosity_level=1,
|
| 75 |
-
prompt_templates=prompt_templates
|
| 76 |
)
|
| 77 |
|
| 78 |
-
# ----------
|
| 79 |
-
# Launch UI
|
| 80 |
-
# -------------------------
|
| 81 |
GradioUI(agent).launch()
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, HfApiModel, tool
|
| 2 |
+
from smolagents.agent_types import AgentImage
|
| 3 |
+
import requests
|
| 4 |
import datetime
|
| 5 |
import pytz
|
| 6 |
import yaml
|
| 7 |
from tools.final_answer import FinalAnswerTool
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
+
import os
|
| 10 |
|
| 11 |
+
# ----- Custom Tools -----
|
|
|
|
|
|
|
| 12 |
@tool
|
| 13 |
def my_custom_tool(arg1: str, arg2: int) -> str:
|
| 14 |
+
"""
|
| 15 |
+
Simple demo tool.
|
| 16 |
+
|
| 17 |
Args:
|
| 18 |
+
arg1: The first argument
|
| 19 |
+
arg2: The second argument
|
| 20 |
"""
|
| 21 |
+
return f"Your inputs were arg1={arg1} and arg2={arg2}"
|
| 22 |
|
| 23 |
@tool
|
| 24 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 25 |
+
"""
|
| 26 |
+
Returns current local time in a given timezone.
|
| 27 |
+
|
| 28 |
Args:
|
| 29 |
+
timezone: A valid timezone string.
|
| 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 local time in {timezone} is {now}."
|
| 35 |
except Exception as e:
|
| 36 |
+
return f"Error with timezone '{timezone}': {str(e)}"
|
| 37 |
|
| 38 |
+
# ----- Final Answer -----
|
|
|
|
|
|
|
| 39 |
final_answer = FinalAnswerTool()
|
| 40 |
|
| 41 |
+
# ----- Model Setup -----
|
|
|
|
|
|
|
| 42 |
model = HfApiModel(
|
| 43 |
+
model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 44 |
max_tokens=2096,
|
| 45 |
temperature=0.5
|
| 46 |
)
|
| 47 |
|
| 48 |
+
# ----- Image Generation via HF Inference API -----
|
| 49 |
+
@tool
|
| 50 |
+
def generate_image(prompt: str) -> AgentImage:
|
| 51 |
+
"""
|
| 52 |
+
Generates a photorealistic image using the HF Inference API.
|
| 53 |
+
|
| 54 |
+
Args:
|
| 55 |
+
prompt: text prompt to describe the image.
|
| 56 |
+
|
| 57 |
+
Returns:
|
| 58 |
+
AgentImage that Gradio can render.
|
| 59 |
+
"""
|
| 60 |
+
hf_token = os.environ.get("HF_TOKEN")
|
| 61 |
+
if not hf_token:
|
| 62 |
+
raise ValueError("HF_TOKEN not set in Space settings!")
|
| 63 |
+
|
| 64 |
+
# example HF image endpoint
|
| 65 |
+
api_url = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-2-1"
|
| 66 |
+
headers = {"Authorization": f"Bearer {hf_token}"}
|
| 67 |
+
payload = {"inputs": prompt}
|
| 68 |
+
|
| 69 |
+
resp = requests.post(api_url, headers=headers, json=payload)
|
| 70 |
+
resp.raise_for_status()
|
| 71 |
+
|
| 72 |
+
image_bytes = resp.content
|
| 73 |
+
return AgentImage.from_bytes(image_bytes)
|
| 74 |
|
| 75 |
+
# ----- Load Prompt Templates -----
|
|
|
|
|
|
|
| 76 |
with open("prompts.yaml", "r") as f:
|
| 77 |
prompt_templates = yaml.safe_load(f)
|
| 78 |
|
| 79 |
+
# ----- Create Agent -----
|
|
|
|
|
|
|
| 80 |
agent = CodeAgent(
|
| 81 |
model=model,
|
| 82 |
tools=[
|
| 83 |
final_answer,
|
| 84 |
+
generate_image,
|
| 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 |
+
# ----- Launch UI -----
|
|
|
|
|
|
|
| 94 |
GradioUI(agent).launch()
|