Ludo7127 commited on
Commit
5c9c2bd
·
verified ·
1 Parent(s): 3e622c0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -96
app.py CHANGED
@@ -1,13 +1,11 @@
1
  from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
2
  import os
3
- import datetime as dt
4
  import pytz
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
7
  from Gradio_UI import GradioUI
8
  from smolagents.agent_types import AgentImage
9
-
10
- # ---------- Timezone tool ----------
11
  @tool
12
  def get_current_time_in_timezone(timezone: str) -> str:
13
  """Get the current local time for a given timezone.
@@ -25,22 +23,8 @@ def get_current_time_in_timezone(timezone: str) -> str:
25
  except Exception as e:
26
  return f"Error fetching time for timezone '{timezone}': {e}"
27
 
 
28
 
29
- # ---------- Final answer wrapper (image-aware) ----------
30
- class FinalAnswerWithImages(FinalAnswerTool):
31
- def __call__(self, value):
32
- # If any tool returns a smolagents AgentImage, convert to PIL so Gradio can render it
33
- if isinstance(value, AgentImage):
34
- try:
35
- value = value.to_pil()
36
- except Exception:
37
- pass
38
- return super().__call__(value)
39
-
40
- final_answer = FinalAnswerWithImages()
41
-
42
-
43
- # ---------- LLM backend ----------
44
  model = HfApiModel(
45
  max_tokens=2096,
46
  temperature=0.5,
@@ -48,103 +32,33 @@ model = HfApiModel(
48
  custom_role_conversions=None,
49
  )
50
 
51
- # ---------- Hugging Face token for the Hub tool ----------
52
  HF_TOKEN = os.getenv("HF_TOKEN")
53
  assert HF_TOKEN, "HF_TOKEN is not set in the environment."
54
 
55
- # Load the remote image generation tool from the Hub
56
- _hf_image_tool = load_tool(
57
  "agents-course/text-to-image",
58
  trust_remote_code=True,
59
  hf_token=HF_TOKEN,
60
  )
61
 
62
- # ---------- Helpers to normalize return values into a PIL image ----------
63
- def _to_pil(obj):
64
- from PIL import Image
65
- import io, base64, os
66
-
67
- # smolagents AgentImage → PIL
68
- if isinstance(obj, AgentImage) and hasattr(obj, "to_pil"):
69
- return obj.to_pil()
70
-
71
- # Already a PIL image
72
- if hasattr(obj, "size") and callable(getattr(obj, "save", None)):
73
- return obj # looks like a PIL.Image.Image
74
-
75
- # Raw bytes → PIL
76
- if isinstance(obj, (bytes, bytearray)):
77
- return Image.open(io.BytesIO(obj)).convert("RGB")
78
-
79
- # File path → PIL
80
- if isinstance(obj, str) and os.path.exists(obj):
81
- return Image.open(obj).convert("RGB")
82
-
83
- # base64 string → PIL
84
- if isinstance(obj, str):
85
- try:
86
- return Image.open(io.BytesIO(base64.b64decode(obj))).convert("RGB")
87
- except Exception:
88
- pass
89
-
90
- # Dict structures occasionally returned by tools (image / images / data)
91
- if isinstance(obj, dict):
92
- candidates = []
93
- if "image" in obj:
94
- candidates.append(obj["image"])
95
- if "images" in obj and obj["images"]:
96
- candidates.append(obj["images"][0])
97
- if "data" in obj:
98
- candidates.append(obj["data"])
99
- for c in candidates:
100
- try:
101
- return _to_pil(c)
102
- except Exception:
103
- continue
104
-
105
- raise ValueError(f"Unsupported image output type: {type(obj).__name__}")
106
-
107
-
108
- # ---------- Plain Python function the agent can call in python_interpreter ----------
109
- # IMPORTANT: no @tool decorator here.
110
- def generate_image(prompt: str):
111
- """
112
- Generate an image using the HF Hub tool and return a PIL image that Gradio can display.
113
- The agent will call this function from python_interpreter like:
114
- img = generate_image("a photorealistic cat")
115
- final_answer(img)
116
- """
117
- # Most Hub tools accept just 'prompt'; passing extra kwargs can fail silently.
118
- raw = _hf_image_tool(prompt=prompt)
119
- pil = _to_pil(raw)
120
- # Guard against empty images
121
- if getattr(pil, "size", (0, 0)) == (0, 0):
122
- raise RuntimeError("Image generation produced an empty image.")
123
- return pil
124
-
125
-
126
- # (Optional) Also expose a tool version if the agent decides to use a tool instead of Python.
127
- @tool
128
- def make_image(prompt: str):
129
- """Generate an image from text and return a displayable image (PIL)."""
130
- return generate_image(prompt)
131
-
132
 
133
- # ---------- Prompts / Agent / UI ----------
 
 
134
  with open("prompts.yaml", "r") as f:
135
  prompt_templates = yaml.safe_load(f)
136
 
137
  agent = CodeAgent(
138
  model=model,
139
  tools=[
140
- final_answer, # required to end turns
141
- make_image, # tool route (if the LLM picks a tool)
142
  get_current_time_in_timezone,
143
  DuckDuckGoSearchTool(),
144
  ],
145
  max_steps=6,
146
- verbosity_level=2,
147
  prompt_templates=prompt_templates,
148
  )
149
 
150
- GradioUI(agent).launch()
 
1
  from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
2
  import os
3
+ import datetime
4
  import pytz
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
7
  from Gradio_UI import GradioUI
8
  from smolagents.agent_types import AgentImage
 
 
9
  @tool
10
  def get_current_time_in_timezone(timezone: str) -> str:
11
  """Get the current local time for a given timezone.
 
23
  except Exception as e:
24
  return f"Error fetching time for timezone '{timezone}': {e}"
25
 
26
+ final_answer = FinalAnswerTool()
27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  model = HfApiModel(
29
  max_tokens=2096,
30
  temperature=0.5,
 
32
  custom_role_conversions=None,
33
  )
34
 
 
35
  HF_TOKEN = os.getenv("HF_TOKEN")
36
  assert HF_TOKEN, "HF_TOKEN is not set in the environment."
37
 
38
+ image_generation_tool = load_tool(
 
39
  "agents-course/text-to-image",
40
  trust_remote_code=True,
41
  hf_token=HF_TOKEN,
42
  )
43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
+ # if isinstance(final_answer, AgentImage):
46
+ # output = output.to_pil() # Or output.save("cat.png")
47
+
48
  with open("prompts.yaml", "r") as f:
49
  prompt_templates = yaml.safe_load(f)
50
 
51
  agent = CodeAgent(
52
  model=model,
53
  tools=[
54
+ final_answer,
55
+ image_generation_tool,
56
  get_current_time_in_timezone,
57
  DuckDuckGoSearchTool(),
58
  ],
59
  max_steps=6,
60
+ verbosity_level=2, # show tool traces
61
  prompt_templates=prompt_templates,
62
  )
63
 
64
+ GradioUI(agent).launch()