jinugmathew commited on
Commit
98ebf7c
Β·
verified Β·
1 Parent(s): f9cebef

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +190 -0
app.py ADDED
@@ -0,0 +1,190 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import soundfile as sf
3
+ import gradio as gr
4
+ from transformers import AutoModel, AutoProcessor
5
+
6
+ # ==========================================
7
+ # Audio8 Model
8
+ # ==========================================
9
+
10
+ MODEL_ID = "Audio8/Audio8-TTS-Preview-0.6b"
11
+
12
+ # Select GPU if available
13
+ device = "cuda" if torch.cuda.is_available() else "cpu"
14
+
15
+ # Use BF16 on GPU, FP32 on CPU
16
+ dtype = torch.bfloat16 if device == "cuda" else torch.float32
17
+
18
+ print("=" * 60)
19
+ print("Audio8 Text-to-Speech")
20
+ print("=" * 60)
21
+
22
+ print("PyTorch:", torch.__version__)
23
+ print("CUDA Available:", torch.cuda.is_available())
24
+
25
+ if torch.cuda.is_available():
26
+ print("GPU:", torch.cuda.get_device_name(0))
27
+
28
+ # ==========================================
29
+ # Load Processor
30
+ # ==========================================
31
+
32
+ print("\nLoading processor...")
33
+
34
+ processor = AutoProcessor.from_pretrained(
35
+ MODEL_ID,
36
+ trust_remote_code=True
37
+ )
38
+
39
+ # ==========================================
40
+ # Load Model
41
+ # ==========================================
42
+
43
+ print("Loading Audio8 model...")
44
+
45
+ model = AutoModel.from_pretrained(
46
+ MODEL_ID,
47
+ trust_remote_code=True,
48
+ dtype=dtype
49
+ ).eval().to(device)
50
+
51
+ print("βœ… Audio8 model loaded successfully!")
52
+
53
+ # ==========================================
54
+ # Generate Speech
55
+ # ==========================================
56
+
57
+ def generate_speech(text):
58
+
59
+ # Check empty input
60
+ if not text or not text.strip():
61
+ return None
62
+
63
+ print("\n" + "=" * 50)
64
+ print("Generating speech...")
65
+ print("Text:", text)
66
+
67
+ # Process text
68
+ inputs = processor(
69
+ text=[text],
70
+ return_tensors="pt"
71
+ )
72
+
73
+ # Move inputs to GPU/CPU
74
+ inputs = {
75
+ name: value.to(device)
76
+ for name, value in inputs.items()
77
+ }
78
+
79
+ # Generate speech tokens
80
+ with torch.inference_mode():
81
+
82
+ output = model.generate(
83
+ **inputs,
84
+ max_new_tokens=1024,
85
+ temperature=0.8,
86
+ top_p=0.95,
87
+ top_k=50,
88
+ do_sample=True,
89
+ return_dict_in_generate=True,
90
+ )
91
+
92
+ # Decode generated audio
93
+ waveforms, waveform_lengths = model.decode_audio(
94
+ output.codes
95
+ )
96
+
97
+ # Convert to NumPy
98
+ audio = waveforms[
99
+ 0,
100
+ :int(waveform_lengths[0])
101
+ ].float().cpu().numpy()
102
+
103
+ # ==========================================
104
+ # Normalize Audio Volume
105
+ # ==========================================
106
+
107
+ peak = abs(audio).max()
108
+
109
+ if peak > 0:
110
+ audio = audio / peak
111
+ audio = audio * 0.95
112
+
113
+ # ==========================================
114
+ # Save Audio
115
+ # ==========================================
116
+
117
+ output_file = "output.wav"
118
+
119
+ sf.write(
120
+ output_file,
121
+ audio,
122
+ model.config.codec_sample_rate
123
+ )
124
+
125
+ print("βœ… Speech generated!")
126
+ print("🎡 Saved:", output_file)
127
+
128
+ return output_file
129
+
130
+
131
+ # ==========================================
132
+ # Gradio User Interface
133
+ # ==========================================
134
+
135
+ with gr.Blocks(title="SmartVoiceAI") as app:
136
+
137
+ gr.Markdown(
138
+ """
139
+ # πŸ”Š SmartVoiceAI
140
+
141
+ ### AI Text-to-Speech
142
+
143
+ Enter your text below and generate natural-sounding speech
144
+ using the Audio8 TTS model.
145
+ """
146
+ )
147
+
148
+ # Text input
149
+ text_input = gr.Textbox(
150
+ label="Enter Text",
151
+ placeholder="Type something here...",
152
+ lines=6
153
+ )
154
+
155
+ # Generate button
156
+ generate_button = gr.Button(
157
+ "πŸ”Š Generate Speech",
158
+ variant="primary"
159
+ )
160
+
161
+ # Audio output
162
+ audio_output = gr.Audio(
163
+ label="Generated Voice",
164
+ type="filepath",
165
+ autoplay=True
166
+ )
167
+
168
+ # Button action
169
+ generate_button.click(
170
+ fn=generate_speech,
171
+ inputs=text_input,
172
+ outputs=audio_output
173
+ )
174
+
175
+ gr.Markdown(
176
+ """
177
+ ---
178
+ **Model:** Audio8-TTS-Preview-0.6b
179
+ **Device:** CUDA / CPU
180
+ """
181
+ )
182
+
183
+
184
+ # ==========================================
185
+ # Start Application
186
+ # ==========================================
187
+
188
+ if __name__ == "__main__":
189
+
190
+ app.launch()