Lucas-C-R commited on
Commit
581a2aa
·
1 Parent(s): 984ba4d

feat: add main script to handle questions and send answers to Hugging Face

Browse files
Files changed (1) hide show
  1. app.py +222 -0
app.py ADDED
@@ -0,0 +1,222 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ import gradio as gr
4
+ import pandas as pd
5
+ import requests
6
+
7
+ from services import BasicAgent
8
+
9
+ # (Keep Constants as is)
10
+ # --- Constants ---
11
+ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
12
+
13
+
14
+ def save_results_log(results_log: list, username: str) -> str:
15
+ """Save results to CSV."""
16
+ filename = f"results_{username}.csv"
17
+
18
+ df = pd.DataFrame(results_log)
19
+ df.to_csv(filename, index=False)
20
+ return filename
21
+
22
+
23
+ def run_and_submit_all(profile: gr.OAuthProfile | None):
24
+ """
25
+ Fetches all questions, runs the BasicAgent on them, submits all answers,
26
+ and displays the results.
27
+ """
28
+ # --- Determine HF Space Runtime URL and Repo URL ---
29
+ space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
30
+
31
+ if profile:
32
+ username = f"{profile.username}"
33
+ print(f"User logged in: {username}")
34
+ else:
35
+ print("User not logged in.")
36
+ return "Please Login to Hugging Face with the button.", None
37
+
38
+ api_url = DEFAULT_API_URL
39
+ questions_url = f"{api_url}/questions"
40
+ submit_url = f"{api_url}/submit"
41
+
42
+ # 1. Instantiate Agent ( modify this part to create your agent)
43
+ try:
44
+ agent = BasicAgent()
45
+ except Exception as e:
46
+ print(f"Error instantiating agent: {e}")
47
+ return f"Error initializing agent: {e}", None
48
+ # 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)
49
+ agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
50
+ print(agent_code)
51
+
52
+ # 2. Fetch Questions
53
+ print(f"Fetching questions from: {questions_url}")
54
+ try:
55
+ response = requests.get(questions_url, timeout=15)
56
+ response.raise_for_status()
57
+ questions_data = response.json()
58
+ if not questions_data:
59
+ print("Fetched questions list is empty.")
60
+ return "Fetched questions list is empty or invalid format.", None
61
+ print(f"Fetched {len(questions_data)} questions.")
62
+ except requests.exceptions.RequestException as e:
63
+ print(f"Error fetching questions: {e}")
64
+ return f"Error fetching questions: {e}", None
65
+ except requests.exceptions.JSONDecodeError as e:
66
+ print(f"Error decoding JSON response from questions endpoint: {e}")
67
+ print(f"Response text: {response.text[:500]}")
68
+ return f"Error decoding server response for questions: {e}", None
69
+ except Exception as e:
70
+ print(f"An unexpected error occurred fetching questions: {e}")
71
+ return f"An unexpected error occurred fetching questions: {e}", None
72
+
73
+ # 3. Run your Agent
74
+ results_log = []
75
+ answers_payload = []
76
+ print(f"Running agent on {len(questions_data)} questions...")
77
+ for item in questions_data:
78
+ task_id = item.get("task_id")
79
+ question_text = item.get("question")
80
+ if not task_id or question_text is None:
81
+ print(f"Skipping item with missing task_id or question: {item}")
82
+ continue
83
+ try:
84
+ submitted_answer = agent(question_text)
85
+ answers_payload.append(
86
+ {"task_id": task_id, "submitted_answer": submitted_answer}
87
+ )
88
+ results_log.append(
89
+ {
90
+ "Task ID": task_id,
91
+ "Question": question_text,
92
+ "Submitted Answer": submitted_answer,
93
+ }
94
+ )
95
+ except Exception as e:
96
+ print(f"Error running agent on task {task_id}: {e}")
97
+ results_log.append(
98
+ {
99
+ "Task ID": task_id,
100
+ "Question": question_text,
101
+ "Submitted Answer": f"AGENT ERROR: {e}",
102
+ }
103
+ )
104
+
105
+ if not answers_payload:
106
+ print("Agent did not produce any answers to submit.")
107
+ return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
108
+
109
+ # Save results to CSV
110
+ log_filename = save_results_log(results_log, username)
111
+ print(f"Results saved to {log_filename}")
112
+
113
+ # 4. Prepare Submission
114
+ submission_data = {
115
+ "username": username.strip(),
116
+ "agent_code": agent_code,
117
+ "answers": answers_payload,
118
+ }
119
+ status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
120
+ print(status_update)
121
+
122
+ # 5. Submit
123
+ print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
124
+ try:
125
+ response = requests.post(submit_url, json=submission_data, timeout=60)
126
+ response.raise_for_status()
127
+ result_data = response.json()
128
+ final_status = (
129
+ f"Submission Successful!\n"
130
+ f"User: {result_data.get('username')}\n"
131
+ f"Overall Score: {result_data.get('score', 'N/A')}% "
132
+ f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
133
+ f"Message: {result_data.get('message', 'No message received.')}"
134
+ )
135
+ print("Submission successful.")
136
+ results_df = pd.DataFrame(results_log)
137
+ return final_status, results_df
138
+ except requests.exceptions.HTTPError as e:
139
+ error_detail = f"Server responded with status {e.response.status_code}."
140
+ try:
141
+ error_json = e.response.json()
142
+ error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
143
+ except requests.exceptions.JSONDecodeError:
144
+ error_detail += f" Response: {e.response.text[:500]}"
145
+ status_message = f"Submission Failed: {error_detail}"
146
+ print(status_message)
147
+ results_df = pd.DataFrame(results_log)
148
+ return status_message, results_df
149
+ except requests.exceptions.Timeout:
150
+ status_message = "Submission Failed: The request timed out."
151
+ print(status_message)
152
+ results_df = pd.DataFrame(results_log)
153
+ return status_message, results_df
154
+ except requests.exceptions.RequestException as e:
155
+ status_message = f"Submission Failed: Network error - {e}"
156
+ print(status_message)
157
+ results_df = pd.DataFrame(results_log)
158
+ return status_message, results_df
159
+ except Exception as e:
160
+ status_message = f"An unexpected error occurred during submission: {e}"
161
+ print(status_message)
162
+ results_df = pd.DataFrame(results_log)
163
+ return status_message, results_df
164
+
165
+
166
+ # --- Build Gradio Interface using Blocks ---
167
+ with gr.Blocks() as demo:
168
+ gr.Markdown("# Basic Agent Evaluation Runner")
169
+ gr.Markdown(
170
+ """
171
+ **Instructions:**
172
+
173
+ 1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
174
+ 2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
175
+ 3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
176
+
177
+ ---
178
+ **Disclaimers:**
179
+ 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).
180
+ 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.
181
+ """
182
+ )
183
+
184
+ gr.LoginButton()
185
+
186
+ run_button = gr.Button("Run Evaluation & Submit All Answers")
187
+
188
+ status_output = gr.Textbox(
189
+ label="Run Status / Submission Result", lines=5, interactive=False
190
+ )
191
+ # Removed max_rows=10 from DataFrame constructor
192
+ results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
193
+
194
+ run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
195
+
196
+ if __name__ == "__main__":
197
+ print("\n" + "-" * 30 + " App Starting " + "-" * 30)
198
+ # Check for SPACE_HOST and SPACE_ID at startup for information
199
+ space_host_startup = os.getenv("SPACE_HOST")
200
+ space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
201
+
202
+ if space_host_startup:
203
+ print(f"✅ SPACE_HOST found: {space_host_startup}")
204
+ print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
205
+ else:
206
+ print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
207
+
208
+ if space_id_startup: # Print repo URLs if SPACE_ID is found
209
+ print(f"✅ SPACE_ID found: {space_id_startup}")
210
+ print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
211
+ print(
212
+ f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main"
213
+ )
214
+ else:
215
+ print(
216
+ "ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined."
217
+ )
218
+
219
+ print("-" * (60 + len(" App Starting ")) + "\n")
220
+
221
+ print("Launching Gradio Interface for Basic Agent Evaluation...")
222
+ demo.launch(debug=True, share=False)