Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
| 44 |
-
except Exception as e:
|
| 45 |
-
|
| 46 |
-
|
| 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 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
if not answers_payload:
|
| 91 |
-
|
| 92 |
-
|
| 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 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
except requests.exceptions.HTTPError as e:
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
except requests.exceptions.Timeout:
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
except requests.exceptions.RequestException as e:
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
except Exception as e:
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 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 ---
|