File size: 4,229 Bytes
98ebf7c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import torch
import soundfile as sf
import gradio as gr
from transformers import AutoModel, AutoProcessor

# ==========================================
# Audio8 Model
# ==========================================

MODEL_ID = "Audio8/Audio8-TTS-Preview-0.6b"

# Select GPU if available
device = "cuda" if torch.cuda.is_available() else "cpu"

# Use BF16 on GPU, FP32 on CPU
dtype = torch.bfloat16 if device == "cuda" else torch.float32

print("=" * 60)
print("Audio8 Text-to-Speech")
print("=" * 60)

print("PyTorch:", torch.__version__)
print("CUDA Available:", torch.cuda.is_available())

if torch.cuda.is_available():
    print("GPU:", torch.cuda.get_device_name(0))

# ==========================================
# Load Processor
# ==========================================

print("\nLoading processor...")

processor = AutoProcessor.from_pretrained(
    MODEL_ID,
    trust_remote_code=True
)

# ==========================================
# Load Model
# ==========================================

print("Loading Audio8 model...")

model = AutoModel.from_pretrained(
    MODEL_ID,
    trust_remote_code=True,
    dtype=dtype
).eval().to(device)

print("βœ… Audio8 model loaded successfully!")

# ==========================================
# Generate Speech
# ==========================================

def generate_speech(text):

    # Check empty input
    if not text or not text.strip():
        return None

    print("\n" + "=" * 50)
    print("Generating speech...")
    print("Text:", text)

    # Process text
    inputs = processor(
        text=[text],
        return_tensors="pt"
    )

    # Move inputs to GPU/CPU
    inputs = {
        name: value.to(device)
        for name, value in inputs.items()
    }

    # Generate speech tokens
    with torch.inference_mode():

        output = model.generate(
            **inputs,
            max_new_tokens=1024,
            temperature=0.8,
            top_p=0.95,
            top_k=50,
            do_sample=True,
            return_dict_in_generate=True,
        )

    # Decode generated audio
    waveforms, waveform_lengths = model.decode_audio(
        output.codes
    )

    # Convert to NumPy
    audio = waveforms[
        0,
        :int(waveform_lengths[0])
    ].float().cpu().numpy()

    # ==========================================
    # Normalize Audio Volume
    # ==========================================

    peak = abs(audio).max()

    if peak > 0:
        audio = audio / peak
        audio = audio * 0.95

    # ==========================================
    # Save Audio
    # ==========================================

    output_file = "output.wav"

    sf.write(
        output_file,
        audio,
        model.config.codec_sample_rate
    )

    print("βœ… Speech generated!")
    print("🎡 Saved:", output_file)

    return output_file


# ==========================================
# Gradio User Interface
# ==========================================

with gr.Blocks(title="SmartVoiceAI") as app:

    gr.Markdown(
        """

        # πŸ”Š SmartVoiceAI



        ### AI Text-to-Speech



        Enter your text below and generate natural-sounding speech

        using the Audio8 TTS model.

        """
    )

    # Text input
    text_input = gr.Textbox(
        label="Enter Text",
        placeholder="Type something here...",
        lines=6
    )

    # Generate button
    generate_button = gr.Button(
        "πŸ”Š Generate Speech",
        variant="primary"
    )

    # Audio output
    audio_output = gr.Audio(
        label="Generated Voice",
        type="filepath",
        autoplay=True
    )

    # Button action
    generate_button.click(
        fn=generate_speech,
        inputs=text_input,
        outputs=audio_output
    )

    gr.Markdown(
        """

        ---

        **Model:** Audio8-TTS-Preview-0.6b  

        **Device:** CUDA / CPU

        """
    )


# ==========================================
# Start Application
# ==========================================

if __name__ == "__main__":

    app.launch()