MasterOfHugs commited on
Commit
296dd35
·
verified ·
1 Parent(s): e15bd56

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -37
app.py CHANGED
@@ -2,12 +2,11 @@ import os
2
  import gradio as gr
3
  import requests
4
  import pandas as pd
 
5
 
6
  # --- Constants ---
7
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
8
 
9
- import re
10
-
11
  # ----- Robust Hardcoded Agent Definition -----
12
  class RobustHardcodedAgent:
13
  def __init__(self):
@@ -28,7 +27,7 @@ class RobustHardcodedAgent:
28
  # Supprime retours à la ligne, espaces multiples, ponctuation et met en minuscules
29
  text = text.lower()
30
  text = re.sub(r'\s+', ' ', text)
31
- text = re.sub(r'[^\w\s]', '', text) # retire ponctuation
32
  return text.strip()
33
 
34
  def __call__(self, question: str) -> str:
@@ -38,11 +37,10 @@ class RobustHardcodedAgent:
38
  print(f"Agent returning answer: {answer}")
39
  return answer
40
 
41
-
42
- # ----- Run & Submit Function -----
43
  def run_and_submit_all(profile: gr.OAuthProfile | None):
44
- """ Fetches all questions, runs the HardcodedAgent on them, submits all answers, and displays the results. """
45
- space_id = os.getenv("SPACE_ID")
46
  if profile:
47
  username = profile.username
48
  print(f"User logged in: {username}")
@@ -54,25 +52,26 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
54
  questions_url = f"{api_url}/questions"
55
  submit_url = f"{api_url}/submit"
56
 
57
- # Instantiate agent
58
  try:
59
- agent = HardcodedAgent()
60
  except Exception as e:
 
61
  return f"Error initializing agent: {e}", None
62
 
63
- agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
64
-
65
- # Fetch questions
66
  try:
67
  response = requests.get(questions_url, timeout=15)
68
  response.raise_for_status()
69
  questions_data = response.json()
70
  if not questions_data:
71
  return "Fetched questions list is empty or invalid format.", None
 
72
  except Exception as e:
 
73
  return f"Error fetching questions: {e}", None
74
 
75
- # Run agent on questions
76
  results_log = []
77
  answers_payload = []
78
  for item in questions_data:
@@ -90,8 +89,12 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
90
  if not answers_payload:
91
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
92
 
93
- # Submit answers
 
 
94
  submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
 
 
95
  try:
96
  response = requests.post(submit_url, json=submission_data, timeout=60)
97
  response.raise_for_status()
@@ -103,37 +106,29 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
103
  f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
104
  f"Message: {result_data.get('message', 'No message received.')}"
105
  )
106
- return final_status, pd.DataFrame(results_log)
 
107
  except Exception as e:
108
- status_message = f"Submission Failed: {e}"
109
- return status_message, pd.DataFrame(results_log)
110
 
111
- # ----- Build Gradio Interface -----
112
  with gr.Blocks() as demo:
113
- gr.Markdown("# Hardcoded Agent Evaluation Runner")
114
- gr.Markdown("""
115
- **Instructions:**
116
- 1. Clone this space, then modify the code to define your agent's logic if needed.
117
- 2. Log in to your Hugging Face account using the button below.
118
- 3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
119
- """)
 
120
  gr.LoginButton()
121
  run_button = gr.Button("Run Evaluation & Submit All Answers")
122
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
123
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
124
-
125
  run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
126
 
 
127
  if __name__ == "__main__":
128
- print("\n" + "-"*30 + " App Starting " + "-"*30)
129
- space_host_startup = os.getenv("SPACE_HOST")
130
- space_id_startup = os.getenv("SPACE_ID")
131
- if space_host_startup:
132
- print(f"✅ SPACE_HOST found: {space_host_startup}")
133
- print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
134
- if space_id_startup:
135
- print(f"✅ SPACE_ID found: {space_id_startup}")
136
- print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
137
- print("-"*(60 + len(" App Starting ")) + "\n")
138
- print("Launching Gradio Interface for Hardcoded Agent Evaluation...")
139
  demo.launch(debug=True, share=False)
 
2
  import gradio as gr
3
  import requests
4
  import pandas as pd
5
+ import re
6
 
7
  # --- Constants ---
8
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
9
 
 
 
10
  # ----- Robust Hardcoded Agent Definition -----
11
  class RobustHardcodedAgent:
12
  def __init__(self):
 
27
  # Supprime retours à la ligne, espaces multiples, ponctuation et met en minuscules
28
  text = text.lower()
29
  text = re.sub(r'\s+', ' ', text)
30
+ text = re.sub(r'[^\w\s]', '', text)
31
  return text.strip()
32
 
33
  def __call__(self, question: str) -> str:
 
37
  print(f"Agent returning answer: {answer}")
38
  return answer
39
 
40
+ # ----- Run and Submit All -----
 
41
  def run_and_submit_all(profile: gr.OAuthProfile | None):
42
+ """ Fetches all questions, runs the RobustHardcodedAgent on them, submits answers, and returns results. """
43
+
44
  if profile:
45
  username = profile.username
46
  print(f"User logged in: {username}")
 
52
  questions_url = f"{api_url}/questions"
53
  submit_url = f"{api_url}/submit"
54
 
55
+ # 1. Instantiate Agent
56
  try:
57
+ agent = RobustHardcodedAgent()
58
  except Exception as e:
59
+ print(f"Error instantiating agent: {e}")
60
  return f"Error initializing agent: {e}", None
61
 
62
+ # 2. Fetch Questions
 
 
63
  try:
64
  response = requests.get(questions_url, timeout=15)
65
  response.raise_for_status()
66
  questions_data = response.json()
67
  if not questions_data:
68
  return "Fetched questions list is empty or invalid format.", None
69
+ print(f"Fetched {len(questions_data)} questions.")
70
  except Exception as e:
71
+ print(f"Error fetching questions: {e}")
72
  return f"Error fetching questions: {e}", None
73
 
74
+ # 3. Run Agent
75
  results_log = []
76
  answers_payload = []
77
  for item in questions_data:
 
89
  if not answers_payload:
90
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
91
 
92
+ # 4. Prepare Submission
93
+ space_id = os.getenv("SPACE_ID")
94
+ agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
95
  submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
96
+
97
+ # 5. Submit
98
  try:
99
  response = requests.post(submit_url, json=submission_data, timeout=60)
100
  response.raise_for_status()
 
106
  f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
107
  f"Message: {result_data.get('message', 'No message received.')}"
108
  )
109
+ results_df = pd.DataFrame(results_log)
110
+ return final_status, results_df
111
  except Exception as e:
112
+ results_df = pd.DataFrame(results_log)
113
+ return f"Submission Failed: {e}", results_df
114
 
115
+ # ----- Gradio Interface -----
116
  with gr.Blocks() as demo:
117
+ gr.Markdown("# Robust Hardcoded Agent Evaluation Runner")
118
+ gr.Markdown(
119
+ """
120
+ **Instructions:**
121
+ 1. Log in to your Hugging Face account.
122
+ 2. Click 'Run Evaluation & Submit All Answers' to fetch questions, run the agent, submit answers, and see the score.
123
+ """
124
+ )
125
  gr.LoginButton()
126
  run_button = gr.Button("Run Evaluation & Submit All Answers")
127
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
128
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
 
129
  run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
130
 
131
+ # ----- Main -----
132
  if __name__ == "__main__":
133
+ print("\nLaunching Gradio Interface for Robust Hardcoded Agent...")
 
 
 
 
 
 
 
 
 
 
134
  demo.launch(debug=True, share=False)