SurendraKumarDhaka commited on
Commit
6d1ceea
·
1 Parent(s): 1ee4c02

Upload app.py.py

Browse files
Files changed (1) hide show
  1. app.py.py +143 -0
app.py.py ADDED
@@ -0,0 +1,143 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ """
21
+ This is an AI tool. This tool will convert your text into audio. You can also drop you text file here and download the audio file.
22
+ """
23
+ )
24
+ model = SpeechT5ForTextToSpeech.from_pretrained("microsoft/speecht5_tts")
25
+ processor = SpeechT5Processor.from_pretrained("microsoft/speecht5_tts")
26
+ vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan")
27
+
28
+ speaker_embeddings = np.load("cmu_us_slt_arctic-wav-arctic_a0499.npy")
29
+ speaker_embeddings = torch.tensor(speaker_embeddings).unsqueeze(0)
30
+
31
+ text = st.text_area("Type your text..")
32
+ st.button("Convert")
33
+ inputs = processor(text=text, return_tensors="pt")
34
+ spectrogram = model.generate_speech(inputs["input_ids"], speaker_embeddings)
35
+ with torch.no_grad():
36
+ speech = vocoder(spectrogram)
37
+ sf.write("speech.wav", speech.numpy(), samplerate=16000)
38
+
39
+ audio_file = open('speech.wav', 'rb')
40
+ audio_bytes = audio_file.read()
41
+ st.audio(audio_bytes, format='audio/wav')
42
+
43
+
44
+ uploaded_file=st.file_uploader("Upload your text file here",type=['txt'] )
45
+ if uploaded_file is not None:
46
+ stringio = StringIO(uploaded_file.getvalue().decode("utf-8"))
47
+ #To read file as string:
48
+ text = stringio.read()
49
+ st.write(text)
50
+
51
+ st.button("Convert",key=1)
52
+ inputs = processor(text=text, return_tensors="pt")
53
+ spectrogram = model.generate_speech(inputs["input_ids"], speaker_embeddings)
54
+ with torch.no_grad():
55
+ speech = vocoder(spectrogram)
56
+ sf.write("speech.wav", speech.numpy(), samplerate=16000)
57
+ audio_file = open('speech.wav', 'rb')
58
+ audio_bytes = audio_file.read()
59
+ st.audio(audio_bytes, format='audio/wav')
60
+
61
+
62
+
63
+
64
+
65
+ st.text("Thanks for using")
66
+
67
+ if st.button("About"):
68
+ st.text("Created by Surendra Kumar")
69
+ ## footer
70
+ from htbuilder import HtmlElement, div, ul, li, br, hr, a, p, img, styles, classes, fonts
71
+ from htbuilder.units import percent, px
72
+ from htbuilder.funcs import rgba, rgb
73
+
74
+
75
+ def image(src_as_string, **style):
76
+ return img(src=src_as_string, style=styles(**style))
77
+
78
+
79
+ def link(link, text, **style):
80
+ return a(_href=link, _target="_blank", style=styles(**style))(text)
81
+
82
+
83
+ def layout(*args):
84
+ style = """
85
+ <style>
86
+ # MainMenu {visibility: hidden;}
87
+ footer {visibility: hidden;}
88
+ .stApp { bottom: 105px; }
89
+ </style>
90
+ """
91
+
92
+ style_div = styles(
93
+ position="fixed",
94
+ left=0,
95
+ bottom=0,
96
+ margin=px(0, 0, 0, 0),
97
+ width=percent(100),
98
+ color="black",
99
+ text_align="center",
100
+ height="auto",
101
+ opacity=1
102
+ )
103
+
104
+ style_hr = styles(
105
+ display="block",
106
+ margin=px(8, 8, "auto", "auto"),
107
+ border_style="solid",
108
+ border_width=px(0.5)
109
+ )
110
+
111
+ body = p()
112
+ foot = div(
113
+ style=style_div
114
+ )(
115
+ hr(
116
+ style=style_hr
117
+ ),
118
+ body
119
+ )
120
+ st.markdown(style,unsafe_allow_html=True)
121
+
122
+ for arg in args:
123
+ if isinstance(arg, str):
124
+ body(arg)
125
+
126
+ elif isinstance(arg, HtmlElement):
127
+ body(arg)
128
+
129
+ st.markdown(str(foot), unsafe_allow_html=True)
130
+
131
+
132
+ def footer():
133
+ myargs = [
134
+ "©️ surendraKumar",
135
+ br(),
136
+ link("https://www.linkedin.com/in/surendra-kumar-51802022b", image('https://icons.getbootstrap.com/assets/icons/linkedin.svg') ),
137
+ br(),
138
+ link("https://www.instagram.com/im_surendra_dhaka/",image('https://icons.getbootstrap.com/assets/icons/instagram.svg')),
139
+ ]
140
+ layout(*myargs)
141
+
142
+ if __name__ == "__main__":
143
+ footer()