meadevx commited on
Commit
80bb6f2
·
verified ·
1 Parent(s): 3646d51

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -0
app.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ import numpy as np
4
+ import scipy
5
+ from transformers import VitsModel, AutoTokenizer
6
+
7
+ # Load the model and tokenizer
8
+ model = VitsModel.from_pretrained("kakao-enterprise/vits-ljs")
9
+ tokenizer = AutoTokenizer.from_pretrained("kakao-enterprise/vits-ljs")
10
+
11
+ def text_to_speech(text):
12
+ # Tokenize the text
13
+ inputs = tokenizer(text, return_tensors="pt")
14
+
15
+ # Generate audio
16
+ with torch.no_grad():
17
+ output = model(**inputs).waveform
18
+
19
+ # Convert to numpy array and save as WAV file
20
+ audio_array = output.cpu().numpy().squeeze()
21
+ audio_array /= 1.414
22
+ audio_array *= 32767
23
+ audio_array = audio_array.astype(np.int16)
24
+
25
+ # Save to WAV file
26
+ output_file = "output/output.wav"
27
+ scipy.io.wavfile.write(output_file, rate=model.config.sampling_rate, data=audio_array)
28
+
29
+ # Return the path to the WAV file
30
+ return output_file
31
+
32
+ demo = gr.Interface(
33
+ text_to_speech,
34
+ gr.Textbox(label="Text to narrate"),
35
+ gr.Audio(label="Narrated audio"),
36
+ title="Text-to-Speech",
37
+ description="Enter text to generate audio narration",
38
+ )
39
+
40
+ demo.launch()