Spaces:
Running
Running
Update tool2.py
Browse files
tool2.py
CHANGED
@@ -9,7 +9,7 @@ def load_question_sets_vce(directory='questions'):
|
|
9 |
for root, dirs, files in os.walk(directory):
|
10 |
for file in files:
|
11 |
if file.endswith(".json"):
|
12 |
-
question_sets.append(os.path.join(
|
13 |
return question_sets
|
14 |
|
15 |
exams = load_question_sets_vce('questions/')
|
@@ -27,9 +27,27 @@ def select_exam_vce(exam_name):
|
|
27 |
return [] # Return an empty list to indicate no questions were found
|
28 |
|
29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
# Text-to-speech function with rate limiting, retry mechanism, and client rotation
|
31 |
import time
|
32 |
import httpx
|
|
|
33 |
# Text-to-speech clients
|
34 |
client_fast = None # Initialize client_fast to None
|
35 |
client_2 = None
|
@@ -48,93 +66,12 @@ except Exception as e: # Catch other potential exceptions during client initiali
|
|
48 |
print(f"An unexpected error occurred during fast TTS client (client_fast) initialization: {e}")
|
49 |
print("Fast Text-to-Speech will be unavailable.")
|
50 |
|
51 |
-
|
52 |
# Retry logic for client_2 initialization
|
53 |
max_retries = 3
|
54 |
retry_delay = 5 # seconds
|
55 |
-
|
56 |
for attempt in range(max_retries):
|
57 |
try:
|
58 |
client_2 = Client("ruslanmv/Text-To-Speech")
|
59 |
clients.append(client_2)
|
60 |
print("Loaded TTS client (client_2) ✔")
|
61 |
-
break # If successful, break
|
62 |
-
except httpx.ReadTimeout as e:
|
63 |
-
print(f"Attempt {attempt + 1} failed with ReadTimeout: {e}")
|
64 |
-
if attempt < max_retries - 1:
|
65 |
-
print(f"Retrying in {retry_delay} seconds...")
|
66 |
-
time.sleep(retry_delay)
|
67 |
-
else:
|
68 |
-
print("Max retries reached for TTS client (client_2). It may not be available.")
|
69 |
-
client_2 = None # Ensure client_2 is None if all retries fail
|
70 |
-
except ValueError as e:
|
71 |
-
print(f"Error loading TTS client (client_2): {e}")
|
72 |
-
client_2 = None
|
73 |
-
break # No point retrying if it's a ValueError, break out of the loop
|
74 |
-
except Exception as e: # Catch other potential exceptions during client initialization
|
75 |
-
print(f"An unexpected error occurred during TTS client (client_2) initialization: {e}")
|
76 |
-
client_2 = None # Ensure client_2 is None if initialization fails
|
77 |
-
break # No point retrying if it's not a timeout issue, break out of the loop
|
78 |
-
|
79 |
-
|
80 |
-
# Initialize client_3 with error handling
|
81 |
-
try:
|
82 |
-
client_3 = Client("ruslanmv/Text-to-Voice-Transformers")
|
83 |
-
clients.append(client_3)
|
84 |
-
print("Loaded voice transformer TTS client (client_3) ✔")
|
85 |
-
except ValueError as e:
|
86 |
-
print(f"Error loading voice transformer TTS client (client_3): {e}")
|
87 |
-
print("Voice Transformer Text-to-Speech (client_3) will be unavailable.")
|
88 |
-
except Exception as e: # Catch other potential exceptions during client initialization
|
89 |
-
print(f"An unexpected error occurred during voice transformer TTS client (client_3) initialization: {e}")
|
90 |
-
print("Voice Transformer Text-to-Speech (client_3) will be unavailable.")
|
91 |
-
|
92 |
-
|
93 |
-
if not clients:
|
94 |
-
print("No Text-to-speech clients loaded due to errors. Audio functionality will be disabled.")
|
95 |
-
else:
|
96 |
-
print(f"Loaded {len(clients)} TTS clients.")
|
97 |
-
|
98 |
-
|
99 |
-
# Text-to-speech function with rate limiting, retry mechanism, and client rotation
|
100 |
-
def text_to_speech(text, retries=3, delay=5):
|
101 |
-
global clients # Ensure we are using the global clients list
|
102 |
-
if not clients: # If no clients are loaded, return None immediately
|
103 |
-
print("Warning: No Text-to-speech clients available.")
|
104 |
-
return None
|
105 |
-
|
106 |
-
client_index = 0 # Start with the first available client
|
107 |
-
for attempt in range(retries):
|
108 |
-
try:
|
109 |
-
client = clients[client_index % len(clients)] # Use modulo to cycle through available clients
|
110 |
-
client_index += 1 # Increment client_index for the next attempt in case of rate limit
|
111 |
-
print(f"Attempt {attempt + 1} using client: {client_index % len(clients) +1}") # Client index for logging (1-based)
|
112 |
-
|
113 |
-
if client == client_fast: # Check if using client_fast
|
114 |
-
result = client.predict(
|
115 |
-
language="English",
|
116 |
-
repo_id="csukuangfj/vits-piper-en_US-hfc_female-medium|1 speaker",
|
117 |
-
text=text,
|
118 |
-
sid="0",
|
119 |
-
speed=0.8,
|
120 |
-
api_name="/process"
|
121 |
-
)
|
122 |
-
else: # Assuming client_2 or client_3 or any other client in the future
|
123 |
-
result = client.predict(
|
124 |
-
text=text,
|
125 |
-
api_name="/predict"
|
126 |
-
)
|
127 |
-
return result
|
128 |
-
except httpx.HTTPStatusError as e:
|
129 |
-
if e.response.status_code == 429:
|
130 |
-
print(f"Rate limit exceeded using client {client_index % len(clients) + 1}. Retrying in {delay} seconds...")
|
131 |
-
time.sleep(delay)
|
132 |
-
else:
|
133 |
-
raise e
|
134 |
-
except Exception as e: # Catch any other potential errors during prediction
|
135 |
-
print(f"Error during text-to-speech prediction using client {client_index % len(clients) + 1}: {e}")
|
136 |
-
# Consider rotating client on any error, not just rate limits, to try other clients if available
|
137 |
-
client_index += 1 # Rotate to the next client for the next attempt
|
138 |
-
|
139 |
-
print("Max retries exceeded for all TTS clients. Could not process the request.")
|
140 |
-
return None
|
|
|
9 |
for root, dirs, files in os.walk(directory):
|
10 |
for file in files:
|
11 |
if file.endswith(".json"):
|
12 |
+
question_sets.append(os.path.join(file)[:-5]) # remove the .json extension
|
13 |
return question_sets
|
14 |
|
15 |
exams = load_question_sets_vce('questions/')
|
|
|
27 |
return [] # Return an empty list to indicate no questions were found
|
28 |
|
29 |
|
30 |
+
def display_question(index, audio_enabled, selected_questions):
|
31 |
+
"""Displays the question, options, and generates audio if enabled."""
|
32 |
+
if 0 <= index < len(selected_questions):
|
33 |
+
question_data = selected_questions[index]
|
34 |
+
question_text = question_data["question"]
|
35 |
+
options = question_data["options"]
|
36 |
+
audio_path = text_to_speech(question_text) if audio_enabled else None
|
37 |
+
return question_text, options, audio_path
|
38 |
+
else:
|
39 |
+
return "Question index out of range.", [], None
|
40 |
+
|
41 |
+
def get_explanation_and_answer(index, selected_questions):
|
42 |
+
"""Retrieves the explanation for a given question index."""
|
43 |
+
if 0 <= index < len(selected_questions):
|
44 |
+
return selected_questions[index]["explanation"], selected_questions[index]["answer"]
|
45 |
+
return "No explanation available.", ""
|
46 |
+
|
47 |
# Text-to-speech function with rate limiting, retry mechanism, and client rotation
|
48 |
import time
|
49 |
import httpx
|
50 |
+
|
51 |
# Text-to-speech clients
|
52 |
client_fast = None # Initialize client_fast to None
|
53 |
client_2 = None
|
|
|
66 |
print(f"An unexpected error occurred during fast TTS client (client_fast) initialization: {e}")
|
67 |
print("Fast Text-to-Speech will be unavailable.")
|
68 |
|
|
|
69 |
# Retry logic for client_2 initialization
|
70 |
max_retries = 3
|
71 |
retry_delay = 5 # seconds
|
|
|
72 |
for attempt in range(max_retries):
|
73 |
try:
|
74 |
client_2 = Client("ruslanmv/Text-To-Speech")
|
75 |
clients.append(client_2)
|
76 |
print("Loaded TTS client (client_2) ✔")
|
77 |
+
break # If successful, break
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|