Spaces:
Runtime error
Runtime error
| import os | |
| import requests | |
| import json | |
| API_URL = os.getenv("DEFAULT_API_URL") | |
| def fetch_questions(output_file_path: str | None = None): | |
| QUESTIONS_URL = f"{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.") | |
| except Exception as e: | |
| raise e | |
| if output_file_path: | |
| output_dir_path = os.path.dirname(output_file_path) | |
| if not os.path.exists(output_dir_path): | |
| os.makedirs(output_dir_path, exist_ok=True) | |
| with open(output_file_path, "w") as output_file: | |
| json.dump( | |
| questions_data, | |
| output_file, | |
| indent=4, | |
| ensure_ascii=False, | |
| ) | |
| return questions_data | |
| def download_questions_files( | |
| questions: list[dict[str, str]], output_folder: str = "./data/assets" | |
| ): | |
| """Download files associated with each question and save them locally.""" | |
| # Implement your logic to check if the question files exist here. | |
| FILES_URL = f"{API_URL}/files" | |
| if not os.path.exists(output_folder): | |
| os.makedirs(output_folder, exist_ok=True) | |
| for question in questions: | |
| if file_name := question.get("file_name"): | |
| task_id = question.get("task_id") | |
| file_url = f"{FILES_URL}/{task_id}" | |
| print(f"Downloading file associated with task_id {task_id}...") | |
| try: | |
| response = requests.get(file_url, timeout=15) | |
| response.raise_for_status() | |
| with open(f"{output_folder}/{file_name}", "wb") as file: | |
| file.write(response.content) | |
| print(f"File downloaded successfully for task_id: {task_id}") | |
| except Exception as e: | |
| raise e | |