Update app.py
Browse files
app.py
CHANGED
|
@@ -1,82 +1,80 @@
|
|
| 1 |
-
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
import soundfile as sf
|
| 5 |
import tempfile
|
| 6 |
-
from
|
| 7 |
-
from transformers import AutoTokenizer
|
| 8 |
-
import os
|
| 9 |
-
from huggingface_hub import login
|
| 10 |
-
login(token=os.getenv("HF_TOKEN"))
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 14 |
|
| 15 |
print("🚀 Using device:", device)
|
| 16 |
-
print("⏳ Loading
|
| 17 |
|
| 18 |
-
model =
|
| 19 |
-
MODEL_NAME
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 23 |
-
|
| 24 |
-
description_tokenizer = AutoTokenizer.from_pretrained(
|
| 25 |
-
model.config.text_encoder._name_or_path
|
| 26 |
)
|
| 27 |
|
| 28 |
-
print("✅
|
| 29 |
|
| 30 |
|
| 31 |
# =========================================================
|
| 32 |
# TTS FUNCTION
|
| 33 |
# =========================================================
|
| 34 |
-
def
|
| 35 |
-
|
|
|
|
| 36 |
|
| 37 |
-
if
|
| 38 |
return None
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
)
|
| 45 |
|
| 46 |
-
|
| 47 |
-
description,
|
| 48 |
-
return_tensors="pt"
|
| 49 |
-
).to(device)
|
| 50 |
-
|
| 51 |
-
prompt_inputs = tokenizer(
|
| 52 |
-
prompt_text,
|
| 53 |
-
return_tensors="pt"
|
| 54 |
-
).to(device)
|
| 55 |
-
|
| 56 |
-
with torch.no_grad():
|
| 57 |
-
generation = model.generate(
|
| 58 |
-
input_ids=description_inputs.input_ids,
|
| 59 |
-
prompt_input_ids=prompt_inputs.input_ids
|
| 60 |
-
)
|
| 61 |
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
-
|
| 65 |
-
sf.write(temp_wav.name, audio, model.config.sampling_rate)
|
| 66 |
|
| 67 |
return temp_wav.name
|
| 68 |
|
| 69 |
|
| 70 |
-
|
|
|
|
|
|
|
| 71 |
demo = gr.Interface(
|
| 72 |
-
fn=
|
| 73 |
-
inputs=
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
outputs=gr.Audio(label="Generated Kannada Speech"),
|
| 78 |
-
title="Kannada Text To Speech
|
| 79 |
-
description="
|
| 80 |
)
|
| 81 |
|
| 82 |
-
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
import soundfile as sf
|
| 5 |
import tempfile
|
| 6 |
+
from transformers import AutoModel
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
# =========================================================
|
| 9 |
+
# CONFIG
|
| 10 |
+
# =========================================================
|
| 11 |
+
MODEL_NAME = "ai4bharat/IndicF5"
|
| 12 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 13 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 14 |
|
| 15 |
print("🚀 Using device:", device)
|
| 16 |
+
print("⏳ Loading IndicF5 model...")
|
| 17 |
|
| 18 |
+
model = AutoModel.from_pretrained(
|
| 19 |
+
MODEL_NAME,
|
| 20 |
+
trust_remote_code=True,
|
| 21 |
+
token=HF_TOKEN
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
)
|
| 23 |
|
| 24 |
+
print("✅ IndicF5 model loaded")
|
| 25 |
|
| 26 |
|
| 27 |
# =========================================================
|
| 28 |
# TTS FUNCTION
|
| 29 |
# =========================================================
|
| 30 |
+
def generate_indicf5_tts(text, ref_audio, ref_text):
|
| 31 |
+
if not text.strip():
|
| 32 |
+
return None
|
| 33 |
|
| 34 |
+
if ref_audio is None:
|
| 35 |
return None
|
| 36 |
|
| 37 |
+
audio = model(
|
| 38 |
+
text,
|
| 39 |
+
ref_audio_path=ref_audio,
|
| 40 |
+
ref_text=ref_text
|
| 41 |
)
|
| 42 |
|
| 43 |
+
temp_wav = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
# normalize if int16
|
| 46 |
+
import numpy as np
|
| 47 |
+
audio = np.array(audio)
|
| 48 |
+
if audio.dtype == np.int16:
|
| 49 |
+
audio = audio.astype(np.float32) / 32768.0
|
| 50 |
|
| 51 |
+
sf.write(temp_wav.name, audio, 24000)
|
|
|
|
| 52 |
|
| 53 |
return temp_wav.name
|
| 54 |
|
| 55 |
|
| 56 |
+
# =========================================================
|
| 57 |
+
# UI
|
| 58 |
+
# =========================================================
|
| 59 |
demo = gr.Interface(
|
| 60 |
+
fn=generate_indicf5_tts,
|
| 61 |
+
inputs=[
|
| 62 |
+
gr.Textbox(
|
| 63 |
+
label="Text to Synthesize (Kannada)",
|
| 64 |
+
placeholder="ನಮಸ್ಕಾರ, ಇದು ನನ್ನ ಕನ್ನಡ TTS ಪ್ರಾಜೆಕ್ಟ್"
|
| 65 |
+
),
|
| 66 |
+
gr.Audio(
|
| 67 |
+
type="filepath",
|
| 68 |
+
label="Reference Prompt Audio"
|
| 69 |
+
),
|
| 70 |
+
gr.Textbox(
|
| 71 |
+
label="Reference Audio Transcript",
|
| 72 |
+
placeholder="Reference audio spoken text"
|
| 73 |
+
)
|
| 74 |
+
],
|
| 75 |
outputs=gr.Audio(label="Generated Kannada Speech"),
|
| 76 |
+
title="IndicF5 Kannada Text To Speech",
|
| 77 |
+
description="Near-human Kannada TTS using AI4Bharat IndicF5"
|
| 78 |
)
|
| 79 |
|
| 80 |
+
demo.launch()
|