lwant commited on
Commit
7a17415
Β·
1 Parent(s): 3ec345b

Refactor submission logic into a separate module

Browse files

Moved functions related to agent instantiation, question fetching, answer submission, and data preparation to a new `hf_submission_api` module. This improves code organization and separation of concerns, making the `app.py` file cleaner and the codebase more maintainable.

Files changed (2) hide show
  1. app.py +3 -118
  2. src/gaia_solving_agent/hf_submission_api.py +119 -0
app.py CHANGED
@@ -1,8 +1,8 @@
1
  import os
2
  import gradio as gr
3
- import requests
4
- import inspect
5
- import pandas as pd
6
 
7
  # (Keep Constants as is)
8
  # --- Constants ---
@@ -21,121 +21,6 @@ class BasicAgent:
21
  return fixed_answer
22
 
23
 
24
- def instantiate_agent(space_id: str):
25
- try:
26
- agent = BasicAgent()
27
- except Exception as e:
28
- print(f"Error instantiating agent: {e}")
29
- return f"Error initializing agent: {e}", None
30
- # In the case of an app running as a hugging Face space, this link points toward your codebase
31
- # ( useful for others so please keep it public)
32
- agent_code: str = f"https://huggingface.co/spaces/{space_id}/tree/main"
33
- print(f"{agent_code=}")
34
- return agent, agent_code
35
-
36
-
37
- def fetching_questions(api_url: str = DEFAULT_API_URL):
38
- questions_url = f"{api_url}/questions"
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
- return questions_data
60
-
61
-
62
- def run_agent(agent, questions_data):
63
- results_log = []
64
- answers_payload = []
65
- print(f"Running agent on {len(questions_data)} questions...")
66
- for item in questions_data:
67
- task_id = item.get("task_id")
68
- question_text = item.get("question")
69
- if not task_id or question_text is None:
70
- print(f"Skipping item with missing task_id or question: {item}")
71
- continue
72
- try:
73
- submitted_answer = agent(question_text)
74
- answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
75
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
76
- except Exception as e:
77
- print(f"Error running agent on task {task_id}: {e}")
78
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
79
-
80
- if not answers_payload:
81
- print("Agent did not produce any answers to submit.")
82
- return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
83
- return answers_payload, results_log
84
-
85
-
86
- def prepare_submission_data(username:str, agent_code: str, answers_payload: list[dict]):
87
- submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
88
- status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
89
- print(status_update)
90
- return submission_data
91
-
92
-
93
- def submit_answers(submission_data, results_log, api_url: str = DEFAULT_API_URL):
94
- submit_url = f"{api_url}/submit"
95
-
96
- print(f"Submitting {len(submission_data.get("answers"))} answers to: {submit_url}")
97
- try:
98
- response = requests.post(submit_url, json=submission_data, timeout=60)
99
- response.raise_for_status()
100
- result_data = response.json()
101
- final_status = (
102
- f"Submission Successful!\n"
103
- f"User: {result_data.get('username')}\n"
104
- f"Overall Score: {result_data.get('score', 'N/A')}% "
105
- f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
106
- f"Message: {result_data.get('message', 'No message received.')}"
107
- )
108
- print("Submission successful.")
109
- results_df = pd.DataFrame(results_log)
110
- return final_status, results_df
111
- except requests.exceptions.HTTPError as e:
112
- error_detail = f"Server responded with status {e.response.status_code}."
113
- try:
114
- error_json = e.response.json()
115
- error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
116
- except requests.exceptions.JSONDecodeError:
117
- error_detail += f" Response: {e.response.text[:500]}"
118
- status_message = f"Submission Failed: {error_detail}"
119
- print(status_message)
120
- results_df = pd.DataFrame(results_log)
121
- return status_message, results_df
122
- except requests.exceptions.Timeout:
123
- status_message = "Submission Failed: The request timed out."
124
- print(status_message)
125
- results_df = pd.DataFrame(results_log)
126
- return status_message, results_df
127
- except requests.exceptions.RequestException as e:
128
- status_message = f"Submission Failed: Network error - {e}"
129
- print(status_message)
130
- results_df = pd.DataFrame(results_log)
131
- return status_message, results_df
132
- except Exception as e:
133
- status_message = f"An unexpected error occurred during submission: {e}"
134
- print(status_message)
135
- results_df = pd.DataFrame(results_log)
136
- return status_message, results_df
137
-
138
-
139
  def run_and_submit_all( profile: gr.OAuthProfile | None):
140
  """
141
  Fetches all questions, runs the BasicAgent on them, submits all answers,
 
1
  import os
2
  import gradio as gr
3
+
4
+ from gaia_solving_agent.hf_submission_api import instantiate_agent, fetching_questions, run_agent, \
5
+ prepare_submission_data, submit_answers
6
 
7
  # (Keep Constants as is)
8
  # --- Constants ---
 
21
  return fixed_answer
22
 
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  def run_and_submit_all( profile: gr.OAuthProfile | None):
25
  """
