22Nikk0 commited on
Commit
bae04c8
·
verified ·
1 Parent(s): 81917a3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -76
app.py CHANGED
@@ -39,14 +39,14 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
39
  submit_url = f"{api_url}/submit"
40
 
41
  # 1. Instantiate Agent ( modify this part to create your agent)
42
- try:
43
- agent = BasicAgent()
44
- except Exception as e:
45
- print(f"Error instantiating agent: {e}")
46
- return f"Error initializing agent: {e}", None
47
- # 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)
48
- agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
49
- print(agent_code)
50
 
51
  # 2. Fetch Questions
52
  print(f"Fetching questions from: {questions_url}")
@@ -69,75 +69,78 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
69
  print(f"An unexpected error occurred fetching questions: {e}")
70
  return f"An unexpected error occurred fetching questions: {e}", None
71
 
 
 
 
72
  # 3. Run your Agent
73
- results_log = []
74
- answers_payload = []
75
- print(f"Running agent on {len(questions_data)} questions...")
76
- for item in questions_data:
77
- task_id = item.get("task_id")
78
- question_text = item.get("question")
79
- if not task_id or question_text is None:
80
- print(f"Skipping item with missing task_id or question: {item}")
81
- continue
82
- try:
83
- submitted_answer = agent(question_text)
84
- answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
85
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
86
- except Exception as e:
87
- print(f"Error running agent on task {task_id}: {e}")
88
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
89
-
90
- if not answers_payload:
91
- print("Agent did not produce any answers to submit.")
92
- return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
93
-
94
- # 4. Prepare Submission
95
- submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
96
- status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
97
- print(status_update)
98
-
99
- # 5. Submit
100
- print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
101
- try:
102
- response = requests.post(submit_url, json=submission_data, timeout=60)
103
- response.raise_for_status()
104
- result_data = response.json()
105
- final_status = (
106
- f"Submission Successful!\n"
107
- f"User: {result_data.get('username')}\n"
108
- f"Overall Score: {result_data.get('score', 'N/A')}% "
109
- f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
110
- f"Message: {result_data.get('message', 'No message received.')}"
111
- )
112
- print("Submission successful.")
113
- results_df = pd.DataFrame(results_log)
114
- return final_status, results_df
115
- except requests.exceptions.HTTPError as e:
116
- error_detail = f"Server responded with status {e.response.status_code}."
117
- try:
118
- error_json = e.response.json()
119
- error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
120
- except requests.exceptions.JSONDecodeError:
121
- error_detail += f" Response: {e.response.text[:500]}"
122
- status_message = f"Submission Failed: {error_detail}"
123
- print(status_message)
124
- results_df = pd.DataFrame(results_log)
125
- return status_message, results_df
126
- except requests.exceptions.Timeout:
127
- status_message = "Submission Failed: The request timed out."
128
- print(status_message)
129
- results_df = pd.DataFrame(results_log)
130
- return status_message, results_df
131
- except requests.exceptions.RequestException as e:
132
- status_message = f"Submission Failed: Network error - {e}"
133
- print(status_message)
134
- results_df = pd.DataFrame(results_log)
135
- return status_message, results_df
136
- except Exception as e:
137
- status_message = f"An unexpected error occurred during submission: {e}"
138
- print(status_message)
139
- results_df = pd.DataFrame(results_log)
140
- return status_message, results_df
141
 
142
 
143
  # --- Build Gradio Interface using Blocks ---
 
39
  submit_url = f"{api_url}/submit"
40
 
41
  # 1. Instantiate Agent ( modify this part to create your agent)
42
+ # try:
43
+ # agent = BasicAgent()
44
+ # except Exception as e:
45
+ # print(f"Error instantiating agent: {e}")
46
+ # return f"Error initializing agent: {e}", None
47
+ # # 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)
48
+ # agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
49
+ # print(agent_code)
50
 
51
  # 2. Fetch Questions
52
  print(f"Fetching questions from: {questions_url}")
 
69
  print(f"An unexpected error occurred fetching questions: {e}")
70
  return f"An unexpected error occurred fetching questions: {e}", None
71
 
72
+ with open("questions.json", "w") as file:
73
+ file.write(questions_data)
74
+
75
  # 3. Run your Agent
76
+ # results_log = []
77
+ # answers_payload = []
78
+ # print(f"Running agent on {len(questions_data)} questions...")
79
+ # for item in questions_data:
80
+ # task_id = item.get("task_id")
81
+ # question_text = item.get("question")
82
+ # if not task_id or question_text is None:
83
+ # print(f"Skipping item with missing task_id or question: {item}")
84
+ # continue
85
+ # try:
86
+ # submitted_answer = agent(question_text)
87
+ # answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
88
+ # results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
89
+ # except Exception as e:
90
+ # print(f"Error running agent on task {task_id}: {e}")
91
+ # results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
92
+
93
+ # if not answers_payload:
94
+ # print("Agent did not produce any answers to submit.")
95
+ # return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
96
+
97
+ # # 4. Prepare Submission
98
+ # submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
99
+ # status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
100
+ # print(status_update)
101
+
102
+ # # 5. Submit
103
+ # print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
104
+ # try:
105
+ # response = requests.post(submit_url, json=submission_data, timeout=60)
106
+ # response.raise_for_status()
107
+ # result_data = response.json()
108
+ # final_status = (
109
+ # f"Submission Successful!\n"
110
+ # f"User: {result_data.get('username')}\n"
111
+ # f"Overall Score: {result_data.get('score', 'N/A')}% "
112
+ # f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
113
+ # f"Message: {result_data.get('message', 'No message received.')}"
114
+ # )
115
+ # print("Submission successful.")
116
+ # results_df = pd.DataFrame(results_log)
117
+ # return final_status, results_df
118
+ # except requests.exceptions.HTTPError as e:
119
+ # error_detail = f"Server responded with status {e.response.status_code}."
120
+ # try:
121
+ # error_json = e.response.json()
122
+ # error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
123
+ # except requests.exceptions.JSONDecodeError:
124
+ # error_detail += f" Response: {e.response.text[:500]}"
125
+ # status_message = f"Submission Failed: {error_detail}"
126
+ # print(status_message)
127
+ # results_df = pd.DataFrame(results_log)
128
+ # return status_message, results_df
129
+ # except requests.exceptions.Timeout:
130
+ # status_message = "Submission Failed: The request timed out."
131
+ # print(status_message)
132
+ # results_df = pd.DataFrame(results_log)
133
+ # return status_message, results_df
134
+ # except requests.exceptions.RequestException as e:
135
+ # status_message = f"Submission Failed: Network error - {e}"
136
+ # print(status_message)
137
+ # results_df = pd.DataFrame(results_log)
138
+ # return status_message, results_df
139
+ # except Exception as e:
140
+ # status_message = f"An unexpected error occurred during submission: {e}"
141
+ # print(status_message)
142
+ # results_df = pd.DataFrame(results_log)
143
+ # return status_message, results_df
144
 
145
 
146
  # --- Build Gradio Interface using Blocks ---