mnosouhi96 commited on
Commit
51ce1ed
·
1 Parent(s): cbf5963

handle login

Browse files
Files changed (1) hide show
  1. app.py +49 -100
app.py CHANGED
@@ -11,17 +11,24 @@ class BasicAgent:
11
  print("BasicAgent initialized.")
12
  def __call__(self, question: str) -> str:
13
  print(f"Agent received question (first 50 chars): {question[:50]}...")
14
- fixed_answer = "This is a default answer."
15
- print(f"Agent returning fixed answer: {fixed_answer}")
16
- return fixed_answer
 
 
 
 
 
 
 
 
 
 
17
 
18
  def run_and_submit_all(profile, evt=None):
19
  space_id = SPACE_ID
20
- if profile:
21
- username = f"{getattr(profile, 'username', '')}"
22
- print(f"User logged in: {username}")
23
- else:
24
- print("User not logged in.")
25
  return "Please Login to Hugging Face with the button.", None
26
 
27
  api_url = DEFAULT_API_URL
@@ -31,121 +38,72 @@ def run_and_submit_all(profile, evt=None):
31
  try:
32
  agent = BasicAgent()
33
  except Exception as e:
34
- print(f"Error instantiating agent: {e}")
35
  return f"Error initializing agent: {e}", None
36
 
37
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
38
- print(agent_code)
39
 
40
- print(f"Fetching questions from: {questions_url}")
41
  try:
42
  response = requests.get(questions_url, timeout=15)
43
  response.raise_for_status()
44
  questions_data = response.json()
45
  if not questions_data:
46
- print("Fetched questions list is empty.")
47
  return "Fetched questions list is empty or invalid format.", None
48
- print(f"Fetched {len(questions_data)} questions.")
49
- except requests.exceptions.RequestException as e:
50
- print(f"Error fetching questions: {e}")
51
- return f"Error fetching questions: {e}", None
52
- except requests.exceptions.JSONDecodeError as e:
53
- print(f"Error decoding JSON response from questions endpoint: {e}")
54
- print(f"Response text: {response.text[:500]}")
55
- return f"Error decoding server response for questions: {e}", None
56
  except Exception as e:
57
- print(f"An unexpected error occurred fetching questions: {e}")
58
- return f"An unexpected error occurred fetching questions: {e}", None
59
 
60
- results_log = []
61
- answers_payload = []
62
- print(f"Running agent on {len(questions_data)} questions...")
63
  for item in questions_data:
64
- task_id = item.get("task_id")
65
- question_text = item.get("question")
66
- if not task_id or question_text is None:
67
- print(f"Skipping item with missing task_id or question: {item}")
68
  continue
69
  try:
70
- submitted_answer = agent(question_text)
71
- answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
72
- results_log.append({
73
- "Task ID": task_id,
74
- "Question": question_text,
75
- "Submitted Answer": submitted_answer
76
- })
77
  except Exception as e:
78
- print(f"Error running agent on task {task_id}: {e}")
79
- results_log.append({
80
- "Task ID": task_id,
81
- "Question": question_text,
82
- "Submitted Answer": f"AGENT ERROR: {e}"
83
- })
84
 
85
  if not answers_payload:
86
- print("Agent did not produce any answers to submit.")
87
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
88
 
89
  submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
90
- status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
91
- print(status_update)
92
-
93
- print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
94
  try:
95
- response = requests.post(submit_url, json=submission_data, timeout=60)
96
- response.raise_for_status()
97
- result_data = response.json()
98
- final_status = (
99
- f"Submission Successful!\n"
100
- f"User: {result_data.get('username')}\n"
101
- f"Overall Score: {result_data.get('score', 'N/A')}% "
102
- f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
103
- f"Message: {result_data.get('message', 'No message received.')}"
104
  )
105
- print("Submission successful.")
106
- results_df = pd.DataFrame(results_log)
107
- return final_status, results_df
108
  except requests.exceptions.HTTPError as e:
109
- error_detail = f"Server responded with status {e.response.status_code}."
110
  try:
111
- error_json = e.response.json()
112
- error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
113
- except requests.exceptions.JSONDecodeError:
114
- error_detail += f" Response: {e.response.text[:500]}"
115
- status_message = f"Submission Failed: {error_detail}"
116
- print(status_message)
117
- results_df = pd.DataFrame(results_log)
118
- return status_message, results_df
119
  except requests.exceptions.Timeout:
120
- status_message = "Submission Failed: The request timed out."
121
- print(status_message)
122
- results_df = pd.DataFrame(results_log)
123
- return status_message, results_df
124
- except requests.exceptions.RequestException as e:
125
- status_message = f"Submission Failed: Network error - {e}"
126
- print(status_message)
127
- results_df = pd.DataFrame(results_log)
128
- return status_message, results_df
129
  except Exception as e:
130
- status_message = f"An unexpected error occurred during submission: {e}"
131
- print(status_message)
132
- results_df = pd.DataFrame(results_log)
133
- return status_message, results_df
134
 
135
  with gr.Blocks() as demo:
136
  gr.Markdown("# Basic Agent Evaluation Runner")
137
- gr.Markdown(
138
- """
139
- **Instructions:**
140
- 1) Clone this space, then modify the code to define your agent's logic.
141
- 2) Log in to your Hugging Face account using the button below.
142
- 3) Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
143
- """
144
- )
145
 
146
  login = gr.LoginButton()
