Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -22,13 +22,6 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 22 |
class BasicAgent:
|
| 23 |
def __init__(self):
|
| 24 |
print("BasicAgent initialized.")
|
| 25 |
-
api_token = os.getenv("HF_TOKEN")
|
| 26 |
-
|
| 27 |
-
os.environ["HUGGINGFACEHUB_API_TOKEN"] = "hf_abcdefghijklmnopqrstuvwxyz"
|
| 28 |
-
login(api_token)
|
| 29 |
-
print(f"Using API Token: {api_token}")
|
| 30 |
-
self.model = InferenceClient("mistralai/Mistral-7B-Instruct-v0.1")
|
| 31 |
-
self.tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.1") # Initialize tokenizer
|
| 32 |
SYSTEM_PROMPT = """You are a general AI assistant. I will ask you a question. Report your thoughts, and
|
| 33 |
finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER].
|
| 34 |
YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated
|
|
@@ -44,7 +37,12 @@ class BasicAgent:
|
|
| 44 |
|
| 45 |
def maybe_reversed(self, text: str) -> bool:
|
| 46 |
words = text.split()
|
| 47 |
-
reversed_ratio = sum(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
return reversed_ratio > 0.3
|
| 49 |
|
| 50 |
def solve_riddle(self, question: str) -> str:
|
|
@@ -53,60 +51,30 @@ class BasicAgent:
|
|
| 53 |
match = re.search(r"opposite of the word ['\"](\w+)['\"]", reversed_text)
|
| 54 |
if match:
|
| 55 |
word = match.group(1).lower()
|
| 56 |
-
opposites = {
|
|
|
|
|
|
|
|
|
|
| 57 |
opposite = opposites.get(word, f"UNKNOWN_OPPOSITE_OF_{word}")
|
| 58 |
return f"FINAL ANSWER: {opposite}"
|
| 59 |
-
return
|
| 60 |
-
|
| 61 |
-
def is_chess_question(self, question: str) -> bool:
|
| 62 |
-
keywords = ["chess", "algebraic notation", "checkmate", "bishop", "rook", "knight", "position", "board"]
|
| 63 |
-
return any(k in question.lower() for k in keywords)
|
| 64 |
-
|
| 65 |
-
def extract_board_from_image(self, image_data: str) -> chess.Board:
|
| 66 |
-
# Stub implementation; replace with real image-to-FEN logic
|
| 67 |
-
# For now, simulate board with forced mate
|
| 68 |
-
board = chess.Board()
|
| 69 |
-
board.set_fen("6k1/5ppp/8/8/8/8/5PPP/6K1 b - - 0 1") # Example stub position
|
| 70 |
-
return board
|
| 71 |
-
|
| 72 |
-
def find_best_move(self, board: chess.Board) -> str:
|
| 73 |
-
try:
|
| 74 |
-
with chess.engine.SimpleEngine.popen_uci("/usr/games/stockfish") as engine:
|
| 75 |
-
result = engine.play(board, chess.engine.Limit(time=0.1))
|
| 76 |
-
return f"FINAL ANSWER: {board.san(result.move)}"
|
| 77 |
-
except Exception as e:
|
| 78 |
-
return f"FINAL ANSWER: ENGINE_ERROR: {e}"
|
| 79 |
|
| 80 |
def __call__(self, question: str) -> str:
|
| 81 |
-
print(f"
|
| 82 |
-
|
| 83 |
-
# Tokenize input question using tokenizer
|
| 84 |
-
inputs = self.tokenizer(question, return_tensors="pt", truncation=True, padding=True, max_length=512)
|
| 85 |
-
|
| 86 |
-
if self.is_chess_question(question):
|
| 87 |
-
print("Detected chess-related question. Attempting board extraction...")
|
| 88 |
-
board = self.extract_board_from_image("") # Replace with actual image extraction logic
|
| 89 |
-
return self.find_best_move(board)
|
| 90 |
-
|
| 91 |
if self.maybe_reversed(question):
|
| 92 |
-
print("Detected likely reversed
|
| 93 |
return self.solve_riddle(question)
|
|
|
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
and displays the results.
|
| 105 |
-
"""
|
| 106 |
-
# --- Determine HF Space Runtime URL and Repo URL ---
|
| 107 |
-
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
| 108 |
-
|
| 109 |
-
if profile:
|
| 110 |
username= f"{profile.username}"
|
| 111 |
print(f"User logged in: {username}")
|
| 112 |
else:
|
|
|
|
| 22 |
class BasicAgent:
|
| 23 |
def __init__(self):
|
| 24 |
print("BasicAgent initialized.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
SYSTEM_PROMPT = """You are a general AI assistant. I will ask you a question. Report your thoughts, and
|
| 26 |
finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER].
|
| 27 |
YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated
|
|
|
|
| 37 |
|
| 38 |
def maybe_reversed(self, text: str) -> bool:
|
| 39 |
words = text.split()
|
| 40 |
+
reversed_ratio = sum(
|
| 41 |
+
1 for word in words if word[::-1].lower() in {
|
| 42 |
+
"if", "you", "understand", "this", "sentence", "write",
|
| 43 |
+
"opposite", "of", "the", "word", "left", "answer"
|
| 44 |
+
}
|
| 45 |
+
) / len(words)
|
| 46 |
return reversed_ratio > 0.3
|
| 47 |
|
| 48 |
def solve_riddle(self, question: str) -> str:
|
|
|
|
| 51 |
match = re.search(r"opposite of the word ['\"](\w+)['\"]", reversed_text)
|
| 52 |
if match:
|
| 53 |
word = match.group(1).lower()
|
| 54 |
+
opposites = {
|
| 55 |
+
"left": "right", "up": "down", "hot": "cold",
|
| 56 |
+
"true": "false", "yes": "no", "black": "white"
|
| 57 |
+
}
|
| 58 |
opposite = opposites.get(word, f"UNKNOWN_OPPOSITE_OF_{word}")
|
| 59 |
return f"FINAL ANSWER: {opposite}"
|
| 60 |
+
return "FINAL ANSWER: COULD_NOT_SOLVE"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
def __call__(self, question: str) -> str:
|
| 63 |
+
print(f"Received question: {question[:50]}...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
if self.maybe_reversed(question):
|
| 65 |
+
print("Detected likely reversed riddle.")
|
| 66 |
return self.solve_riddle(question)
|
| 67 |
+
return "FINAL ANSWER: NOT_A_RIDDLE"
|
| 68 |
|
| 69 |
+
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 70 |
+
"""
|
| 71 |
+
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 72 |
+
and displays the results.
|
| 73 |
+
"""
|
| 74 |
+
# --- Determine HF Space Runtime URL and Repo URL ---
|
| 75 |
+
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
| 76 |
+
|
| 77 |
+
if profile:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
username= f"{profile.username}"
|
| 79 |
print(f"User logged in: {username}")
|
| 80 |
else:
|