Commit
Β·
0ea7098
1
Parent(s):
fca100e
commit
Browse files
app.py
CHANGED
@@ -239,6 +239,9 @@ def render_grid_image(grid: List[List[str]]) -> str:
|
|
239 |
import os
|
240 |
from matplotlib.colors import ListedColormap
|
241 |
|
|
|
|
|
|
|
242 |
# Mapping from tile type to index
|
243 |
tile_map = {"P": 0, "W": 1, "E": 2, "T": 3, "S": 4, "G": 5}
|
244 |
colors = [
|
@@ -253,9 +256,10 @@ def render_grid_image(grid: List[List[str]]) -> str:
|
|
253 |
|
254 |
numeric_grid = np.array([[tile_map[cell] for cell in row] for row in grid])
|
255 |
|
256 |
-
#
|
257 |
-
image_path = "/
|
258 |
|
|
|
259 |
fig, ax = plt.subplots(figsize=(6, 6))
|
260 |
ax.imshow(numeric_grid, cmap=color_map, interpolation='none')
|
261 |
|
@@ -265,16 +269,16 @@ def render_grid_image(grid: List[List[str]]) -> str:
|
|
265 |
ax.grid(which='minor', color='black', linewidth=0.5)
|
266 |
ax.tick_params(which='both', bottom=False, left=False, labelbottom=False, labelleft=False)
|
267 |
|
268 |
-
#
|
269 |
for i in range(len(grid)):
|
270 |
for j in range(len(grid[0])):
|
271 |
cell = grid[i][j]
|
272 |
if cell in ["S", "G", "E", "T"]:
|
273 |
label = {
|
274 |
-
"S": "S",
|
275 |
-
"G": "G",
|
276 |
-
"E": "β οΈ",
|
277 |
-
"T": "π£",
|
278 |
}.get(cell, "")
|
279 |
ax.text(j, i, label, ha='center', va='center', fontsize=12, color='black')
|
280 |
|
@@ -282,7 +286,7 @@ def render_grid_image(grid: List[List[str]]) -> str:
|
|
282 |
plt.savefig(image_path, bbox_inches="tight", dpi=150)
|
283 |
plt.close()
|
284 |
|
285 |
-
return image_path # β
|
286 |
|
287 |
|
288 |
|
@@ -294,7 +298,7 @@ render_image_agent = create_react_agent(
|
|
294 |
"- Only use the render_grid_image tool to render the grid.\n"
|
295 |
"- When responding, ONLY return the raw file path returned by the tool.\n"
|
296 |
"- Do NOT add any commentary or formatting.\n"
|
297 |
-
"- Example: /
|
298 |
),
|
299 |
name="render_image_agent"
|
300 |
)
|
@@ -324,24 +328,21 @@ def run_streamed_supervisor(user_input):
|
|
324 |
{"role": "user", "content": user_input}
|
325 |
]
|
326 |
}):
|
327 |
-
pass #
|
328 |
|
329 |
final_messages = chunk["supervisor"]["messages"]
|
330 |
|
331 |
-
#
|
332 |
for message in reversed(final_messages):
|
333 |
if hasattr(message, "content") and isinstance(message.content, str):
|
334 |
-
match = re.search(r"(/
|
335 |
if match:
|
336 |
image_path = match.group(1)
|
337 |
break
|
338 |
|
339 |
-
# β
|
340 |
if image_path and os.path.exists(image_path):
|
341 |
-
|
342 |
-
static_path = os.path.join("static", os.path.basename(image_path))
|
343 |
-
shutil.copy(image_path, static_path)
|
344 |
-
return static_path # βοΈ Gradio will render it from here
|
345 |
|
346 |
return "β Image path not found or file does not exist."
|
347 |
|
|
|
239 |
import os
|
240 |
from matplotlib.colors import ListedColormap
|
241 |
|
242 |
+
# Ensure the static folder exists
|
243 |
+
os.makedirs("static", exist_ok=True)
|
244 |
+
|
245 |
# Mapping from tile type to index
|
246 |
tile_map = {"P": 0, "W": 1, "E": 2, "T": 3, "S": 4, "G": 5}
|
247 |
colors = [
|
|
|
256 |
|
257 |
numeric_grid = np.array([[tile_map[cell] for cell in row] for row in grid])
|
258 |
|
259 |
+
# Save path
|
260 |
+
image_path = "static/level.png"
|
261 |
|
262 |
+
# Render image
|
263 |
fig, ax = plt.subplots(figsize=(6, 6))
|
264 |
ax.imshow(numeric_grid, cmap=color_map, interpolation='none')
|
265 |
|
|
|
269 |
ax.grid(which='minor', color='black', linewidth=0.5)
|
270 |
ax.tick_params(which='both', bottom=False, left=False, labelbottom=False, labelleft=False)
|
271 |
|
272 |
+
# Emoji/text labels for key tiles
|
273 |
for i in range(len(grid)):
|
274 |
for j in range(len(grid[0])):
|
275 |
cell = grid[i][j]
|
276 |
if cell in ["S", "G", "E", "T"]:
|
277 |
label = {
|
278 |
+
"S": "S", # Start
|
279 |
+
"G": "G", # Goal
|
280 |
+
"E": "β οΈ", # Enemy
|
281 |
+
"T": "π£", # Trap
|
282 |
}.get(cell, "")
|
283 |
ax.text(j, i, label, ha='center', va='center', fontsize=12, color='black')
|
284 |
|
|
|
286 |
plt.savefig(image_path, bbox_inches="tight", dpi=150)
|
287 |
plt.close()
|
288 |
|
289 |
+
return image_path # β
Serve this in gr.Image(type="filepath")
|
290 |
|
291 |
|
292 |
|
|
|
298 |
"- Only use the render_grid_image tool to render the grid.\n"
|
299 |
"- When responding, ONLY return the raw file path returned by the tool.\n"
|
300 |
"- Do NOT add any commentary or formatting.\n"
|
301 |
+
"- Example: static/level.png"
|
302 |
),
|
303 |
name="render_image_agent"
|
304 |
)
|
|
|
328 |
{"role": "user", "content": user_input}
|
329 |
]
|
330 |
}):
|
331 |
+
pass # Wait for final chunk
|
332 |
|
333 |
final_messages = chunk["supervisor"]["messages"]
|
334 |
|
335 |
+
# Look for static image path in last messages
|
336 |
for message in reversed(final_messages):
|
337 |
if hasattr(message, "content") and isinstance(message.content, str):
|
338 |
+
match = re.search(r"(static/level\.png)", message.content)
|
339 |
if match:
|
340 |
image_path = match.group(1)
|
341 |
break
|
342 |
|
343 |
+
# β
Return static file path directly (already saved by render_grid_image)
|
344 |
if image_path and os.path.exists(image_path):
|
345 |
+
return image_path # Gradio will use it directly
|
|
|
|
|
|
|
346 |
|
347 |
return "β Image path not found or file does not exist."
|
348 |
|