ruslanmv commited on
Commit
c7d2ec8
·
verified ·
1 Parent(s): 1e346bd

Update tool2.py

Browse files
Files changed (1) hide show
  1. tool2.py +51 -1
tool2.py CHANGED
@@ -102,4 +102,54 @@ try:
102
  clients.append(client_3)
103
  print("Loaded voice transformer TTS client (client_3) ✔")
104
  except ValueError as e:
105
- print(f"Error loading voice transformer TTS client (
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
  clients.append(client_3)
103
  print("Loaded voice transformer TTS client (client_3) ✔")
104
  except ValueError as e:
105
+ print(f"Error loading voice transformer TTS client (client_3): {e}")
106
+ print("Voice Transformer Text-to-Speech (client_3) will be unavailable.")
107
+ except Exception as e: # Catch other potential exceptions during client initialization
108
+ print(f"An unexpected error occurred during voice transformer TTS client (client_3) initialization: {e}")
109
+ print("Voice Transformer Text-to-Speech (client_3) will be unavailable.")
110
+
111
+ if not clients:
112
+ print("No Text-to-speech clients loaded due to errors. Audio functionality will be disabled.")
113
+ else:
114
+ print(f"Loaded {len(clients)} TTS clients.")
115
+
116
+ # Text-to-speech function with rate limiting, retry mechanism, and client rotation
117
+ def text_to_speech(text, retries=3, delay=5):
118
+ global clients # Ensure we are using the global clients list
119
+ if not clients: # If no clients are loaded, return None immediately
120
+ print("Warning: No Text-to-speech clients available.")
121
+ return None
122
+
123
+ client_index = 0 # Start with the first available client
124
+ for attempt in range(retries):
125
+ try:
126
+ client = clients[client_index % len(clients)] # Use modulo to cycle through available clients
127
+ client_index += 1 # Increment client_index for the next attempt in case of rate limit
128
+ print(f"Attempt {attempt + 1} using client: {client_index % len(clients) + 1}") # Client index for logging (1-based)
129
+ if client == client_fast: # Check if using client_fast
130
+ result = client.predict(
131
+ language="English",
132
+ repo_id="csukuangfj/vits-piper-en_US-hfc_female-medium|1 speaker",
133
+ text=text,
134
+ sid="0",
135
+ speed=0.8,
136
+ api_name="/process"
137
+ )
138
+ else: # Assuming client_2 or client_3 or any other client in the future
139
+ result = client.predict(
140
+ text=text,
141
+ api_name="/predict"
142
+ )
143
+ return result
144
+ except httpx.HTTPStatusError as e:
145
+ if e.response.status_code == 429:
146
+ print(f"Rate limit exceeded using client {client_index % len(clients) + 1}. Retrying in {delay} seconds...")
147
+ time.sleep(delay)
148
+ else:
149
+ raise e
150
+ except Exception as e: # Catch any other potential errors during prediction
151
+ print(f"Error during text-to-speech prediction using client {client_index % len(clients) + 1}: {e}")
152
+ # Consider rotating client on any error, not just rate limits, to try other clients if available
153
+ client_index += 1 # Rotate to the next client for the next attempt
154
+ print("Max retries exceeded for all TTS clients. Could not process the request.")
155
+ return None