Files changed (1) hide show
  1. app.py +24 -19
app.py CHANGED
@@ -1,8 +1,10 @@
1
  import os
2
  import gradio as gr
3
  import requests
4
- import inspect
5
  import pandas as pd
 
 
 
6
 
7
  # (Keep Constants as is)
8
  # --- Constants ---
@@ -10,20 +12,15 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
10
 
11
  # --- Basic Agent Definition ---
12
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
13
- class BasicAgent:
14
- def __init__(self):
15
- print("BasicAgent initialized.")
16
- def __call__(self, question: str) -> str:
17
- print(f"Agent received question (first 50 chars): {question[:50]}...")
18
- fixed_answer = "This is a default answer."
19
- print(f"Agent returning fixed answer: {fixed_answer}")
20
- return fixed_answer
21
 
22
  def run_and_submit_all( profile: gr.OAuthProfile | None):
23
  """
24
  Fetches all questions, runs the BasicAgent on them, submits all answers,
25
  and displays the results.
26
  """
 
 
 
27
  # --- Determine HF Space Runtime URL and Repo URL ---
28
  space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
29
 
@@ -36,14 +33,15 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
36
 
37
  api_url = DEFAULT_API_URL
38
  questions_url = f"{api_url}/questions"
 
39
  submit_url = f"{api_url}/submit"
40
-
41
  # 1. Instantiate Agent ( modify this part to create your agent)
42
- try:
43
- agent = BasicAgent()
44
- except Exception as e:
45
- print(f"Error instantiating agent: {e}")
46
- return f"Error initializing agent: {e}", None
47
  # 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)
48
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
49
  print(agent_code)
@@ -51,7 +49,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
51
  # 2. Fetch Questions
52
  print(f"Fetching questions from: {questions_url}")
53
  try:
54
- response = requests.get(questions_url, timeout=15)
55
  response.raise_for_status()
56
  questions_data = response.json()
57
  if not questions_data:
@@ -80,7 +78,15 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
80
  print(f"Skipping item with missing task_id or question: {item}")
81
  continue
82
  try:
83
- submitted_answer = agent(question_text)
 
 
 
 
 
 
 
 
84
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
85
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
86
  except Exception as e:
@@ -146,11 +152,9 @@ with gr.Blocks() as demo:
146
  gr.Markdown(
147
  """
148
  **Instructions:**
149
-
150
  1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
151
  2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
152
  3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
153
-
154
  ---
155
  **Disclaimers:**
156
  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).
@@ -173,6 +177,7 @@ with gr.Blocks() as demo:
173
 
174
  if __name__ == "__main__":
175
  print("\n" + "-"*30 + " App Starting " + "-"*30)
 
176
  # Check for SPACE_HOST and SPACE_ID at startup for information
177
  space_host_startup = os.getenv("SPACE_HOST")
178
  space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
 
1
  import os
2
  import gradio as gr
3
  import requests
 
4
  import pandas as pd
5
+ from dotenv.main import load_dotenv
6
+ import agent
7
+ import asyncio
8
 
9
  # (Keep Constants as is)
10
  # --- Constants ---
 
12
 
13
  # --- Basic Agent Definition ---
14
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
 
 
 
 
 
 
 
 
15
 
16
  def run_and_submit_all( profile: gr.OAuthProfile | None):
17
  """
18
  Fetches all questions, runs the BasicAgent on them, submits all answers,
19
  and displays the results.
20
  """
21
+
22
+ load_dotenv()
23
+
24
  # --- Determine HF Space Runtime URL and Repo URL ---
25
  space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
26
 
 
33
 
34
  api_url = DEFAULT_API_URL
35
  questions_url = f"{api_url}/questions"
36
+ files_url = f"{api_url}/files"
37
  submit_url = f"{api_url}/submit"
38
+
39
  # 1. Instantiate Agent ( modify this part to create your agent)
40
+ #try:
41
+ # agent = BasicAgent()
42
+ #except Exception as e:
43
+ # print(f"Error instantiating agent: {e}")
44
+ # return f"Error initializing agent: {e}", None
45
  # 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)
46
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
47
  print(agent_code)
 
49
  # 2. Fetch Questions
50
  print(f"Fetching questions from: {questions_url}")
51
  try:
52
+ response = requests.get(questions_url, timeout=60)
53
  response.raise_for_status()
54
  questions_data = response.json()
55
  if not questions_data:
 
78
  print(f"Skipping item with missing task_id or question: {item}")
79
  continue
80
  try:
81
+ file_url = f"{files_url}/{task_id}"
82
+ if "application/json" in requests.get(file_url).headers.get("Content-Type"):
83
+ file_url = None
84
+
85
+ agent_output = asyncio.run(agent.main(question_text, file_url))
86
+ submitted_answer = str(agent_output)
87
+ if 'FINAL ANSWER:' in submitted_answer:
88
+ submitted_answer = submitted_answer.split('FINAL ANSWER:')[1].strip()
89
+
90
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
91
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
92
  except Exception as e:
 
152
  gr.Markdown(
153
  """
154
  **Instructions:**
 
155
  1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
156
  2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
157
  3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
 
158
  ---
159
  **Disclaimers:**
160
  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).
 
177
 
178
  if __name__ == "__main__":
179
  print("\n" + "-"*30 + " App Starting " + "-"*30)
180
+
181
  # Check for SPACE_HOST and SPACE_ID at startup for information
182
  space_host_startup = os.getenv("SPACE_HOST")
183
  space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup