lucasnseq commited on
Commit
1ac19e5
·
verified ·
1 Parent(s): 3abd679

Update app.py

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