Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,125 +1,138 @@
|
|
1 |
-
import
|
2 |
-
import
|
|
|
|
|
3 |
import numpy as np
|
4 |
import torch
|
|
|
|
|
5 |
|
6 |
-
from transformers import SpeechT5Processor, SpeechT5ForTextToSpeech, SpeechT5HifiGan
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
12 |
vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan")
|
13 |
|
14 |
-
|
15 |
-
speaker_embeddings =
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
inputs = processor(text=text, return_tensors="pt")
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
value="BDL (male)"),
|
117 |
-
],
|
118 |
-
outputs=[
|
119 |
-
gr.Audio(label="Generated Speech", type="numpy"),
|
120 |
-
],
|
121 |
-
title=title,
|
122 |
-
description=description,
|
123 |
-
article=article,
|
124 |
-
examples=examples,
|
125 |
-
).launch()
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import time
|
3 |
+
from datetime import datetime
|
4 |
+
from transformers import SpeechT5Processor, SpeechT5ForSpeechToSpeech, SpeechT5HifiGan,SpeechT5ForTextToSpeech
|
5 |
import numpy as np
|
6 |
import torch
|
7 |
+
from io import StringIO
|
8 |
+
import soundfile as sf
|
9 |
|
|
|
10 |
|
11 |
+
html_temp= """
|
12 |
+
<div style="background-color:tomato;padding:10px">
|
13 |
+
<h2 style="color:white;text-align:centre;"> Text-to-Speech </h2>
|
14 |
+
</div>
|
15 |
+
"""
|
16 |
+
st.markdown(html_temp,unsafe_allow_html=True)
|
17 |
|
18 |
+
st.markdown(
|
19 |
+
"""
|
20 |
+
Text to Audio Conversion.
|
21 |
+
"""
|
22 |
+
)
|
23 |
+
model = SpeechT5ForTextToSpeech.from_pretrained("microsoft/speecht5_tts")
|
24 |
+
processor = SpeechT5Processor.from_pretrained("microsoft/speecht5_tts")
|
25 |
vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan")
|
26 |
|
27 |
+
speaker_embeddings = np.load("cmu_us_slt_arctic-wav-arctic_a0499.npy")
|
28 |
+
speaker_embeddings = torch.tensor(speaker_embeddings).unsqueeze(0)
|
29 |
+
|
30 |
+
text = st.text_area("Type your text..")
|
31 |
+
st.button("Convert")
|
32 |
+
inputs = processor(text=text, return_tensors="pt")
|
33 |
+
spectrogram = model.generate_speech(inputs["input_ids"], speaker_embeddings)
|
34 |
+
with torch.no_grad():
|
35 |
+
speech = vocoder(spectrogram)
|
36 |
+
sf.write("speech.wav", speech.numpy(), samplerate=16000)
|
37 |
+
|
38 |
+
audio_file = open('speech.wav', 'rb')
|
39 |
+
audio_bytes = audio_file.read()
|
40 |
+
st.audio(audio_bytes, format='audio/wav')
|
41 |
+
|
42 |
+
|
43 |
+
uploaded_file=st.file_uploader("Upload your text file here",type=['txt'] )
|
44 |
+
if uploaded_file is not None:
|
45 |
+
stringio = StringIO(uploaded_file.getvalue().decode("utf-8"))
|
46 |
+
#To read file as string:
|
47 |
+
text = stringio.read()
|
48 |
+
st.write(text)
|
49 |
+
|
50 |
+
st.button("Convert",key=1)
|
51 |
inputs = processor(text=text, return_tensors="pt")
|
52 |
+
spectrogram = model.generate_speech(inputs["input_ids"], speaker_embeddings)
|
53 |
+
with torch.no_grad():
|
54 |
+
speech = vocoder(spectrogram)
|
55 |
+
sf.write("speech.wav", speech.numpy(), samplerate=16000)
|
56 |
+
audio_file = open('speech.wav', 'rb')
|
57 |
+
audio_bytes = audio_file.read()
|
58 |
+
st.audio(audio_bytes, format='audio/wav')
|
59 |
+
|
60 |
+
|
61 |
+
|
62 |
+
|
63 |
+
|
64 |
+
st.text("Thanks for using")
|
65 |
+
|
66 |
+
if st.button("About"):
|
67 |
+
st.text("Created by Surendra Kumar")
|
68 |
+
## footer
|
69 |
+
from htbuilder import HtmlElement, div, ul, li, br, hr, a, p, img, styles, classes, fonts
|
70 |
+
from htbuilder.units import percent, px
|
71 |
+
from htbuilder.funcs import rgba, rgb
|
72 |
+
|
73 |
+
|
74 |
+
def image(src_as_string, **style):
|
75 |
+
return img(src=src_as_string, style=styles(**style))
|
76 |
+
|
77 |
+
|
78 |
+
def link(link, text, **style):
|
79 |
+
return a(_href=link, _target="_blank", style=styles(**style))(text)
|
80 |
+
|
81 |
+
|
82 |
+
def layout(*args):
|
83 |
+
style = """
|
84 |
+
<style>
|
85 |
+
# MainMenu {visibility: hidden;}
|
86 |
+
footer {visibility: hidden;}
|
87 |
+
.stApp { bottom: 105px; }
|
88 |
+
</style>
|
89 |
+
"""
|
90 |
+
|
91 |
+
style_div = styles(
|
92 |
+
position="fixed",
|
93 |
+
left=0,
|
94 |
+
bottom=0,
|
95 |
+
margin=px(0, 0, 0, 0),
|
96 |
+
width=percent(100),
|
97 |
+
color="black",
|
98 |
+
text_align="center",
|
99 |
+
height="auto",
|
100 |
+
opacity=1
|
101 |
+
)
|
102 |
+
|
103 |
+
style_hr = styles(
|
104 |
+
display="block",
|
105 |
+
margin=px(8, 8, "auto", "auto"),
|
106 |
+
border_style="solid",
|
107 |
+
border_width=px(0.5)
|
108 |
+
)
|
109 |
+
|
110 |
+
body = p()
|
111 |
+
foot = div(
|
112 |
+
style=style_div
|
113 |
+
)(
|
114 |
+
hr(
|
115 |
+
style=style_hr
|
116 |
+
),
|
117 |
+
body
|
118 |
+
)
|
119 |
+
st.markdown(style,unsafe_allow_html=True)
|
120 |
+
|
121 |
+
for arg in args:
|
122 |
+
if isinstance(arg, str):
|
123 |
+
body(arg)
|
124 |
+
|
125 |
+
elif isinstance(arg, HtmlElement):
|
126 |
+
body(arg)
|
127 |
+
|
128 |
+
st.markdown(str(foot), unsafe_allow_html=True)
|
129 |
+
|
130 |
+
|
131 |
+
def footer():
|
132 |
+
myargs = [
|
133 |
+
"©️ Apps Consultants",
|
134 |
+
]
|
135 |
+
layout(*myargs)
|
136 |
+
|
137 |
+
if __name__ == "__main__":
|
138 |
+
footer()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|