Spaces:
Running
Running
Update tool2.py
Browse files
tool2.py
CHANGED
@@ -39,7 +39,7 @@ def display_question(index, audio_enabled, selected_questions):
|
|
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.", ""
|
@@ -82,4 +82,70 @@ for attempt in range(max_retries):
|
|
82 |
time.sleep(retry_delay)
|
83 |
else:
|
84 |
print("Max retries reached for TTS client (client_2). It may not be available.")
|
85 |
-
client_2 =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
return "Question index out of range.", [], None
|
40 |
|
41 |
def get_explanation_and_answer(index, selected_questions):
|
42 |
+
"""Retrieves the explanation and correct answer 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.", ""
|
|
|
82 |
time.sleep(retry_delay)
|
83 |
else:
|
84 |
print("Max retries reached for TTS client (client_2). It may not be available.")
|
85 |
+
client_2 = None # Ensure client_2 is None if all retries fail
|
86 |
+
except ValueError as e:
|
87 |
+
print(f"Error loading TTS client (client_2): {e}")
|
88 |
+
client_2 = None
|
89 |
+
break # No point retrying if it's a ValueError, break out of the loop
|
90 |
+
except Exception as e: # Catch other potential exceptions during client initialization
|
91 |
+
print(f"An unexpected error occurred during TTS client (client_2) initialization: {e}")
|
92 |
+
client_2 = None # Ensure client_2 is None if initialization fails
|
93 |
+
break # No point retrying if it's not a timeout issue, break out of the loop
|
94 |
+
|
95 |
+
# Initialize client_3 with error handling
|
96 |
+
try:
|
97 |
+
client_3 = Client("ruslanmv/Text-to-Voice-Transformers")
|
98 |
+
clients.append(client_3)
|
99 |
+
print("Loaded voice transformer TTS client (client_3) ✔")
|
100 |
+
except ValueError as e:
|
101 |
+
print(f"Error loading voice transformer TTS client (client_3): {e}")
|
102 |
+
print("Voice Transformer Text-to-Speech (client_3) will be unavailable.")
|
103 |
+
except Exception as e: # Catch other potential exceptions during client initialization
|
104 |
+
print(f"An unexpected error occurred during voice transformer TTS client (client_3) initialization: {e}")
|
105 |
+
print("Voice Transformer Text-to-Speech (client_3) will be unavailable.")
|
106 |
+
|
107 |
+
if not clients:
|
108 |
+
print("No Text-to-speech clients loaded due to errors. Audio functionality will be disabled.")
|
109 |
+
else:
|
110 |
+
print(f"Loaded {len(clients)} TTS clients.")
|
111 |
+
|
112 |
+
# Text-to-speech function with rate limiting, retry mechanism, and client rotation
|
113 |
+
def text_to_speech(text, retries=3, delay=5):
|
114 |
+
global clients # Ensure we are using the global clients list
|
115 |
+
if not clients: # If no clients are loaded, return None immediately
|
116 |
+
print("Warning: No Text-to-speech clients available.")
|
117 |
+
return None
|
118 |
+
|
119 |
+
client_index = 0 # Start with the first available client
|
120 |
+
for attempt in range(retries):
|
121 |
+
try:
|
122 |
+
client = clients[client_index % len(clients)] # Use modulo to cycle through available clients
|
123 |
+
client_index += 1 # Increment client_index for the next attempt in case of rate limit
|
124 |
+
print(f"Attempt {attempt + 1} using client: {client_index % len(clients) + 1}") # Client index for logging (1-based)
|
125 |
+
if client == client_fast: # Check if using client_fast
|
126 |
+
result = client.predict(
|
127 |
+
language="English",
|
128 |
+
repo_id="csukuangfj/vits-piper-en_US-hfc_female-medium|1 speaker",
|
129 |
+
text=text,
|
130 |
+
sid="0",
|
131 |
+
speed=0.8,
|
132 |
+
api_name="/process"
|
133 |
+
)
|
134 |
+
else: # Assuming client_2 or client_3 or any other client in the future
|
135 |
+
result = client.predict(
|
136 |
+
text=text,
|
137 |
+
api_name="/predict"
|
138 |
+
)
|
139 |
+
return result
|
140 |
+
except httpx.HTTPStatusError as e:
|
141 |
+
if e.response.status_code == 429:
|
142 |
+
print(f"Rate limit exceeded using client {client_index % len(clients) + 1}. Retrying in {delay} seconds...")
|
143 |
+
time.sleep(delay)
|
144 |
+
else:
|
145 |
+
raise e
|
146 |
+
except Exception as e: # Catch any other potential errors during prediction
|
147 |
+
print(f"Error during text-to-speech prediction using client {client_index % len(clients) + 1}: {e}")
|
148 |
+
# Consider rotating client on any error, not just rate limits, to try other clients if available
|
149 |
+
client_index += 1 # Rotate to the next client for the next attempt
|
150 |
+
print("Max retries exceeded for all TTS clients. Could not process the request.")
|
151 |
+
return None
|