147
  user_state = gr.State()
148
- login.click(lambda p: p, inputs=login, outputs=user_state)
 
 
 
 
 
 
149
 
150
  run_button = gr.Button("Run Evaluation & Submit All Answers")
151
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
@@ -154,13 +112,4 @@ with gr.Blocks() as demo:
154
  run_button.click(fn=run_and_submit_all, inputs=[user_state], outputs=[status_output, results_table])
155
 
156
  if __name__ == "__main__":
157
- print("\n" + "-"*30 + " App Starting " + "-"*30)
158
- space_host_startup = os.getenv("SPACE_HOST")
159
- print(f"Repo URL: https://huggingface.co/spaces/{SPACE_ID}")
160
- print(f"Repo Tree URL: https://huggingface.co/spaces/{SPACE_ID}/tree/main")
161
- if space_host_startup:
162
- print(f"Runtime URL should be: https://{space_host_startup}.hf.space")
163
- else:
164
- print("SPACE_HOST not found (running locally?).")
165
- print("-"*(60 + len(" App Starting ")) + "\n")
166
- demo.launch(debug=True, share=False)
 
11
  print("BasicAgent initialized.")
12
  def __call__(self, question: str) -> str:
13
  print(f"Agent received question (first 50 chars): {question[:50]}...")
14
+ return "This is a default answer."
15
+
16
+ def _username_from_profile(p):
17
+ if p is None:
18
+ return None
19
+ # object with attribute
20
+ u = getattr(p, "username", None)
21
+ if u:
22
+ return str(u)
23
+ # dict-style
24
+ if isinstance(p, dict):
25
+ return str(p.get("username") or "")
26
+ return None
27
 
28
  def run_and_submit_all(profile, evt=None):
29
  space_id = SPACE_ID
30
+ username = _username_from_profile(profile)
31
+ if not username:
 
 
 
32
  return "Please Login to Hugging Face with the button.", None
33
 
34
  api_url = DEFAULT_API_URL
 
38
  try:
39
  agent = BasicAgent()
40
  except Exception as e:
 
41
  return f"Error initializing agent: {e}", None
42
 
43
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
 
44
 
 
45
  try:
46
  response = requests.get(questions_url, timeout=15)
47
  response.raise_for_status()
48
  questions_data = response.json()
49
  if not questions_data:
 
50
  return "Fetched questions list is empty or invalid format.", None
 
 
 
 
 
 
 
 
51
  except Exception as e:
52
+ return f"Error fetching questions: {e}", None
 
53
 
54
+ results_log, answers_payload = [], []
 
 
55
  for item in questions_data:
56
+ tid = item.get("task_id")
57
+ q = item.get("question")
58
+ if not tid or q is None:
 
59
  continue
60
  try:
61
+ ans = agent(q)
 
 
 
 
 
 
62
  except Exception as e:
63
+ ans = f"AGENT ERROR: {e}"
64
+ answers_payload.append({"task_id": tid, "submitted_answer": ans})
65
+ results_log.append({"Task ID": tid, "Question": q, "Submitted Answer": ans})
 
 
 
66
 
67
  if not answers_payload:
 
68
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
69
 
70
  submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
 
 
 
 
71
  try:
72
+ resp = requests.post(submit_url, json=submission_data, timeout=60)
73
+ resp.raise_for_status()
74
+ data = resp.json()
75
+ status = (
76
+ "Submission Successful!\n"
77
+ f"User: {data.get('username')}\n"
78
+ f"Overall Score: {data.get('score','N/A')}% "
79
+ f"({data.get('correct_count','?')}/{data.get('total_attempted','?')} correct)\n"
80
+ f"Message: {data.get('message','No message received.')}"
81
  )
82
+ return status, pd.DataFrame(results_log)
 
 
83
  except requests.exceptions.HTTPError as e:
 
84
  try:
85
+ detail = e.response.json().get("detail", e.response.text)
86
+ except Exception:
87
+ detail = e.response.text
88
+ return f"Submission Failed: HTTP {e.response.status_code}. Detail: {detail[:500]}", pd.DataFrame(results_log)
 
 
 
 
89
  except requests.exceptions.Timeout:
90
+ return "Submission Failed: The request timed out.", pd.DataFrame(results_log)
 
 
 
 
 
 
 
 
91
  except Exception as e:
92
+ return f"Submission Failed: {e}", pd.DataFrame(results_log)
 
 
 
93
 
94
  with gr.Blocks() as demo:
95
  gr.Markdown("# Basic Agent Evaluation Runner")
96
+ gr.Markdown("Click **Log in with Hugging Face** first. After you see your username below, press **Run**.")
 
 
 
 
 
 
 
97
 
98
  login = gr.LoginButton()
99
  user_state = gr.State()
100
+ whoami = gr.Markdown()
101
+
102
+ def store_and_echo(p):
103
+ u = _username_from_profile(p) or "(not logged in)"
104
+ return p, f"✅ Logged in as **{u}**" if u and u != "(not logged in)" else "❌ Not logged in"
105
+
106
+ login.click(store_and_echo, inputs=login, outputs=[user_state, whoami])
107
 
108
  run_button = gr.Button("Run Evaluation & Submit All Answers")
109
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
 
112
  run_button.click(fn=run_and_submit_all, inputs=[user_state], outputs=[status_output, results_table])
113
 
114
  if __name__ == "__main__":
115
+ demo.launch(debug=True, share=True)