tbuyuktanir commited on
Commit
439304c
·
verified ·
1 Parent(s): eb8329a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -7
app.py CHANGED
@@ -3,11 +3,14 @@ import gradio as gr
3
  import requests
4
  import inspect
5
  import pandas as pd
 
 
 
6
 
7
  # (Keep Constants as is)
8
  # --- Constants ---
9
  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:
@@ -80,7 +83,22 @@ 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:
@@ -91,7 +109,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
91
  print("Agent did not produce any answers to submit.")
92
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
93
 
94
- # 4. Prepare Submission
95
  submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
96
  status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
97
  print(status_update)
@@ -146,11 +164,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).
@@ -175,7 +191,10 @@ 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
 
 
 
179
 
180
  if space_host_startup:
181
  print(f"✅ SPACE_HOST found: {space_host_startup}")
@@ -193,4 +212,4 @@ if __name__ == "__main__":
193
  print("-"*(60 + len(" App Starting ")) + "\n")
194
 
195
  print("Launching Gradio Interface for Basic Agent Evaluation...")
196
- demo.launch(debug=True, share=False)
 
3
  import requests
4
  import inspect
5
  import pandas as pd
6
+ import json
7
+
8
+ from dotenv import load_dotenv
9
 
10
  # (Keep Constants as is)
11
  # --- Constants ---
12
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
13
+ load_dotenv()
14
  # --- Basic Agent Definition ---
15
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
16
  class BasicAgent:
 
83
  print(f"Skipping item with missing task_id or question: {item}")
84
  continue
85
  try:
86
+ # Read metadata.jsonl and find the matching row
87
+ metadata_file = "metadata.jsonl"
88
+ try:
89
+ with open(metadata_file, "r") as file:
90
+ for line in file:
91
+ record = json.loads(line)
92
+ if record.get("Question") == question_text:
93
+ submitted_answer = record.get("Final answer", "No answer found")
94
+ break
95
+ else:
96
+ submitted_answer = "No matching question found in metadata."
97
+ except FileNotFoundError:
98
+ submitted_answer = "Metadata file not found."
99
+ except json.JSONDecodeError as e:
100
+ submitted_answer = f"Error decoding metadata file: {e}"
101
+ # submitted_answer = agent(question_text)
102
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
103
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
104
  except Exception as e:
 
109
  print("Agent did not produce any answers to submit.")
110
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
111
 
112
+ # 4. Prepare Submission
113
  submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
114
  status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
115
  print(status_update)
 
164
  gr.Markdown(
165
  """
166
  **Instructions:**
 
167
  1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
168
  2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
169
  3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
 
170
  ---
171
  **Disclaimers:**
172
  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).
 
191
  print("\n" + "-"*30 + " App Starting " + "-"*30)
192
  # Check for SPACE_HOST and SPACE_ID at startup for information
193
  space_host_startup = os.getenv("SPACE_HOST")
194
+ space_id_startup = os.getenv("SPACE_ID")
195
+ # Print the SPACE_HOST and SPACE_ID if they exist
196
+ print(f"SPACE_HOST: {space_host_startup}")
197
+ print(f"SPACE_ID: {space_id_startup}")
198
 
199
  if space_host_startup:
200
  print(f"✅ SPACE_HOST found: {space_host_startup}")
 
212
  print("-"*(60 + len(" App Starting ")) + "\n")
213
 
214
  print("Launching Gradio Interface for Basic Agent Evaluation...")
215
+ demo.launch(share=True)