Spaces:
Runtime error
Runtime error
Commit
·
3f2b048
1
Parent(s):
c934a84
Minor modifications
Browse files- README.md +32 -1
- agent.py +10 -1
- app.py +5 -2
- chess_tool.py +8 -3
- graph.md +26 -0
README.md
CHANGED
|
@@ -12,4 +12,35 @@ license: mit
|
|
| 12 |
short_description: Final Assignment for agents course
|
| 13 |
---
|
| 14 |
|
| 15 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
short_description: Final Assignment for agents course
|
| 13 |
---
|
| 14 |
|
| 15 |
+
<!--- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference --->
|
| 16 |
+
|
| 17 |
+
### Graph
|
| 18 |
+
|
| 19 |
+
The graph is built with LangGraph framework. There is a control of iterations between the agent and the tools node, with a field in the state. The **route_tools** function checks if the **iterations** number, updated in the **increase** node, is greater than the stablished in MAX_ITERATIONS constant.
|
| 20 |
+
|
| 21 |
+
```mermaid
|
| 22 |
+
---
|
| 23 |
+
config:
|
| 24 |
+
flowchart:
|
| 25 |
+
curve: linear
|
| 26 |
+
---
|
| 27 |
+
graph TD;
|
| 28 |
+
__start__([<p>__start__</p>]):::first
|
| 29 |
+
input(input)
|
| 30 |
+
agent(agent)
|
| 31 |
+
increase(increase)
|
| 32 |
+
tools(tools)
|
| 33 |
+
final_output(final_output)
|
| 34 |
+
__end__([<p>__end__</p>]):::last
|
| 35 |
+
__start__ --> input;
|
| 36 |
+
agent -. final_output .-> final_output;
|
| 37 |
+
agent -. tools .-> increase;
|
| 38 |
+
increase --> tools;
|
| 39 |
+
input --> agent;
|
| 40 |
+
tools --> agent;
|
| 41 |
+
final_output --> __end__;
|
| 42 |
+
classDef default fill:#f2f0ff,line-height:1.2
|
| 43 |
+
classDef first fill-opacity:0
|
| 44 |
+
classDef last fill:#bfb6fc
|
| 45 |
+
|
| 46 |
+
```
|
agent.py
CHANGED
|
@@ -17,7 +17,11 @@ from tools import (
|
|
| 17 |
)
|
| 18 |
from chess_tool import chess_tool
|
| 19 |
|
|
|
|
|
|
|
|
|
|
| 20 |
MAX_ITERATIONS = 5
|
|
|
|
| 21 |
SYSTEM_PROMPT = \
|
| 22 |
"""You are a general AI assistant. This is a GAIA problem to solve, be succinct in your answer.
|
| 23 |
YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings.
|
|
@@ -49,7 +53,12 @@ llm_openai = ChatOpenAI(
|
|
| 49 |
base_url="https://router.huggingface.co/v1",
|
| 50 |
)
|
| 51 |
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
tools = [python_tool,
|
| 55 |
reverse_tool,
|
|
|
|
| 17 |
)
|
| 18 |
from chess_tool import chess_tool
|
| 19 |
|
| 20 |
+
MODEL_PROVIDER = "gemini"
|
| 21 |
+
# MODEL_PROVIDER = "openai"
|
| 22 |
+
|
| 23 |
MAX_ITERATIONS = 5
|
| 24 |
+
|
| 25 |
SYSTEM_PROMPT = \
|
| 26 |
"""You are a general AI assistant. This is a GAIA problem to solve, be succinct in your answer.
|
| 27 |
YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings.
|
|
|
|
| 53 |
base_url="https://router.huggingface.co/v1",
|
| 54 |
)
|
| 55 |
|
| 56 |
+
if MODEL_PROVIDER == "gemini":
|
| 57 |
+
llm = llm_openai
|
| 58 |
+
elif MODEL_PROVIDER == "openai":
|
| 59 |
+
llm = llm_openai
|
| 60 |
+
else:
|
| 61 |
+
raise ValueError(f"Unsupported MODEL_PROVIDER: {MODEL_PROVIDER}")
|
| 62 |
|
| 63 |
tools = [python_tool,
|
| 64 |
reverse_tool,
|
app.py
CHANGED
|
@@ -5,7 +5,10 @@ import inspect
|
|
| 5 |
import pandas as pd
|
| 6 |
import json
|
| 7 |
from logging_config import logger # Import the shared logger
|
|
|
|
|
|
|
| 8 |
|
|
|
|
| 9 |
# (Keep Constants as is)
|
| 10 |
# --- Constants ---
|
| 11 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
@@ -16,7 +19,6 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 16 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 17 |
class BasicAgent:
|
| 18 |
def __init__(self):
|
| 19 |
-
from agent import graph # Importing here to avoid circular imports
|
| 20 |
print("BasicAgent initialized.")
|
| 21 |
self.graph = graph
|
| 22 |
def __call__(self, item: dict) -> str:
|
|
@@ -53,7 +55,6 @@ def run_all(profile: gr.OAuthProfile | None):
|
|
| 53 |
|
| 54 |
api_url = DEFAULT_API_URL
|
| 55 |
questions_url = f"{api_url}/questions"
|
| 56 |
-
submit_url = f"{api_url}/submit"
|
| 57 |
|
| 58 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 59 |
try:
|
|
@@ -115,6 +116,8 @@ def run_all(profile: gr.OAuthProfile | None):
|
|
| 115 |
|
| 116 |
# 5. Submit
|
| 117 |
def submit_all(profile: gr.OAuthProfile | None):
|
|
|
|
|
|
|
| 118 |
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
| 119 |
try:
|
| 120 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
|
|
|
| 5 |
import pandas as pd
|
| 6 |
import json
|
| 7 |
from logging_config import logger # Import the shared logger
|
| 8 |
+
from dotenv import load_dotenv
|
| 9 |
+
from agent import MODEL_PROVIDER, graph
|
| 10 |
|
| 11 |
+
load_dotenv(".env")
|
| 12 |
# (Keep Constants as is)
|
| 13 |
# --- Constants ---
|
| 14 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
|
| 19 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 20 |
class BasicAgent:
|
| 21 |
def __init__(self):
|
|
|
|
| 22 |
print("BasicAgent initialized.")
|
| 23 |
self.graph = graph
|
| 24 |
def __call__(self, item: dict) -> str:
|
|
|
|
| 55 |
|
| 56 |
api_url = DEFAULT_API_URL
|
| 57 |
questions_url = f"{api_url}/questions"
|
|
|
|
| 58 |
|
| 59 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 60 |
try:
|
|
|
|
| 116 |
|
| 117 |
# 5. Submit
|
| 118 |
def submit_all(profile: gr.OAuthProfile | None):
|
| 119 |
+
api_url = DEFAULT_API_URL
|
| 120 |
+
submit_url = f"{api_url}/submit"
|
| 121 |
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
| 122 |
try:
|
| 123 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
chess_tool.py
CHANGED
|
@@ -5,12 +5,16 @@ from logging_config import logger # Import the shared logger
|
|
| 5 |
|
| 6 |
# Adapted from Alberto Formaggio's GAIA agents
|
| 7 |
# https://github.com/AlbertoFormaggio1/gaia-ai-agents/blob/main/tools/chess_tool.py
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
@tool
|
| 10 |
def chess_tool(task_id: str, color_to_move: str) -> str:
|
| 11 |
"""
|
| 12 |
Given an image of a chessboard, and the color to move,
|
| 13 |
predict the FEN notation and suggest the best move.
|
|
|
|
| 14 |
|
| 15 |
Args:
|
| 16 |
task_id (str): The identifier for the chessboard image.
|
|
@@ -27,12 +31,13 @@ def chess_tool(task_id: str, color_to_move: str) -> str:
|
|
| 27 |
predicted_fen = predict_fen(f'./files/{task_id}.png', output_type='simple')
|
| 28 |
|
| 29 |
if color_to_move == 'b':
|
| 30 |
-
|
|
|
|
| 31 |
board = '/'.join([row[::-1] for row in rows])
|
| 32 |
elif color_to_move == 'w':
|
| 33 |
board = predicted_fen
|
| 34 |
|
| 35 |
-
fen = f"{board} {color_to_move} -- 0 1"
|
| 36 |
|
| 37 |
# Initialize Stockfish engine (ensure the path to executable is correct)
|
| 38 |
stockfish = Stockfish(path="/home/jmlv/engines/stockfish/stockfish")
|
|
@@ -40,4 +45,4 @@ def chess_tool(task_id: str, color_to_move: str) -> str:
|
|
| 40 |
best_move = stockfish.get_best_move()
|
| 41 |
logger.info(f"Best move determined: {best_move!r}")
|
| 42 |
|
| 43 |
-
return best_move
|
|
|
|
| 5 |
|
| 6 |
# Adapted from Alberto Formaggio's GAIA agents
|
| 7 |
# https://github.com/AlbertoFormaggio1/gaia-ai-agents/blob/main/tools/chess_tool.py
|
| 8 |
+
# This tool requires the Stockfish chess engine to be installed and accessible:
|
| 9 |
+
# not possible in HuggingFace Spaces due to system-level installation requirements.
|
| 10 |
+
# You can run it locally or in an environment where you can install Stockfish.
|
| 11 |
|
| 12 |
@tool
|
| 13 |
def chess_tool(task_id: str, color_to_move: str) -> str:
|
| 14 |
"""
|
| 15 |
Given an image of a chessboard, and the color to move,
|
| 16 |
predict the FEN notation and suggest the best move.
|
| 17 |
+
https://en.wikipedia.org/wiki/Forsyth%E2%80%93Edwards_Notation
|
| 18 |
|
| 19 |
Args:
|
| 20 |
task_id (str): The identifier for the chessboard image.
|
|
|
|
| 31 |
predicted_fen = predict_fen(f'./files/{task_id}.png', output_type='simple')
|
| 32 |
|
| 33 |
if color_to_move == 'b':
|
| 34 |
+
# if the color to move is black, we need to flip the board
|
| 35 |
+
rows = reversed(predicted_fen.split('/'))#type: ignore
|
| 36 |
board = '/'.join([row[::-1] for row in rows])
|
| 37 |
elif color_to_move == 'w':
|
| 38 |
board = predicted_fen
|
| 39 |
|
| 40 |
+
fen = f"{board} {color_to_move} -- 0 1" #type: ignore
|
| 41 |
|
| 42 |
# Initialize Stockfish engine (ensure the path to executable is correct)
|
| 43 |
stockfish = Stockfish(path="/home/jmlv/engines/stockfish/stockfish")
|
|
|
|
| 45 |
best_move = stockfish.get_best_move()
|
| 46 |
logger.info(f"Best move determined: {best_move!r}")
|
| 47 |
|
| 48 |
+
return best_move #type: ignore
|
graph.md
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
```mermaid
|
| 2 |
+
---
|
| 3 |
+
config:
|
| 4 |
+
flowchart:
|
| 5 |
+
curve: linear
|
| 6 |
+
---
|
| 7 |
+
graph TD;
|
| 8 |
+
__start__([<p>__start__</p>]):::first
|
| 9 |
+
input(input)
|
| 10 |
+
agent(agent)
|
| 11 |
+
increase(increase)
|
| 12 |
+
tools(tools)
|
| 13 |
+
final_output(final_output)
|
| 14 |
+
__end__([<p>__end__</p>]):::last
|
| 15 |
+
__start__ --> input;
|
| 16 |
+
agent -. final_output .-> final_output;
|
| 17 |
+
agent -. tools .-> increase;
|
| 18 |
+
increase --> tools;
|
| 19 |
+
input --> agent;
|
| 20 |
+
tools --> agent;
|
| 21 |
+
final_output --> __end__;
|
| 22 |
+
classDef default fill:#f2f0ff,line-height:1.2
|
| 23 |
+
classDef first fill-opacity:0
|
| 24 |
+
classDef last fill:#bfb6fc
|
| 25 |
+
|
| 26 |
+
```
|