Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import transformers
|
3 |
+
from transformers import VitsModel, AutoTokenizer
|
4 |
+
import torch
|
5 |
+
import numpy as np
|
6 |
+
import io
|
7 |
+
import soundfile as sf
|
8 |
+
|
9 |
+
# Load model and tokenizer
|
10 |
+
model = VitsModel.from_pretrained("facebook/mms-tts-eng")
|
11 |
+
tokenizer = AutoTokenizer.from_pretrained("facebook/mms-tts-eng")
|
12 |
+
|
13 |
+
def generate_speech(text):
|
14 |
+
inputs = tokenizer(text, return_tensors="pt")
|
15 |
+
|
16 |
+
with torch.no_grad():
|
17 |
+
output = model(**inputs).waveform # Corrected typo: waveform (not waveformform)
|
18 |
+
|
19 |
+
# Convert the waveform tensor to a NumPy array
|
20 |
+
waveform = output.squeeze().cpu().numpy()
|
21 |
+
|
22 |
+
# Convert the waveform to bytes
|
23 |
+
audio_bytes_io = io.BytesIO()
|
24 |
+
sf.write(audio_bytes_io, waveform, samplerate=22050, format='WAV')
|
25 |
+
audio_bytes_io.seek(0)
|
26 |
+
|
27 |
+
return audio_bytes_io
|
28 |
+
|
29 |
+
st.title("Text-to-Speech Converter")
|
30 |
+
st.write("Developed by Safwan Ahmad Saffi")
|
31 |
+
st.write("Enter text below and click 'Generate Speech' to convert it to audio.")
|
32 |
+
|
33 |
+
# Text input
|
34 |
+
text_input = st.text_area("Text to convert:", "Some example text in the English language")
|
35 |
+
|
36 |
+
if st.button("Generate Speech"):
|
37 |
+
if text_input:
|
38 |
+
st.write("Generating speech...")
|
39 |
+
audio_bytes_io = generate_speech(text_input)
|
40 |
+
|
41 |
+
# Display audio in Streamlit
|
42 |
+
st.audio(audio_bytes_io, format="audio/wav")
|
43 |
+
else:
|
44 |
+
st.write("Please enter some text.")
|