Renderlib-dev commited on
Commit
62c042a
·
verified ·
1 Parent(s): c9f7c1d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -13
app.py CHANGED
@@ -2,18 +2,25 @@ import gradio as gr
2
  from supertonic import TTS
3
  import os
4
  import numpy as np
 
 
 
 
 
 
5
 
6
- # Initialize TTS - auto_download=True handles the HF model fetching automatically
7
- # This runs on CPU by default via ONNX
8
  try:
 
 
 
 
9
  tts = TTS(auto_download=True)
 
10
  except Exception as e:
11
  print(f"Error initializing TTS: {e}")
12
 
13
- # All available voices found in the repository tree
14
  VOICES = ["M1", "M2", "M3", "M4", "M5", "F1", "F2", "F3", "F4", "F5"]
15
 
16
- # The 31 supported languages
17
  LANGUAGES = {
18
  "English": "en", "Korean": "ko", "Japanese": "ja", "Arabic": "ar",
19
  "Bulgarian": "bg", "Czech": "cs", "Danish": "da", "German": "de",
@@ -25,6 +32,7 @@ LANGUAGES = {
25
  "Turkish": "tr", "Ukrainian": "uk", "Vietnamese": "vi"
26
  }
27
 
 
28
  def generate_speech(text, voice, language_name):
29
  if not text.strip():
30
  raise gr.Error("Please enter some text.")
@@ -32,17 +40,15 @@ def generate_speech(text, voice, language_name):
32
  try:
33
  lang_code = LANGUAGES[language_name]
34
 
35
- # Get the voice style object
36
  style = tts.get_voice_style(voice_name=voice)
37
-
38
- # Synthesize (Returns wav data and a numpy array for duration)
39
  wav, duration = tts.synthesize(text, voice_style=style, lang=lang_code)
40
 
41
- # Save to a temporary path
42
- output_path = "output.wav"
 
 
43
  tts.save_audio(wav, output_path)
44
 
45
- # FIX: Convert numpy.ndarray duration to float for f-string compatibility
46
  readable_duration = float(duration)
47
 
48
  return output_path, f"Generation Successful! \nDuration: {readable_duration:.2f}s"
@@ -50,10 +56,10 @@ def generate_speech(text, voice, language_name):
50
  except Exception as e:
51
  raise gr.Error(f"Generation failed: {str(e)}")
52
 
53
- # Define the Gradio Interface
54
- with gr.Blocks(theme='soft', title="Supertonic 3 TTS") as demo:
55
  gr.Markdown("# 🎙️ Supertonic 3: Multilingual TTS")
56
- gr.Markdown("An on-device, lightweight Text-to-Speech system by **Supertone**. Running on CPU via ONNX.")
57
 
58
  with gr.Row():
59
  with gr.Column(scale=1):
 
2
  from supertonic import TTS
3
  import os
4
  import numpy as np
5
+ import tempfile
6
+ import spaces # ZeroGPU के लिए अनिवार्य
7
+ import torch
8
+
9
+ # GPU डिटेक्ट करना और TTS को GPU पर लोड करना
10
+ device = "cuda" if torch.cuda.is_available() else "cpu"
11
 
 
 
12
  try:
13
+ # अगर Supertonic लाइब्रेरी device पैरामीटर सपोर्ट करती है
14
+ tts = TTS(auto_download=True, device=device)
15
+ except TypeError:
16
+ # अगर यह डिफ़ॉल्ट रूप से ONNX का उपयोग कर रही है
17
  tts = TTS(auto_download=True)
18
+ print("Warning: Initialization using default backend.")
19
  except Exception as e:
20
  print(f"Error initializing TTS: {e}")
21
 
 
22
  VOICES = ["M1", "M2", "M3", "M4", "M5", "F1", "F2", "F3", "F4", "F5"]
23
 
 
24
  LANGUAGES = {
25
  "English": "en", "Korean": "ko", "Japanese": "ja", "Arabic": "ar",
26
  "Bulgarian": "bg", "Czech": "cs", "Danish": "da", "German": "de",
 
32
  "Turkish": "tr", "Ukrainian": "uk", "Vietnamese": "vi"
33
  }
34
 
35
+ @spaces.GPU # यह लाइन जनरेशन के समय ZeroGPU को एक्टिवेट करेगी
36
  def generate_speech(text, voice, language_name):
37
  if not text.strip():
38
  raise gr.Error("Please enter some text.")
 
40
  try:
41
  lang_code = LANGUAGES[language_name]
42
 
 
43
  style = tts.get_voice_style(voice_name=voice)
 
 
44
  wav, duration = tts.synthesize(text, voice_style=style, lang=lang_code)
45
 
46
+ # ERROR FIX: हर ऑडियो के लिए एक यूनीक टेम्परेरी फाइल बनाना
47
+ temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
48
+ output_path = temp_file.name
49
+
50
  tts.save_audio(wav, output_path)
51
 
 
52
  readable_duration = float(duration)
53
 
54
  return output_path, f"Generation Successful! \nDuration: {readable_duration:.2f}s"
 
56
  except Exception as e:
57
  raise gr.Error(f"Generation failed: {str(e)}")
58
 
59
+ # Gradio Interface
60
+ with gr.Blocks(theme='soft', title="Supertonic 3 TTS (GPU Accelerated)") as demo:
61
  gr.Markdown("# 🎙️ Supertonic 3: Multilingual TTS")
62
+ gr.Markdown("An on-device, lightweight Text-to-Speech system by **Supertone**. Accelerated via **ZeroGPU**.")
63
 
64
  with gr.Row():
65
  with gr.Column(scale=1):