Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import AutoProcessor, DiaForConditionalGeneration
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
# Device: use GPU if available
|
| 6 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 7 |
+
|
| 8 |
+
# Load model and processor
|
| 9 |
+
checkpoint = "nari-labs/Dia-1.6B-0626"
|
| 10 |
+
processor = AutoProcessor.from_pretrained(checkpoint)
|
| 11 |
+
model = DiaForConditionalGeneration.from_pretrained(checkpoint).to(device)
|
| 12 |
+
|
| 13 |
+
def tts_dialogue(script: str):
|
| 14 |
+
"""
|
| 15 |
+
Expects script formatted with [S1], [S2] tags for dialogue.
|
| 16 |
+
Example: "[S1] Hello there! [S2] Hi, how are you?"
|
| 17 |
+
"""
|
| 18 |
+
inputs = processor(text=script, return_tensors="pt", padding=True).to(device)
|
| 19 |
+
outputs = model.generate(
|
| 20 |
+
**inputs,
|
| 21 |
+
max_new_tokens=3072,
|
| 22 |
+
guidance_scale=3.0,
|
| 23 |
+
temperature=1.8,
|
| 24 |
+
top_p=0.9,
|
| 25 |
+
top_k=45
|
| 26 |
+
)
|
| 27 |
+
audio_list = processor.batch_decode(outputs) # returns list of audio bytes
|
| 28 |
+
return (audio_list[0],)
|
| 29 |
+
|
| 30 |
+
iface = gr.Interface(
|
| 31 |
+
fn=tts_dialogue,
|
| 32 |
+
inputs=gr.Textbox(label="Dialogue Script", placeholder="[S1] Hello [S2] Hi!"),
|
| 33 |
+
outputs=gr.Audio(label="Generated Audio"),
|
| 34 |
+
title="📢 Dia 1.6B TTS Dialogue Demo",
|
| 35 |
+
description="A demo using Dia 1.6B for expressive, multi‑speaker TTS"
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
if __name__ == "__main__":
|
| 39 |
+
iface.launch()
|