IliaLarchenko commited on
Commit
11f6522
1 Parent(s): cb330d2

Empty text TTS fix

Browse files
Files changed (1) hide show
  1. api/audio.py +26 -25
api/audio.py CHANGED
@@ -175,36 +175,37 @@ class TTSManager:
175
  if not text:
176
  yield b""
177
 
178
- if stream is None:
179
- stream = self.streaming
180
-
181
- headers = {"Authorization": "Bearer " + self.config.tts.key}
182
- data = {"model": self.config.tts.name, "input": text, "voice": "alloy", "response_format": "opus"}
183
 
184
- try:
185
- if not stream:
186
- if self.config.tts.type == "OPENAI_API":
187
- response = requests.post(self.config.tts.url + "/audio/speech", headers=headers, json=data)
188
- elif self.config.tts.type == "HF_API":
189
- response = requests.post(self.config.tts.url, headers=headers, json={"inputs": text})
190
 
191
- if response.status_code != 200:
192
- error_details = response.json().get("error", "No error message provided")
193
- raise APIError(f"TTS Error: {self.config.tts.type} error", status_code=response.status_code, details=error_details)
194
- yield response.content
195
- else:
196
- if self.config.tts.type != "OPENAI_API":
197
- raise APIError("TTS Error: Streaming not supported for this TTS type")
198
 
199
- with requests.post(self.config.tts.url + "/audio/speech", headers=headers, json=data, stream=True) as response:
200
  if response.status_code != 200:
201
  error_details = response.json().get("error", "No error message provided")
202
- raise APIError("TTS Error: OPENAI API error", status_code=response.status_code, details=error_details)
203
- yield from response.iter_content(chunk_size=1024)
204
- except APIError:
205
- raise
206
- except Exception as e:
207
- raise APIError(f"TTS Error: Unexpected error: {e}")
 
 
 
 
 
 
 
 
 
208
 
209
  def read_last_message(self, chat_history: List[List[Optional[str]]]) -> Generator[bytes, None, None]:
210
  """
 
175
  if not text:
176
  yield b""
177
 
178
+ else:
179
+ if stream is None:
180
+ stream = self.streaming
 
 
181
 
182
+ headers = {"Authorization": "Bearer " + self.config.tts.key}
183
+ data = {"model": self.config.tts.name, "input": text, "voice": "alloy", "response_format": "opus"}
 
 
 
 
184
 
185
+ try:
186
+ if not stream:
187
+ if self.config.tts.type == "OPENAI_API":
188
+ response = requests.post(self.config.tts.url + "/audio/speech", headers=headers, json=data)
189
+ elif self.config.tts.type == "HF_API":
190
+ response = requests.post(self.config.tts.url, headers=headers, json={"inputs": text})
 
191
 
 
192
  if response.status_code != 200:
193
  error_details = response.json().get("error", "No error message provided")
194
+ raise APIError(f"TTS Error: {self.config.tts.type} error", status_code=response.status_code, details=error_details)
195
+ yield response.content
196
+ else:
197
+ if self.config.tts.type != "OPENAI_API":
198
+ raise APIError("TTS Error: Streaming not supported for this TTS type")
199
+
200
+ with requests.post(self.config.tts.url + "/audio/speech", headers=headers, json=data, stream=True) as response:
201
+ if response.status_code != 200:
202
+ error_details = response.json().get("error", "No error message provided")
203
+ raise APIError("TTS Error: OPENAI API error", status_code=response.status_code, details=error_details)
204
+ yield from response.iter_content(chunk_size=1024)
205
+ except APIError:
206
+ raise
207
+ except Exception as e:
208
+ raise APIError(f"TTS Error: Unexpected error: {e}")
209
 
210
  def read_last_message(self, chat_history: List[List[Optional[str]]]) -> Generator[bytes, None, None]:
211
  """