Spaces:
Runtime error

dinssync commited on
Commit
b7ed782
Β·
1 Parent(s): 8c22261

Create train.py

Browse files
Files changed (1) hide show
  1. train.py +35 -0
train.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ import torchaudio
4
+ from transformers import Wav2Vec2ForCTC, Wav2Vec2Tokenizer
5
+
6
+ # Muat model Wav2Vec2 yang sudah dilatih sebelumnya
7
+ model_name = "facebook/wav2vec2-base-960h"
8
+ model = Wav2Vec2ForCTC.from_pretrained(model_name)
9
+ tokenizer = Wav2Vec2Tokenizer.from_pretrained(model_name)
10
+
11
+ # Buat fungsi untuk mengonversi teks ke ucapan
12
+ def text_to_speech(text):
13
+ # Encode teks menjadi token
14
+ input_ids = tokenizer.encode(text, return_tensors="pt")
15
+
16
+ # Generate ucapan
17
+ with torch.no_grad():
18
+ logits = model.generate(input_ids)
19
+ audio = model.get_feat_extract_output(logits)
20
+
21
+ return audio[0].numpy()
22
+
23
+ # Buat Gradio UI
24
+ iface = gr.Interface(
25
+ fn=text_to_speech,
26
+ inputs=gr.inputs.Textbox(),
27
+ outputs=gr.outputs.Audio(player=True),
28
+ live=True,
29
+ title="Text-to-Speech (TTS) with Wav2Vec2",
30
+ description="Masukkan teks dan nikmati ucapan yang dihasilkan oleh model TTS Wav2Vec2."
31
+ )
32
+
33
+ # Jalankan aplikasi
34
+ if __name__ == "__main__":
35
+ iface.launch()