Spaces:
Runtime error

File size: 1,109 Bytes
8c22261
0d36e96
 
 
cefe3aa
 
 
8c22261
0d36e96
 
 
 
8c22261
0d36e96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import torch
import torchaudio
from transformers import Wav2Vec2ForCTC, Wav2Vec2Tokenizer
import subprocess

subprocess.check_call(['pip', 'install', '-r', 'requirements.txt'])

# 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()