Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,69 +1,174 @@
|
|
| 1 |
-
|
| 2 |
import datetime
|
| 3 |
-
import requests
|
| 4 |
-
import pytz
|
| 5 |
import yaml
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 11 |
@tool
|
| 12 |
-
def my_custom_tool(arg1:str, arg2:int)-> str:
|
| 13 |
-
|
| 14 |
-
"""A tool that does nothing yet
|
| 15 |
Args:
|
| 16 |
arg1: the first argument
|
| 17 |
arg2: the second argument
|
| 18 |
"""
|
| 19 |
-
return "
|
| 20 |
|
| 21 |
@tool
|
| 22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 23 |
-
"""
|
| 24 |
Args:
|
| 25 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 26 |
"""
|
| 27 |
try:
|
| 28 |
-
# Create timezone object
|
| 29 |
tz = pytz.timezone(timezone)
|
| 30 |
-
# Get current time in that timezone
|
| 31 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 32 |
return f"The current local time in {timezone} is: {local_time}"
|
| 33 |
except Exception as e:
|
| 34 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 35 |
|
| 36 |
-
|
| 37 |
final_answer = FinalAnswerTool()
|
| 38 |
|
| 39 |
-
#
|
| 40 |
-
#
|
| 41 |
-
|
|
|
|
| 42 |
model = HfApiModel(
|
| 43 |
-
max_tokens=2096,
|
| 44 |
-
temperature=0.5,
|
| 45 |
-
model_id=
|
| 46 |
-
custom_role_conversions=None,
|
| 47 |
)
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
| 54 |
prompt_templates = yaml.safe_load(stream)
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
agent = CodeAgent(
|
| 57 |
model=model,
|
| 58 |
-
tools=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
max_steps=6,
|
| 60 |
verbosity_level=1,
|
| 61 |
grammar=None,
|
| 62 |
planning_interval=None,
|
| 63 |
name=None,
|
| 64 |
description=None,
|
| 65 |
-
prompt_templates=prompt_templates
|
| 66 |
)
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
import datetime
|
|
|
|
|
|
|
| 3 |
import yaml
|
| 4 |
+
import pytz
|
| 5 |
+
|
| 6 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
| 7 |
+
from tools.final_answer import FinalAnswerTool # keep your existing file
|
| 8 |
+
from Gradio_UI import GradioUI # our UI that unwraps FinalAnswerStep and shows images
|
| 9 |
|
| 10 |
+
# ---------------------------
|
| 11 |
+
# Tools
|
| 12 |
+
# ---------------------------
|
| 13 |
|
|
|
|
| 14 |
@tool
|
| 15 |
+
def my_custom_tool(arg1: str, arg2: int) -> str:
|
| 16 |
+
"""A toy tool that echoes args.
|
|
|
|
| 17 |
Args:
|
| 18 |
arg1: the first argument
|
| 19 |
arg2: the second argument
|
| 20 |
"""
|
| 21 |
+
return f"my_custom_tool got: arg1={arg1}, arg2={arg2}"
|
| 22 |
|
| 23 |
@tool
|
| 24 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 25 |
+
"""Fetch the current local time in a specified timezone.
|
| 26 |
Args:
|
| 27 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 28 |
"""
|
| 29 |
try:
|
|
|
|
| 30 |
tz = pytz.timezone(timezone)
|
|
|
|
| 31 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 32 |
return f"The current local time in {timezone} is: {local_time}"
|
| 33 |
except Exception as e:
|
| 34 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 35 |
|
|
|
|
| 36 |
final_answer = FinalAnswerTool()
|
| 37 |
|
| 38 |
+
# ---------------------------
|
| 39 |
+
# Model (works on Spaces)
|
| 40 |
+
# ---------------------------
|
| 41 |
+
# If the model is gated or you use a private Inference Endpoint, add a Space secret "HF_TOKEN".
|
| 42 |
model = HfApiModel(
|
| 43 |
+
max_tokens=2096,
|
| 44 |
+
temperature=0.5,
|
| 45 |
+
model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 46 |
+
custom_role_conversions=None,
|
| 47 |
)
|
| 48 |
|
| 49 |
+
# ---------------------------
|
| 50 |
+
# Text-to-image (Hub tool) + wrapper that returns a local file path
|
| 51 |
+
# ---------------------------
|
| 52 |
+
_raw_text_to_image = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 53 |
+
|
| 54 |
+
@tool
|
| 55 |
+
def generate_image(prompt: str) -> str:
|
| 56 |
+
"""Generate an image from text and save it locally. Always returns `IMAGE:<abs_path>`.
|
| 57 |
+
Args:
|
| 58 |
+
prompt: The image description to generate.
|
| 59 |
+
"""
|
| 60 |
+
result = _raw_text_to_image(prompt=prompt)
|
| 61 |
+
|
| 62 |
+
import base64, io
|
| 63 |
+
from PIL import Image, ImageDraw
|
| 64 |
+
|
| 65 |
+
out_dir = os.path.abspath("generated_images")
|
| 66 |
+
os.makedirs(out_dir, exist_ok=True)
|
| 67 |
+
out_path = os.path.join(out_dir, f"img_{int(datetime.datetime.now().timestamp())}.png")
|
| 68 |
+
|
| 69 |
+
def _save_pil(img):
|
| 70 |
+
img.save(out_path)
|
| 71 |
+
return out_path
|
| 72 |
|
| 73 |
+
try:
|
| 74 |
+
# PIL Image?
|
| 75 |
+
if "PIL.Image" in str(type(result)):
|
| 76 |
+
return f"IMAGE:{_save_pil(result)}"
|
| 77 |
+
|
| 78 |
+
# dict variants seen in the wild
|
| 79 |
+
if isinstance(result, dict):
|
| 80 |
+
for key in ["image", "pil_image"]:
|
| 81 |
+
if key in result and "PIL.Image" in str(type(result[key])):
|
| 82 |
+
return f"IMAGE:{_save_pil(result[key])}"
|
| 83 |
+
for key in ["image_base64", "b64", "base64"]:
|
| 84 |
+
if key in result and isinstance(result[key], str):
|
| 85 |
+
data = base64.b64decode(result[key], validate=False)
|
| 86 |
+
Image.open(io.BytesIO(data)).save(out_path)
|
| 87 |
+
return f"IMAGE:{out_path}"
|
| 88 |
+
for key in ["path", "image_path"]:
|
| 89 |
+
if key in result and isinstance(result[key], str):
|
| 90 |
+
p = result[key]
|
| 91 |
+
if os.path.isfile(p):
|
| 92 |
+
return f"IMAGE:{os.path.abspath(p)}"
|
| 93 |
+
try:
|
| 94 |
+
Image.open(p).save(out_path)
|
| 95 |
+
return f"IMAGE:{out_path}"
|
| 96 |
+
except Exception:
|
| 97 |
+
pass
|
| 98 |
+
|
| 99 |
+
# raw bytes
|
| 100 |
+
if isinstance(result, (bytes, bytearray)):
|
| 101 |
+
Image.open(io.BytesIO(result)).save(out_path)
|
| 102 |
+
return f"IMAGE:{out_path}"
|
| 103 |
+
|
| 104 |
+
# string: file path or base64 or text
|
| 105 |
+
if isinstance(result, str):
|
| 106 |
+
if os.path.isfile(result):
|
| 107 |
+
return f"IMAGE:{os.path.abspath(result)}"
|
| 108 |
+
try:
|
| 109 |
+
data = base64.b64decode(result, validate=False)
|
| 110 |
+
Image.open(io.BytesIO(data)).save(out_path)
|
| 111 |
+
return f"IMAGE:{out_path}"
|
| 112 |
+
except Exception:
|
| 113 |
+
img = Image.new("RGB", (1024, 512), color=(245, 245, 245))
|
| 114 |
+
d = ImageDraw.Draw(img)
|
| 115 |
+
d.multiline_text((20, 20), result[:4000], fill=(0, 0, 0))
|
| 116 |
+
img.save(out_path)
|
| 117 |
+
return f"IMAGE:{out_path}"
|
| 118 |
+
|
| 119 |
+
# ultimate fallback: render the prompt as an image
|
| 120 |
+
img = Image.new("RGB", (1024, 512), color=(245, 245, 245))
|
| 121 |
+
d = ImageDraw.Draw(img)
|
| 122 |
+
d.multiline_text((20, 20), prompt, fill=(0, 0, 0))
|
| 123 |
+
img.save(out_path)
|
| 124 |
+
return f"IMAGE:{out_path}"
|
| 125 |
+
|
| 126 |
+
except Exception as e:
|
| 127 |
+
return f"Error generating image: {e}"
|
| 128 |
+
|
| 129 |
+
search = DuckDuckGoSearchTool()
|
| 130 |
|
| 131 |
+
# ---------------------------
|
| 132 |
+
# Prompts
|
| 133 |
+
# ---------------------------
|
| 134 |
+
with open("prompts.yaml", "r", encoding="utf-8") as stream:
|
| 135 |
prompt_templates = yaml.safe_load(stream)
|
| 136 |
+
|
| 137 |
+
IMAGE_RULE = (
|
| 138 |
+
"If you generate an image using the `generate_image` tool, "
|
| 139 |
+
"include the exact returned line `IMAGE:<abs_path>` on a new line in your final answer. "
|
| 140 |
+
"Always finish by calling final_answer(<plain text> with any IMAGE lines)."
|
| 141 |
+
)
|
| 142 |
+
if isinstance(prompt_templates, dict):
|
| 143 |
+
if "rules" in prompt_templates and isinstance(prompt_templates["rules"], str):
|
| 144 |
+
prompt_templates["rules"] += "\n" + IMAGE_RULE
|
| 145 |
+
else:
|
| 146 |
+
prompt_templates["rules"] = IMAGE_RULE
|
| 147 |
+
|
| 148 |
+
# ---------------------------
|
| 149 |
+
# Agent
|
| 150 |
+
# ---------------------------
|
| 151 |
agent = CodeAgent(
|
| 152 |
model=model,
|
| 153 |
+
tools=[
|
| 154 |
+
final_answer,
|
| 155 |
+
get_current_time_in_timezone,
|
| 156 |
+
my_custom_tool,
|
| 157 |
+
search,
|
| 158 |
+
generate_image,
|
| 159 |
+
],
|
| 160 |
max_steps=6,
|
| 161 |
verbosity_level=1,
|
| 162 |
grammar=None,
|
| 163 |
planning_interval=None,
|
| 164 |
name=None,
|
| 165 |
description=None,
|
| 166 |
+
prompt_templates=prompt_templates,
|
| 167 |
)
|
| 168 |
|
| 169 |
+
# ---------------------------
|
| 170 |
+
# Launch Gradio (Spaces-friendly)
|
| 171 |
+
# ---------------------------
|
| 172 |
+
if __name__ == "__main__":
|
| 173 |
+
# Gradio will bind correctly on Spaces; no need to set host/port manually.
|
| 174 |
+
GradioUI(agent).launch()
|