# tool2.py from gradio_client import Client import os import json # Function to load question sets from a directory def load_question_sets_vce(directory='questions'): question_sets = [] for root, dirs, files in os.walk(directory): for file in files: if file.endswith(".json"): question_sets.append(os.path.join(file)[:-5]) # remove the .json extension return question_sets exams = load_question_sets_vce('questions/') print("question_sets:", exams) def select_exam_vce(exam_name): file_path = os.path.join(os.getcwd(), 'questions', f'{exam_name}.json') try: with open(file_path, 'r') as f: questions = json.load(f) print(f"Loaded {len(questions)} questions") return questions # Ensure the questions are returned here except FileNotFoundError: print(f"File {file_path} not found.") return [] # Return an empty list to indicate no questions were found def display_question(index, audio_enabled, selected_questions): """Displays the question, options, and generates audio if enabled.""" if 0 <= index < len(selected_questions): question_data = selected_questions[index] question_text = question_data["question"] options = question_data["options"] audio_path = text_to_speech(question_text) if audio_enabled else None return question_text, options, audio_path else: return "Question index out of range.", [], None def get_explanation_and_answer(index, selected_questions): """Retrieves the explanation for a given question index.""" if 0 <= index < len(selected_questions): return selected_questions[index]["explanation"], selected_questions[index]["answer"] return "No explanation available.", "" # Text-to-speech function with rate limiting, retry mechanism, and client rotation import time import httpx # Text-to-speech clients client_fast = None # Initialize client_fast to None client_2 = None client_3 = None clients = [] # Initialize client_fast with error handling try: client_fast = Client("https://ruslanmv-text-to-speech-fast.hf.space/") clients.append(client_fast) print("Loaded fast TTS client (client_fast) ✔") except ValueError as e: print(f"Error loading fast TTS client (client_fast): {e}") print("Fast Text-to-Speech will be unavailable.") except Exception as e: # Catch other potential exceptions during client initialization print(f"An unexpected error occurred during fast TTS client (client_fast) initialization: {e}") print("Fast Text-to-Speech will be unavailable.") # Retry logic for client_2 initialization max_retries = 3 retry_delay = 5 # seconds for attempt in range(max_retries): try: client_2 = Client("ruslanmv/Text-To-Speech") clients.append(client_2) print("Loaded TTS client (client_2) ✔") break # If successful, break