Spaces:
Runtime error
Runtime error
Update tool2.py
Browse files
tool2.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
from gradio_client import Client
|
2 |
import os
|
3 |
import json
|
@@ -29,19 +30,87 @@ def select_exam_vce(exam_name):
|
|
29 |
# Text-to-speech function with rate limiting, retry mechanism, and client rotation
|
30 |
import time
|
31 |
import httpx
|
32 |
-
# Text-to-speech clients
|
33 |
-
|
34 |
-
client_2 =
|
35 |
-
client_3 =
|
36 |
-
clients = [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
# Text-to-speech function with rate limiting, retry mechanism, and client rotation
|
38 |
def text_to_speech(text, retries=3, delay=5):
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
40 |
for attempt in range(retries):
|
41 |
try:
|
42 |
-
client = clients[client_index]
|
43 |
-
|
44 |
-
|
|
|
|
|
45 |
result = client.predict(
|
46 |
language="English",
|
47 |
repo_id="csukuangfj/vits-piper-en_US-hfc_female-medium|1 speaker",
|
@@ -50,7 +119,7 @@ def text_to_speech(text, retries=3, delay=5):
|
|
50 |
speed=0.8,
|
51 |
api_name="/process"
|
52 |
)
|
53 |
-
else:
|
54 |
result = client.predict(
|
55 |
text=text,
|
56 |
api_name="/predict"
|
@@ -58,11 +127,14 @@ def text_to_speech(text, retries=3, delay=5):
|
|
58 |
return result
|
59 |
except httpx.HTTPStatusError as e:
|
60 |
if e.response.status_code == 429:
|
61 |
-
print(f"Rate limit exceeded. Retrying in {delay} seconds...")
|
62 |
-
client_index = (client_index + 1) % len(clients) # Rotate to the next client
|
63 |
time.sleep(delay)
|
64 |
else:
|
65 |
raise e
|
|
|
|
|
|
|
|
|
66 |
|
67 |
-
print("Max retries exceeded. Could not process the request.")
|
68 |
return None
|
|
|
1 |
+
# tool2.py
|
2 |
from gradio_client import Client
|
3 |
import os
|
4 |
import json
|
|
|
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
|
36 |
+
client_3 = None
|
37 |
+
clients = []
|
38 |
+
|
39 |
+
# Initialize client_fast with error handling
|
40 |
+
try:
|
41 |
+
client_fast = Client("https://ruslanmv-text-to-speech-fast.hf.space/")
|
42 |
+
clients.append(client_fast)
|
43 |
+
print("Loaded fast TTS client (client_fast) ✔")
|
44 |
+
except ValueError as e:
|
45 |
+
print(f"Error loading fast TTS client (client_fast): {e}")
|
46 |
+
print("Fast Text-to-Speech will be unavailable.")
|
47 |
+
except Exception as e: # Catch other potential exceptions during client initialization
|
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 out of the retry loop
|
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",
|
|
|
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"
|
|
|
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
|