Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,54 +6,68 @@ from tools.final_answer import FinalAnswerTool
|
|
| 6 |
from Gradio_UI import GradioUI
|
| 7 |
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
@tool
|
| 10 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 11 |
"""
|
| 12 |
-
Get
|
| 13 |
|
| 14 |
Args:
|
| 15 |
-
timezone (str):
|
| 16 |
|
| 17 |
Returns:
|
| 18 |
-
str:
|
| 19 |
"""
|
| 20 |
try:
|
| 21 |
tz = pytz.timezone(timezone)
|
| 22 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 23 |
-
return f"
|
| 24 |
except Exception as e:
|
| 25 |
-
return
|
| 26 |
|
| 27 |
|
| 28 |
-
#
|
| 29 |
final_answer = FinalAnswerTool()
|
| 30 |
|
| 31 |
|
| 32 |
-
# LLM
|
| 33 |
model = HfApiModel(
|
| 34 |
model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 35 |
-
max_tokens=
|
| 36 |
-
temperature=0.5
|
| 37 |
-
)
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
# Image generation tool from Hugging Face Agents course
|
| 41 |
-
image_generation_tool = load_tool(
|
| 42 |
-
"agents-course/text-to-image",
|
| 43 |
-
trust_remote_code=True
|
| 44 |
)
|
| 45 |
|
| 46 |
|
| 47 |
-
#
|
| 48 |
-
with open("prompts.yaml", "r") as
|
| 49 |
-
prompt_templates = yaml.safe_load(
|
| 50 |
|
| 51 |
|
| 52 |
-
#
|
| 53 |
agent = CodeAgent(
|
| 54 |
model=model,
|
| 55 |
tools=[
|
| 56 |
-
|
| 57 |
DuckDuckGoSearchTool(),
|
| 58 |
get_current_time_in_timezone,
|
| 59 |
final_answer
|
|
@@ -61,10 +75,10 @@ agent = CodeAgent(
|
|
| 61 |
max_steps=6,
|
| 62 |
verbosity_level=2,
|
| 63 |
name="multitool_agent",
|
| 64 |
-
description="Agent that can search the web,
|
| 65 |
prompt_templates=prompt_templates
|
| 66 |
)
|
| 67 |
|
| 68 |
|
| 69 |
-
# Launch
|
| 70 |
GradioUI(agent).launch()
|
|
|
|
| 6 |
from Gradio_UI import GradioUI
|
| 7 |
|
| 8 |
|
| 9 |
+
# Load HF image generation tool
|
| 10 |
+
hf_image_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
@tool
|
| 14 |
+
def generate_image(prompt: str) -> str:
|
| 15 |
+
"""
|
| 16 |
+
Generate an image from a text prompt.
|
| 17 |
+
|
| 18 |
+
Args:
|
| 19 |
+
prompt (str): Description of the image.
|
| 20 |
+
|
| 21 |
+
Returns:
|
| 22 |
+
str: File path of the generated image.
|
| 23 |
+
"""
|
| 24 |
+
img = hf_image_tool(prompt=prompt)
|
| 25 |
+
path = "/tmp/generated_image.png"
|
| 26 |
+
img.save(path)
|
| 27 |
+
return path
|
| 28 |
+
|
| 29 |
+
|
| 30 |
@tool
|
| 31 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 32 |
"""
|
| 33 |
+
Get current time for a timezone.
|
| 34 |
|
| 35 |
Args:
|
| 36 |
+
timezone (str): Example 'Asia/Kolkata'.
|
| 37 |
|
| 38 |
Returns:
|
| 39 |
+
str: Current time in that timezone.
|
| 40 |
"""
|
| 41 |
try:
|
| 42 |
tz = pytz.timezone(timezone)
|
| 43 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 44 |
+
return f"Time in {timezone}: {local_time}"
|
| 45 |
except Exception as e:
|
| 46 |
+
return str(e)
|
| 47 |
|
| 48 |
|
| 49 |
+
# Mandatory final answer tool
|
| 50 |
final_answer = FinalAnswerTool()
|
| 51 |
|
| 52 |
|
| 53 |
+
# LLM for reasoning
|
| 54 |
model = HfApiModel(
|
| 55 |
model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 56 |
+
max_tokens=2048,
|
| 57 |
+
temperature=0.5
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
)
|
| 59 |
|
| 60 |
|
| 61 |
+
# Prompt templates
|
| 62 |
+
with open("prompts.yaml", "r") as f:
|
| 63 |
+
prompt_templates = yaml.safe_load(f)
|
| 64 |
|
| 65 |
|
| 66 |
+
# Agent
|
| 67 |
agent = CodeAgent(
|
| 68 |
model=model,
|
| 69 |
tools=[
|
| 70 |
+
generate_image,
|
| 71 |
DuckDuckGoSearchTool(),
|
| 72 |
get_current_time_in_timezone,
|
| 73 |
final_answer
|
|
|
|
| 75 |
max_steps=6,
|
| 76 |
verbosity_level=2,
|
| 77 |
name="multitool_agent",
|
| 78 |
+
description="Agent that can generate images, search the web, and get timezone time.",
|
| 79 |
prompt_templates=prompt_templates
|
| 80 |
)
|
| 81 |
|
| 82 |
|
| 83 |
+
# Launch UI
|
| 84 |
GradioUI(agent).launch()
|