| import io, base64 | |
| import json | |
| def image_to_base64(image): | |
| """Convert PIL Image to base64 encoded string""" | |
| img_byte_arr = io.BytesIO() | |
| image.save(img_byte_arr, format='JPEG') | |
| return base64.b64encode(img_byte_arr.getvalue()).decode('utf-8') | |
| def load_questions(filepath="data/all_questions.json"): | |
| """Loads questions from a JSON file. | |
| Args: | |
| filepath (str): The path to the JSON file. Defaults to "data/all_questions.json". | |
| Returns: | |
| list: A list of question records. Returns an empty list if the file is not found or if an error occurs during loading. | |
| """ | |
| try: | |
| with open(filepath, 'r') as f: | |
| data = json.load(f) | |
| return data | |
| except FileNotFoundError: | |
| print(f"Error: File not found at {filepath}") | |
| return [] | |
| except json.JSONDecodeError: | |
| print(f"Error: Could not decode JSON from {filepath}. The file may be corrupted.") | |
| return [] | |
| except Exception as e: | |
| print(f"An unexpected error occurred: {e}") | |
| return [] | |