tsuching commited on
Commit
f340b44
·
verified ·
1 Parent(s): 6e312f5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -5
app.py CHANGED
@@ -288,17 +288,37 @@ def get_all_phonetics_schemes(text):
288
 
289
 
290
  # Tibetan TTS function
291
- def run_task_tts(text):
292
  # Always return: [audio_numpy, audio_filepath, text_output]
293
  # 1) Generate speech via MMS-TTS
294
- speech = tts_tibetan(text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
295
  # 2) Clip, cast, flatten for Gradio (browser playback expects float32 in [-1, 1])
296
  audio = speech["audio"]
297
  sr = int(speech["sampling_rate"])
298
  audio = np.clip(audio.astype(np.float32), -1.0, 1.0).flatten()
299
- # 3) Write a WAV file for download/Flutter using PCM_16 to avoid pydub header errors
 
300
  tmpfile = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
301
  sf.write(tmpfile.name, audio, sr, subtype="PCM_16")
 
302
  # 4) Return both audio forms + a status message
303
  return (sr, audio), tmpfile.name, "Tibetan audio generated successfully!"
304
 
@@ -567,15 +587,36 @@ def generate_tts_file(text: str) -> str:
567
  _, file_path, _ = run_task_tts(text) # unpack tuple
568
  return file_path
569
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
570
  @api.post("/api/tts")
571
  async def api_tts(request: gr.Request):
572
  body = await request.json()
573
  text = body.get("text", "")
574
 
575
- if not text:
 
 
 
 
576
  return {"error": "No text provided"}
577
 
578
- output_path = generate_tts_file(text)
 
579
 
580
  return FileResponse(
581
  output_path,
@@ -584,6 +625,7 @@ async def api_tts(request: gr.Request):
584
  )
585
 
586
 
 
587
  #############################################
588
  # 🔥 Attach your existing Gradio UI
589
  #############################################
 
288
 
289
 
290
  # Tibetan TTS function
291
+ #def run_task_tts(text):
292
  # Always return: [audio_numpy, audio_filepath, text_output]
293
  # 1) Generate speech via MMS-TTS
294
+ # speech = tts_tibetan(text)
295
+ # 2) Clip, cast, flatten for Gradio (browser playback expects float32 in [-1, 1])
296
+ # audio = speech["audio"]
297
+ # sr = int(speech["sampling_rate"])
298
+ # audio = np.clip(audio.astype(np.float32), -1.0, 1.0).flatten()
299
+ # 3) Write a WAV file for download/Flutter using PCM_16 to avoid pydub header errors
300
+ # tmpfile = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
301
+ # sf.write(tmpfile.name, audio, sr, subtype="PCM_16")
302
+ # 4) Return both audio forms + a status message
303
+ # return (sr, audio), tmpfile.name, "Tibetan audio generated successfully!"
304
+
305
+ def run_task_tts(text: str):
306
+ # Ensure input is a string
307
+ if not isinstance(text, str):
308
+ text = str(text)
309
+
310
+ # 1) Generate speech via MMS-TTS
311
+ speech = tts_tibetan(text) # pipeline expects plain string
312
+
313
  # 2) Clip, cast, flatten for Gradio (browser playback expects float32 in [-1, 1])
314
  audio = speech["audio"]
315
  sr = int(speech["sampling_rate"])
316
  audio = np.clip(audio.astype(np.float32), -1.0, 1.0).flatten()
317
+
318
+ # 3) Write a WAV file for download/Flutter using PCM_16
319
  tmpfile = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
320
  sf.write(tmpfile.name, audio, sr, subtype="PCM_16")
321
+
322
  # 4) Return both audio forms + a status message
323
  return (sr, audio), tmpfile.name, "Tibetan audio generated successfully!"
324
 
 
587
  _, file_path, _ = run_task_tts(text) # unpack tuple
588
  return file_path
589
 
590
+ #@api.post("/api/tts")
591
+ #async def api_tts(request: gr.Request):
592
+ # body = await request.json()
593
+ # text = body.get("text", "")
594
+
595
+ # if not text:
596
+ # return {"error": "No text provided"}
597
+
598
+ # output_path = generate_tts_file(text)
599
+
600
+ # return FileResponse(
601
+ # output_path,
602
+ # media_type="audio/wav",
603
+ # filename="tts.wav"
604
+ # )
605
+
606
  @api.post("/api/tts")
607
  async def api_tts(request: gr.Request):
608
  body = await request.json()
609
  text = body.get("text", "")
610
 
611
+ # Ensure text is always a string
612
+ if not isinstance(text, str):
613
+ text = str(text)
614
+
615
+ if not text.strip():
616
  return {"error": "No text provided"}
617
 
618
+ # Call your wrapper
619
+ _, output_path, status = run_task_tts(text)
620
 
621
  return FileResponse(
622
  output_path,
 
625
  )
626
 
627
 
628
+
629
  #############################################
630
  # 🔥 Attach your existing Gradio UI
631
  #############################################