26
  Fetches all questions, runs the BasicAgent on them, submits all answers,
src/gaia_solving_agent/hf_submission_api.py ADDED
@@ -0,0 +1,119 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import requests
3
+
4
+ from app import BasicAgent, DEFAULT_API_URL
5
+
6
+
7
+ def instantiate_agent(space_id: str):
8
+ try:
9
+ agent = BasicAgent()
10
+ except Exception as e:
11
+ print(f"Error instantiating agent: {e}")
12
+ return f"Error initializing agent: {e}", None
13
+ # In the case of an app running as a hugging Face space, this link points toward your codebase
14
+ # ( useful for others so please keep it public)
15
+ agent_code: str = f"https://huggingface.co/spaces/{space_id}/tree/main"
16
+ print(f"{agent_code=}")
17
+ return agent, agent_code
18
+
19
+
20
+ def fetching_questions(api_url: str = DEFAULT_API_URL):
21
+ questions_url = f"{api_url}/questions"
22
+
23
+ print(f"Fetching questions from: {questions_url}")
24
+ try:
25
+ response = requests.get(questions_url, timeout=15)
26
+ response.raise_for_status()
27
+ questions_data = response.json()
28
+ if not questions_data:
29
+ print("Fetched questions list is empty.")
30
+ return "Fetched questions list is empty or invalid format.", None
31
+ print(f"Fetched {len(questions_data)} questions.")
32
+ except requests.exceptions.RequestException as e:
33
+ print(f"Error fetching questions: {e}")
34
+ return f"Error fetching questions: {e}", None
35
+ except requests.exceptions.JSONDecodeError as e:
36
+ print(f"Error decoding JSON response from questions endpoint: {e}")
37
+ print(f"Response text: {response.text[:500]}")
38
+ return f"Error decoding server response for questions: {e}", None
39
+ except Exception as e:
40
+ print(f"An unexpected error occurred fetching questions: {e}")
41
+ return f"An unexpected error occurred fetching questions: {e}", None
42
+ return questions_data
43
+
44
+
45
+ def run_agent(agent, questions_data):
46
+ results_log = []
47
+ answers_payload = []
48
+ print(f"Running agent on {len(questions_data)} questions...")
49
+ for item in questions_data:
50
+ task_id = item.get("task_id")
51
+ question_text = item.get("question")
52
+ if not task_id or question_text is None:
53
+ print(f"Skipping item with missing task_id or question: {item}")
54
+ continue
55
+ try:
56
+ submitted_answer = agent(question_text)
57
+ answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
58
+ results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
59
+ except Exception as e:
60
+ print(f"Error running agent on task {task_id}: {e}")
61
+ results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
62
+
63
+ if not answers_payload:
64
+ print("Agent did not produce any answers to submit.")
65
+ return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
66
+ return answers_payload, results_log
67
+
68
+
69
+ def prepare_submission_data(username:str, agent_code: str, answers_payload: list[dict]):
70
+ submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
71
+ status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
72
+ print(status_update)
73
+ return submission_data
74
+
75
+
76
+ def submit_answers(submission_data, results_log, api_url: str = DEFAULT_API_URL):
77
+ submit_url = f"{api_url}/submit"
78
+
79
+ print(f"Submitting {len(submission_data.get("answers"))} answers to: {submit_url}")
80
+ try:
81
+ response = requests.post(submit_url, json=submission_data, timeout=60)
82
+ response.raise_for_status()
83
+ result_data = response.json()
84
+ final_status = (
85
+ f"Submission Successful!\n"
86
+ f"User: {result_data.get('username')}\n"
87
+ f"Overall Score: {result_data.get('score', 'N/A')}% "
88
+ f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
89
+ f"Message: {result_data.get('message', 'No message received.')}"
90
+ )
91
+ print("Submission successful.")
92
+ results_df = pd.DataFrame(results_log)
93
+ return final_status, results_df
94
+ except requests.exceptions.HTTPError as e:
95
+ error_detail = f"Server responded with status {e.response.status_code}."
96
+ try:
97
+ error_json = e.response.json()
98
+ error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
99
+ except requests.exceptions.JSONDecodeError:
100
+ error_detail += f" Response: {e.response.text[:500]}"
101
+ status_message = f"Submission Failed: {error_detail}"
102
+ print(status_message)
103
+ results_df = pd.DataFrame(results_log)
104
+ return status_message, results_df
105
+ except requests.exceptions.Timeout:
106
+ status_message = "Submission Failed: The request timed out."
107
+ print(status_message)
108
+ results_df = pd.DataFrame(results_log)
109
+ return status_message, results_df
110
+ except requests.exceptions.RequestException as e:
111
+ status_message = f"Submission Failed: Network error - {e}"
112
+ print(status_message)
113
+ results_df = pd.DataFrame(results_log)
114
+ return status_message, results_df
115
+ except Exception as e:
116
+ status_message = f"An unexpected error occurred during submission: {e}"
117
+ print(status_message)
118
+ results_df = pd.DataFrame(results_log)
119
+ return status_message, results_df