nharshavardhana commited on
Commit
0ea7098
Β·
1 Parent(s): fca100e
Files changed (1) hide show
  1. app.py +18 -17
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
- # Create output folder if not exists
257
- image_path = "/tmp/level.png"
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
- # Add emoji/text labels for key tiles
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", # or "🏁"
275
- "G": "G", # or "🎯"
276
- "E": "⚠️", # or "E"
277
- "T": "πŸ’£", # or "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 # βœ… Return file path to be used in gr.Image(type="filepath")
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: /tmp/level.png"
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 # just get the final chunk
328
 
329
  final_messages = chunk["supervisor"]["messages"]
330
 
331
- # Search the last messages for image path like /tmp/level.png
332
  for message in reversed(final_messages):
333
  if hasattr(message, "content") and isinstance(message.content, str):
334
- match = re.search(r"(/tmp/level\.png)", message.content)
335
  if match:
336
  image_path = match.group(1)
337
  break
338
 
339
- # βœ… Copy image to static/ and return filepath
340
  if image_path and os.path.exists(image_path):
341
- os.makedirs("static", exist_ok=True)
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