Toumaima commited on
Commit
ff15be3
·
verified ·
1 Parent(s): 272ec1b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -21
app.py CHANGED
@@ -4,6 +4,7 @@ import requests
4
  import inspect
5
  import pandas as pd
6
  from huggingface_hub import InferenceClient
 
7
  import chess
8
  import chess.engine
9
  from PIL import Image
@@ -11,7 +12,6 @@ from io import BytesIO
11
  import base64
12
  import re
13
 
14
- # (Keep Constants and BasicAgent class as is)
15
  # --- Constants ---
16
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
17
 
@@ -24,6 +24,7 @@ class BasicAgent:
24
 
25
  os.environ["HUGGINGFACEHUB_API_TOKEN"] = "hf_abcdefghijklmnopqrstuvwxyz"
26
  self.model = InferenceClient("mistralai/Mistral-7B-Instruct-v0.1")
 
27
  SYSTEM_PROMPT = """You are a general AI assistant. I will ask you a question. Report your thoughts, and
28
  finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER].
29
  YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated
@@ -59,7 +60,7 @@ class BasicAgent:
59
 
60
  def extract_board_from_image(self, image_data: str) -> chess.Board:
61
  # Stub implementation; replace with real image-to-FEN logic
62
- # For now, simulate a board with forced mate
63
  board = chess.Board()
64
  board.set_fen("6k1/5ppp/8/8/8/8/5PPP/6K1 b - - 0 1") # Example stub position
65
  return board
@@ -75,31 +76,22 @@ class BasicAgent:
75
  def __call__(self, question: str) -> str:
76
  print(f"Agent received question (first 50 chars): {question[:50]}...")
77
 
 
 
 
78
  if self.is_chess_question(question):
79
  print("Detected chess-related question. Attempting board extraction...")
80
- # You would extract image info from the question or context here
81
- # For now, we'll simulate board analysis
82
- board = self.extract_board_from_image("") # Replace with actual image
83
  return self.find_best_move(board)
84
 
85
  if self.maybe_reversed(question):
86
  print("Detected likely reversed text. Attempting riddle solving...")
87
  return self.solve_riddle(question)
88
 
89
- final_answer = self.model.chat_completion(question)
90
- print(f"Agent returning final answer: {final_answer}")
91
- return final_answer
92
-
93
- def __call__(self, question: str) -> str:
94
- print(f"Agent received question (first 50 chars): {question[:50]}...")
95
-
96
- if self.maybe_reversed(question):
97
- print("Detected likely reversed text. Attempting riddle solving...")
98
- return self.solve_riddle(question)
99
-
100
- final_answer = self.model.chat_completion(question)
101
- print(f"Agent returning final answer: {final_answer}")
102
- return final_answer
103
 
104
 
105
  def run_and_submit_all( profile: gr.OAuthProfile | None):
@@ -121,13 +113,13 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
121
  questions_url = f"{api_url}/questions"
122
  submit_url = f"{api_url}/submit"
123
 
124
- # 1. Instantiate Agent ( modify this part to create your agent)
125
  try:
126
  agent = BasicAgent()
127
  except Exception as e:
128
  print(f"Error instantiating agent: {e}")
129
  return f"Error initializing agent: {e}", None
130
- # In the case of an app running as a hugging Face space, this link points toward your codebase ( usefull for others so please keep it public)
131
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
132
  print(agent_code)
133
 
 
4
  import inspect
5
  import pandas as pd
6
  from huggingface_hub import InferenceClient
7
+ from transformers import AutoTokenizer # Import AutoTokenizer from transformers
8
  import chess
9
  import chess.engine
10
  from PIL import Image
 
12
  import base64
13
  import re
14
 
 
15
  # --- Constants ---
16
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
17
 
 
24
 
25
  os.environ["HUGGINGFACEHUB_API_TOKEN"] = "hf_abcdefghijklmnopqrstuvwxyz"
26
  self.model = InferenceClient("mistralai/Mistral-7B-Instruct-v0.1")
27
+ self.tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.1") # Initialize tokenizer
28
  SYSTEM_PROMPT = """You are a general AI assistant. I will ask you a question. Report your thoughts, and
29
  finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER].
30
  YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated
 
60
 
61
  def extract_board_from_image(self, image_data: str) -> chess.Board:
62
  # Stub implementation; replace with real image-to-FEN logic
63
+ # For now, simulate board with forced mate
64
  board = chess.Board()
65
  board.set_fen("6k1/5ppp/8/8/8/8/5PPP/6K1 b - - 0 1") # Example stub position
66
  return board
 
76
  def __call__(self, question: str) -> str:
77
  print(f"Agent received question (first 50 chars): {question[:50]}...")
78
 
79
+ # Tokenize input question using tokenizer
80
+ inputs = self.tokenizer(question, return_tensors="pt", truncation=True, padding=True, max_length=512)
81
+
82
  if self.is_chess_question(question):
83
  print("Detected chess-related question. Attempting board extraction...")
84
+ board = self.extract_board_from_image("") # Replace with actual image extraction logic
 
 
85
  return self.find_best_move(board)
86
 
87
  if self.maybe_reversed(question):
88
  print("Detected likely reversed text. Attempting riddle solving...")
89
  return self.solve_riddle(question)
90
 
91
+ # Use the model to generate an answer
92
+ response = self.model.chat_completion(question) # Assuming this works with raw text input
93
+ print(f"Agent returning final answer: {response}")
94
+ return response
 
 
 
 
 
 
 
 
 
 
95
 
96
 
97
  def run_and_submit_all( profile: gr.OAuthProfile | None):
 
113
  questions_url = f"{api_url}/questions"
114
  submit_url = f"{api_url}/submit"
115
 
116
+ # 1. Instantiate Agent (modify this part to create your agent)
117
  try:
118
  agent = BasicAgent()
119
  except Exception as e:
120
  print(f"Error instantiating agent: {e}")
121
  return f"Error initializing agent: {e}", None
122
+ # In the case of an app running as a hugging Face space, this link points toward your codebase (useful for others so please keep it public)
123
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
124
  print(agent_code)
125