Spaces:
Sleeping
Sleeping
Delete fetch_question.py
Browse files- fetch_question.py +0 -122
fetch_question.py
DELETED
@@ -1,122 +0,0 @@
|
|
1 |
-
from typing import Any
|
2 |
-
import pandas as pd
|
3 |
-
import requests
|
4 |
-
import mimetypes
|
5 |
-
|
6 |
-
# --- Constants ---
|
7 |
-
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
8 |
-
|
9 |
-
def get_files_by_task_id(id: str):
|
10 |
-
files_url = f"{DEFAULT_API_URL}/files/{id}"
|
11 |
-
print(f"Fetching files for task {id} from: {files_url}")
|
12 |
-
try:
|
13 |
-
response = requests.get(files_url, timeout=30)
|
14 |
-
response.raise_for_status()
|
15 |
-
|
16 |
-
# Return the raw response and content type
|
17 |
-
return {
|
18 |
-
"content": response.content,
|
19 |
-
"content_type": response.headers.get('Content-Type', ''),
|
20 |
-
"status": "success"
|
21 |
-
}
|
22 |
-
|
23 |
-
except requests.exceptions.RequestException as e:
|
24 |
-
error_message = f"Error fetching file: {e}"
|
25 |
-
print(error_message)
|
26 |
-
return {
|
27 |
-
"status": "error",
|
28 |
-
"error": error_message
|
29 |
-
}
|
30 |
-
|
31 |
-
def get_one_random_question():
|
32 |
-
questions_url = f"{DEFAULT_API_URL}/random-question"
|
33 |
-
print(f"Fetching questions from: {questions_url}")
|
34 |
-
try:
|
35 |
-
response = requests.get(questions_url, timeout=15)
|
36 |
-
response.raise_for_status()
|
37 |
-
questions_data = response.json()
|
38 |
-
if not questions_data:
|
39 |
-
print("Fetched questions list is empty.")
|
40 |
-
return "Fetched questions list is empty or invalid format.", None
|
41 |
-
print(f"Fetched {len(questions_data)} questions.")
|
42 |
-
return questions_data
|
43 |
-
except requests.exceptions.RequestException as e:
|
44 |
-
print(f"Error fetching questions: {e}")
|
45 |
-
return f"Error fetching questions: {e}", None
|
46 |
-
except requests.exceptions.JSONDecodeError as e:
|
47 |
-
print(f"Error decoding JSON response from questions endpoint: {e}")
|
48 |
-
print(f"Response text: {response.text[:500]}")
|
49 |
-
return f"Error decoding server response for questions: {e}", None
|
50 |
-
except Exception as e:
|
51 |
-
print(f"An unexpected error occurred fetching questions: {e}")
|
52 |
-
return f"An unexpected error occurred fetching questions: {e}", None
|
53 |
-
|
54 |
-
# 2. Fetch Questions
|
55 |
-
def get_all_questions():
|
56 |
-
questions_url = f"{DEFAULT_API_URL}/questions"
|
57 |
-
print(f"Fetching questions from: {questions_url}")
|
58 |
-
try:
|
59 |
-
response = requests.get(questions_url, timeout=15)
|
60 |
-
response.raise_for_status()
|
61 |
-
questions_data = response.json()
|
62 |
-
if not questions_data:
|
63 |
-
print("Fetched questions list is empty.")
|
64 |
-
return "Fetched questions list is empty or invalid format.", None
|
65 |
-
print(f"Fetched {len(questions_data)} questions.")
|
66 |
-
return questions_data
|
67 |
-
except requests.exceptions.RequestException as e:
|
68 |
-
print(f"Error fetching questions: {e}")
|
69 |
-
return f"Error fetching questions: {e}", None
|
70 |
-
except requests.exceptions.JSONDecodeError as e:
|
71 |
-
print(f"Error decoding JSON response from questions endpoint: {e}")
|
72 |
-
print(f"Response text: {response.text[:500]}")
|
73 |
-
return f"Error decoding server response for questions: {e}", None
|
74 |
-
except Exception as e:
|
75 |
-
print(f"An unexpected error occurred fetching questions: {e}")
|
76 |
-
return f"An unexpected error occurred fetching questions: {e}", None
|
77 |
-
|
78 |
-
# 5. Submit
|
79 |
-
def submit(submission_data:dict[str, Any], results_log:list):
|
80 |
-
submit_url = f"{DEFAULT_API_URL}/submit"
|
81 |
-
print(f"Submitting {len(submission_data['answers'])} answers to: {submit_url}")
|
82 |
-
try:
|
83 |
-
print(f"Submitting:", submission_data)
|
84 |
-
response = requests.post(submit_url, json=submission_data, timeout=60)
|
85 |
-
response.raise_for_status()
|
86 |
-
result_data = response.json()
|
87 |
-
final_status = (
|
88 |
-
f"Submission Successful!\n"
|
89 |
-
f"User: {result_data.get('username')}\n"
|
90 |
-
f"Overall Score: {result_data.get('score', 'N/A')}% "
|
91 |
-
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
92 |
-
f"Message: {result_data.get('message', 'No message received.')}"
|
93 |
-
)
|
94 |
-
print("Submission successful.")
|
95 |
-
results_df = pd.DataFrame(results_log)
|
96 |
-
return final_status, results_df
|
97 |
-
except requests.exceptions.HTTPError as e:
|
98 |
-
error_detail = f"Server responded with status {e.response.status_code}."
|
99 |
-
try:
|
100 |
-
error_json = e.response.json()
|
101 |
-
error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
|
102 |
-
except requests.exceptions.JSONDecodeError:
|
103 |
-
error_detail += f" Response: {e.response.text[:500]}"
|
104 |
-
status_message = f"Submission Failed: {error_detail}"
|
105 |
-
print(status_message)
|
106 |
-
results_df = pd.DataFrame(results_log)
|
107 |
-
return status_message, results_df
|
108 |
-
except requests.exceptions.Timeout:
|
109 |
-
status_message = "Submission Failed: The request timed out."
|
110 |
-
print(status_message)
|
111 |
-
results_df = pd.DataFrame(results_log)
|
112 |
-
return status_message, results_df
|
113 |
-
except requests.exceptions.RequestException as e:
|
114 |
-
status_message = f"Submission Failed: Network error - {e}"
|
115 |
-
print(status_message)
|
116 |
-
results_df = pd.DataFrame(results_log)
|
117 |
-
return status_message, results_df
|
118 |
-
except Exception as e:
|
119 |
-
status_message = f"An unexpected error occurred during submission: {e}"
|
120 |
-
print(status_message)
|
121 |
-
results_df = pd.DataFrame(results_log)
|
122 |
-
return status_message, results_df
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|