Spaces:
Runtime error
Runtime error
File size: 3,653 Bytes
d919051 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | import re
import requests
# (Keep Constants as is)
# --- Constants ---
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
def get_question() :
questions_url = f"{DEFAULT_API_URL}/questions"
print(f"Fetching questions from: {questions_url}")
try:
response = requests.get(questions_url, timeout=15)
response.raise_for_status()
questions_data = response.json()
if not questions_data:
print("Fetched questions list is empty.")
return "Fetched questions list is empty or invalid format.", None
print(f"Fetched {len(questions_data)} questions.")
return questions_data
except requests.exceptions.RequestException as e:
print(f"Error fetching questions: {e}")
return f"Error fetching questions: {e}", None
except requests.exceptions.JSONDecodeError as e:
print(f"Error decoding JSON response from questions endpoint: {e}")
print(f"Response text: {response.text[:500]}")
return f"Error decoding server response for questions: {e}", None
except Exception as e:
print(f"An unexpected error occurred fetching questions: {e}")
return f"An unexpected error occurred fetching questions: {e}", None
def get_random_question():
random_question_url = f"{DEFAULT_API_URL}/random-question"
print(f"Fetching a random question from: {random_question_url}")
try:
response = requests.get(random_question_url, timeout=15)
response.raise_for_status()
questions_data = response.json()
if not questions_data:
print("Fetched questions list is empty.")
return "Fetched questions list is empty or invalid format.", None
return questions_data
except requests.exceptions.RequestException as e:
print(f"Error fetching a random question: {e}")
return f"Error fetching a random question: {e}", None
except requests.exceptions.JSONDecodeError as e:
print(f"Error decoding JSON response from a random question endpoint: {e}")
print(f"Response text: {response.text[:500]}")
return f"Error decoding server response for a random question: {e}", None
except Exception as e:
print(f"An unexpected error occurred fetching a random question: {e}")
return f"An unexpected error occurred fetching a random question: {e}", None
def get_file_by_task_id(task_id:str):
file_by_task_id_url = f"{DEFAULT_API_URL}/files/{task_id}"
print(f"Fetching a file from: {file_by_task_id_url}")
try:
response = requests.get(file_by_task_id_url, timeout=15)
response.raise_for_status()
content_disp = response.headers.get("Content-Disposition")
file_name = re.findall('filename=(.+)', content_disp)
questions_data = response.content
if not questions_data or not file_name:
print("No file to fetch.")
return "No file to fetch.", None
return questions_data, file_name[0].replace('"', '').strip()
except requests.exceptions.RequestException as e:
print(f"Error fetching a file: {e}")
return f"Error fetching a file: {e}", None
except requests.exceptions.JSONDecodeError as e:
print(f"Error decoding JSON response from the file by task id endpoint: {e}")
print(f"Response text: {response.text[:500]}")
return f"Error decoding server response for the file by task id: {e}", None
except Exception as e:
print(f"An unexpected error occurred fetching a file: {e}")
return f"An unexpected error occurred fetching a file: {e}", None
|