Download train.py from dinssync/tts: direct link, hf CLI and curl.
- Browser
- Download file 1.02 kB
-
https://huggingface.co/spaces/dinssync/tts/resolve/main/train.py
- Command line
-
hf download hf://spaces/dinssync/tts/train.py
-
curl -L -o train.py https://huggingface.co/spaces/dinssync/tts/resolve/main/train.py
1.02 kB
| import gradio as gr | |
| import torch | |
| import torchaudio | |
| from transformers import Wav2Vec2ForCTC, Wav2Vec2Tokenizer | |
| # Muat model Wav2Vec2 yang sudah dilatih sebelumnya | |
| model_name = "facebook/wav2vec2-base-960h" | |
| model = Wav2Vec2ForCTC.from_pretrained(model_name) | |
| tokenizer = Wav2Vec2Tokenizer.from_pretrained(model_name) | |
| # Buat fungsi untuk mengonversi teks ke ucapan | |
| def text_to_speech(text): | |
| # Encode teks menjadi token | |
| input_ids = tokenizer.encode(text, return_tensors="pt") | |
| # Generate ucapan | |
| with torch.no_grad(): | |
| logits = model.generate(input_ids) | |
| audio = model.get_feat_extract_output(logits) | |
| return audio[0].numpy() | |
| # Buat Gradio UI | |
| iface = gr.Interface( | |
| fn=text_to_speech, | |
| inputs=gr.inputs.Textbox(), | |
| outputs=gr.outputs.Audio(player=True), | |
| live=True, | |
| title="Text-to-Speech (TTS) with Wav2Vec2", | |
| description="Masukkan teks dan nikmati ucapan yang dihasilkan oleh model TTS Wav2Vec2." | |
| ) | |
| # Jalankan aplikasi | |
| if __name__ == "__main__": | |
| iface.launch() | |