Surendra Kumar commited on
Commit
be92df1
·
1 Parent(s): c6ced65

Add application file

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