SergeyO7 commited on
Commit
d8d50b0
·
verified ·
1 Parent(s): 0962002

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +36 -62
agent.py CHANGED
@@ -300,73 +300,47 @@ class MagAgent:
300
 
301
 
302
 
303
- class DownloadImageTool(Tool):
304
- name = "download_chess_image"
305
- description = "Downloads chess position image from task ID"
306
- inputs = {'task_id': {'type': 'string'}}
307
- output_type = "string"
308
-
309
- def forward(self, task_id: str) -> str:
310
- try:
311
- response = requests.get(
312
- f"https://agents-course-unit4-scoring.hf.space/files/{task_id}",
313
- stream=True
314
- )
315
- response.raise_for_status()
316
-
317
- img_path = f"chess_{task_id}.png"
318
- with open(img_path, "wb") as f:
319
- for chunk in response.iter_content(8192):
320
- f.write(chunk)
321
- return img_path
322
- except Exception as e:
323
- raise RuntimeError(f"Image download failed: {str(e)}")
324
-
325
-
326
-
327
- class ChessEngineTool(Tool):
328
- import chess
329
- import chess.engine
330
- name = "stockfish_analysis"
331
- description = "Analyzes chess position using Stockfish"
332
- inputs = {'fen': {'type': 'string'}}
333
- output_type = "string"
334
-
335
- def forward(self, fen: str) -> str:
336
- try:
337
- board = chess.Board(fen)
338
- engine = chess.engine.SimpleEngine.popen_uci("stockfish")
339
- result = engine.play(board, chess.engine.Limit(time=2.0))
340
- engine.quit()
341
- return board.san(result.move)
342
- except Exception as e:
343
- return f"Engine error: {str(e)}"
344
-
345
- async def analyze_position(self, task_id: str):
346
  try:
347
- # Step 1: Download image
348
- img_path = await self.tools[0](task_id)
349
-
350
- # Step 2: Get multimodal analysis
351
- response = await self.model.acreate(
352
- messages=[{
353
- "role": "user",
354
- "content": [
355
- {"type": "text", "text": """Analyze this chess position.
356
- It's black's turn. Provide the winning move in algebraic notation.
357
- Respond ONLY with the move, nothing else."""},
358
- {"type": "image_url", "image_url": {"url": f"file://{img_path}"}}
359
- ]
360
- }],
361
- temperature=0.1
362
  )
 
363
 
364
- return response.choices[0].message.content
365
-
 
 
 
366
  except Exception as e:
367
- return f"Analysis failed: {str(e)}"
368
-
369
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
370
 
371
 
372
  class PythonCodeReaderTool(Tool):
 
300
 
301
 
302
 
303
+ class DownloadImageTool(Tool):
304
+ name = "download_chess_image"
305
+ description = "Downloads chess position image from task ID"
306
+ inputs = {'task_id': {'type': 'string'}}
307
+ output_type = "string"
308
+
309
+ def forward(self, task_id: str) -> str:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
310
  try:
311
+ response = requests.get(
312
+ f"https://agents-course-unit4-scoring.hf.space/files/{task_id}",
313
+ stream=True
 
 
 
 
 
 
 
 
 
 
 
 
314
  )
315
+ response.raise_for_status()
316
 
317
+ img_path = f"chess_{task_id}.png"
318
+ with open(img_path, "wb") as f:
319
+ for chunk in response.iter_content(8192):
320
+ f.write(chunk)
321
+ return img_path
322
  except Exception as e:
323
+ raise RuntimeError(f"Image download failed: {str(e)}")
324
+
325
 
326
+ class ChessEngineTool(Tool):
327
+ import chess
328
+ import chess.engine
329
+ name = "stockfish_analysis"
330
+ description = "Analyzes chess position using Stockfish"
331
+ inputs = {'fen': {'type': 'string'}}
332
+ output_type = "string"
333
+
334
+ def forward(self, fen: str) -> str:
335
+ try:
336
+ board = chess.Board(fen)
337
+ engine = chess.engine.SimpleEngine.popen_uci("stockfish")
338
+ result = engine.play(board, chess.engine.Limit(time=2.0))
339
+ engine.quit()
340
+ return board.san(result.move)
341
+ except Exception as e:
342
+ return f"Engine error: {str(e)}"
343
+
344
 
345
 
346
  class PythonCodeReaderTool(Tool):