Toumaima commited on
Commit
0b759c7
·
verified ·
1 Parent(s): db7f5d2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -127
app.py CHANGED
@@ -1,39 +1,29 @@
1
  import os
2
  import gradio as gr
3
  import requests
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
11
- from io import BytesIO
12
- import base64
13
- import re
14
  from huggingface_hub import login
15
-
16
 
17
  # --- Constants ---
18
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
19
 
20
  # --- Basic Agent Definition ---
21
-
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
28
- list of numbers and/or strings.
29
- If you are asked for a number, don't use comma to write your number neither use units such as $ or
30
- percent sign unless specified otherwise.
31
- If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the
32
- digits in plain text unless specified otherwise.
33
- If you are asked for a comma separated list, apply the above rules depending of whether the element
34
- to be put in the list is a number or a string.
35
- """
36
- self.agent_prompt = SYSTEM_PROMPT
37
 
38
  def maybe_reversed(self, text: str) -> bool:
39
  words = text.split()
@@ -46,9 +36,9 @@ class BasicAgent:
46
  return reversed_ratio > 0.3
47
 
48
  def solve_riddle(self, question: str) -> str:
49
- reversed_text = question[::-1]
50
- if "opposite of the word" in reversed_text:
51
- match = re.search(r"opposite of the word ['\"](\w+)['\"]", reversed_text)
52
  if match:
53
  word = match.group(1).lower()
54
  opposites = {
@@ -65,17 +55,14 @@ class BasicAgent:
65
  print("Detected likely reversed riddle.")
66
  return self.solve_riddle(question)
67
  return "FINAL ANSWER: NOT_A_RIDDLE"
68
- def run_and_submit_all( profile: gr.OAuthProfile | None):
69
- """
70
- Fetches all questions, runs the BasicAgent on them, submits all answers,
71
- and displays the results.
72
- """
73
- # --- Determine HF Space Runtime URL and Repo URL ---
74
- space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
75
 
76
  if profile:
77
- username= f"{profile.username}"
78
- print(f"User logged in: {username}")
79
  else:
80
  print("User not logged in.")
81
  return "Please Login to Hugging Face with the button.", None
@@ -84,66 +71,47 @@ class BasicAgent:
84
  questions_url = f"{api_url}/questions"
85
  submit_url = f"{api_url}/submit"
86
 
87
- # 1. Instantiate Agent ( modify this part to create your agent)
88
  try:
89
  agent = BasicAgent()
90
  except Exception as e:
91
  print(f"Error instantiating agent: {e}")
92
  return f"Error initializing agent: {e}", None
93
- # 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)
94
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
95
  print(agent_code)
96
 
97
- # 2. Fetch Questions
98
- print(f"Fetching questions from: {questions_url}")
99
  try:
100
  response = requests.get(questions_url, timeout=15)
101
  response.raise_for_status()
102
  questions_data = response.json()
103
  if not questions_data:
104
- print("Fetched questions list is empty.")
105
- return "Fetched questions list is empty or invalid format.", None
106
- print(f"Fetched {len(questions_data)} questions.")
107
  except requests.exceptions.RequestException as e:
108
- print(f"Error fetching questions: {e}")
109
  return f"Error fetching questions: {e}", None
110
- except requests.exceptions.JSONDecodeError as e:
111
- print(f"Error decoding JSON response from questions endpoint: {e}")
112
- print(f"Response text: {response.text[:500]}")
113
- return f"Error decoding server response for questions: {e}", None
114
- except Exception as e:
115
- print(f"An unexpected error occurred fetching questions: {e}")
116
- return f"An unexpected error occurred fetching questions: {e}", None
117
 
118
- # 3. Run your Agent
119
  results_log = []
120
  answers_payload = []
121
- print(f"Running agent on {len(questions_data)} questions...")
122
  for item in questions_data:
123
  task_id = item.get("task_id")
124
  question_text = item.get("question")
125
  if not task_id or question_text is None:
126
- print(f"Skipping item with missing task_id or question: {item}")
127
  continue
128
  try:
129
  submitted_answer = agent(question_text)
130
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
131
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
132
  except Exception as e:
133
- print(f"Error running agent on task {task_id}: {e}")
134
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
135
 
136
  if not answers_payload:
137
- print("Agent did not produce any answers to submit.")
138
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
139
 
140
- # 4. Prepare Submission
141
- submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
142
- status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
143
- print(status_update)
 
144
 
145
- # 5. Submit
146
- print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
147
  try:
148
  response = requests.post(submit_url, json=submission_data, timeout=60)
149
  response.raise_for_status()
@@ -155,59 +123,17 @@ class BasicAgent:
155
  f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
156
  f"Message: {result_data.get('message', 'No message received.')}"
157
  )
158
- print("Submission successful.")
159
- results_df = pd.DataFrame(results_log)
160
- return final_status, results_df
161
- except requests.exceptions.HTTPError as e:
162
- error_detail = f"Server responded with status {e.response.status_code}."
163
- try:
164
- error_json = e.response.json()
165
- error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
166
- except requests.exceptions.JSONDecodeError:
167
- error_detail += f" Response: {e.response.text[:500]}"
168
- status_message = f"Submission Failed: {error_detail}"
169
- print(status_message)
170
- results_df = pd.DataFrame(results_log)
171
- return status_message, results_df
172
- except requests.exceptions.Timeout:
173
- status_message = "Submission Failed: The request timed out."
174
- print(status_message)
175
- results_df = pd.DataFrame(results_log)
176
- return status_message, results_df
177
- except requests.exceptions.RequestException as e:
178
- status_message = f"Submission Failed: Network error - {e}"
179
- print(status_message)
180
- results_df = pd.DataFrame(results_log)
181
- return status_message, results_df
182
  except Exception as e:
183
- status_message = f"An unexpected error occurred during submission: {e}"
184
- print(status_message)
185
- results_df = pd.DataFrame(results_log)
186
- return status_message, results_df
187
 
188
 
189
- # --- Build Gradio Interface using Blocks ---
190
  with gr.Blocks() as demo:
191
  gr.Markdown("# Basic Agent Evaluation Runner")
192
- gr.Markdown(
193
- """
194
- **Instructions:**
195
- 1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
196
- 2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
197
- 3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
198
- ---
199
- **Disclaimers:**
200
- Once clicking on the "submit button, it can take quite some time ( this is the time for the agent to go through all the questions).
201
- This space provides a basic setup and is intentionally sub-optimal to encourage you to develop your own, more robust solution. For instance for the delay process of the submit button, a solution could be to cache the answers and submit in a seperate action or even to answer the questions in async.
202
- """
203
- )
204
-
205
  gr.LoginButton()
206
-
207
  run_button = gr.Button("Run Evaluation & Submit All Answers")
208
-
209
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
210
- # Removed max_rows=10 from DataFrame constructor
211
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
212
 
213
  run_button.click(
@@ -216,25 +142,5 @@ with gr.Blocks() as demo:
216
  )
217
 
218
  if __name__ == "__main__":
219
- print("\n" + "-"*30 + " App Starting " + "-"*30)
220
- # Check for SPACE_HOST and SPACE_ID at startup for information
221
- space_host_startup = os.getenv("SPACE_HOST")
222
- space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
223
-
224
- if space_host_startup:
225
- print(f"✅ SPACE_HOST found: {space_host_startup}")
226
- print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
227
- else:
228
- print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
229
-
230
- if space_id_startup: # Print repo URLs if SPACE_ID is found
231
- print(f"✅ SPACE_ID found: {space_id_startup}")
232
- print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
233
- print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
234
- else:
235
- print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
236
-
237
- print("-"*(60 + len(" App Starting ")) + "\n")
238
-
239
  print("Launching Gradio Interface for Basic Agent Evaluation...")
240
  demo.launch(debug=True, share=False)
 
1
  import os
2
  import gradio as gr
3
  import requests
 
4
  import pandas as pd
 
 
 
 
 
 
 
 
5
  from huggingface_hub import login
6
+ import re
7
 
8
  # --- Constants ---
9
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
10
 
11
  # --- Basic Agent Definition ---
 
12
  class BasicAgent:
13
  def __init__(self):
14
  print("BasicAgent initialized.")
15
+ self.agent_prompt = (
16
+ """You are a general AI assistant. I will ask you a question. Report your thoughts, and
17
+ finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER].
18
+ YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated
19
+ list of numbers and/or strings.
20
+ If you are asked for a number, don't use comma to write your number neither use units such as $
21
+ or percent sign unless specified otherwise.
22
+ If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the
23
+ digits in plain text unless specified otherwise.
24
+ If you are asked for a comma separated list, apply the above rules depending of whether the element
25
+ to be put in the list is a number or a string."""
26
+ )
27
 
28
  def maybe_reversed(self, text: str) -> bool:
29
  words = text.split()
 
36
  return reversed_ratio > 0.3
37
 
38
  def solve_riddle(self, question: str) -> str:
39
+ question = question[::-1] # properly reverse before parsing
40
+ if "opposite of the word" in question:
41
+ match = re.search(r"opposite of the word ['\"](\w+)['\"]", question)
42
  if match:
43
  word = match.group(1).lower()
44
  opposites = {
 
55
  print("Detected likely reversed riddle.")
56
  return self.solve_riddle(question)
57
  return "FINAL ANSWER: NOT_A_RIDDLE"
58
+
59
+
60
+ def run_and_submit_all(profile: gr.OAuthProfile | None):
61
+ space_id = os.getenv("SPACE_ID")
 
 
 
62
 
63
  if profile:
64
+ username = f"{profile.username}"
65
+ print("User logged in.")
66
  else:
67
  print("User not logged in.")
68
  return "Please Login to Hugging Face with the button.", None
 
71
  questions_url = f"{api_url}/questions"
72
  submit_url = f"{api_url}/submit"
73
 
 
74
  try:
75
  agent = BasicAgent()
76
  except Exception as e:
77
  print(f"Error instantiating agent: {e}")
78
  return f"Error initializing agent: {e}", None
79
+
80
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
81
  print(agent_code)
82
 
 
 
83
  try:
84
  response = requests.get(questions_url, timeout=15)
85
  response.raise_for_status()
86
  questions_data = response.json()
87
  if not questions_data:
88
+ return "Fetched questions list is empty or invalid format.", None
 
 
89
  except requests.exceptions.RequestException as e:
 
90
  return f"Error fetching questions: {e}", None
 
 
 
 
 
 
 
91
 
 
92
  results_log = []
93
  answers_payload = []
 
94
  for item in questions_data:
95
  task_id = item.get("task_id")
96
  question_text = item.get("question")
97
  if not task_id or question_text is None:
 
98
  continue
99
  try:
100
  submitted_answer = agent(question_text)
101
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
102
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
103
  except Exception as e:
104
+ results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
 
105
 
106
  if not answers_payload:
 
107
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
108
 
109
+ submission_data = {
110
+ "username": username.strip(),
111
+ "agent_code": agent_code,
112
+ "answers": answers_payload
113
+ }
114
 
 
 
115
  try:
116
  response = requests.post(submit_url, json=submission_data, timeout=60)
117
  response.raise_for_status()
 
123
  f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
124
  f"Message: {result_data.get('message', 'No message received.')}"
125
  )
126
+ return final_status, pd.DataFrame(results_log)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127
  except Exception as e:
128
+ return f"Submission Failed: {e}", pd.DataFrame(results_log)
 
 
 
129
 
130
 
131
+ # --- Build Gradio Interface ---
132
  with gr.Blocks() as demo:
133
  gr.Markdown("# Basic Agent Evaluation Runner")
 
 
 
 
 
 
 
 
 
 
 
 
 
134
  gr.LoginButton()
 
135
  run_button = gr.Button("Run Evaluation & Submit All Answers")
 
136
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
 
137
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
138
 
139
  run_button.click(
 
142
  )
143
 
144
  if __name__ == "__main__":
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145
  print("Launching Gradio Interface for Basic Agent Evaluation...")
146
  demo.launch(debug=True, share=False)