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+
import streamlit as st
|
2 |
+
import os
|
3 |
+
import time
|
4 |
+
import glob
|
5 |
+
import os
|
6 |
+
import subprocess
|
7 |
+
import whisper
|
8 |
+
from whisper.utils import write_vtt
|
9 |
+
import openai
|
10 |
+
|
11 |
+
#from gtts import gTTS
|
12 |
+
from gtts import *
|
13 |
+
from googletrans import Translator
|
14 |
+
|
15 |
+
try:
|
16 |
+
os.mkdir("temp")
|
17 |
+
except:
|
18 |
+
pass
|
19 |
+
|
20 |
+
st.markdown('<h3 style="text-align:center;text-decoration: lightblue underline;font-size:60px;color:red">Nairo24 <span style="color:#4f9bce;font-weight:bolder;font-size:60px;"> News</span></h3>',unsafe_allow_html=True)
|
21 |
+
|
22 |
+
st.title("Text to speech")
|
23 |
+
translator = Translator()
|
24 |
+
|
25 |
+
# Add an image
|
26 |
+
image = "Nairo24.png"
|
27 |
+
st.image(image, caption="", use_column_width=True)
|
28 |
+
|
29 |
+
text = st.text_area("Enter text", value="", height=200)
|
30 |
+
in_lang = st.selectbox(
|
31 |
+
"Select your input language",
|
32 |
+
("English", "Hindi", "Bengali", "korean", "Chinese", "Japanese"),
|
33 |
+
)
|
34 |
+
if in_lang == "English":
|
35 |
+
input_language = "en"
|
36 |
+
elif in_lang == "Hindi":
|
37 |
+
input_language = "hi"
|
38 |
+
elif in_lang == "Bengali":
|
39 |
+
input_language = "bn"
|
40 |
+
elif in_lang == "korean":
|
41 |
+
input_language = "ko"
|
42 |
+
elif in_lang == "Chinese":
|
43 |
+
input_language = "zh-cn"
|
44 |
+
elif in_lang == "Japanese":
|
45 |
+
input_language = "ja"
|
46 |
+
|
47 |
+
out_lang = st.selectbox(
|
48 |
+
"Select your output language",
|
49 |
+
("English", "Hindi", "Bengali", "korean", "Chinese", "Japanese"),
|
50 |
+
)
|
51 |
+
if out_lang == "English":
|
52 |
+
output_language = "en"
|
53 |
+
elif out_lang == "Hindi":
|
54 |
+
output_language = "hi"
|
55 |
+
elif out_lang == "Bengali":
|
56 |
+
output_language = "bn"
|
57 |
+
elif out_lang == "korean":
|
58 |
+
output_language = "ko"
|
59 |
+
elif out_lang == "Chinese":
|
60 |
+
output_language = "zh-cn"
|
61 |
+
elif out_lang == "Japanese":
|
62 |
+
output_language = "ja"
|
63 |
+
|
64 |
+
english_accent = st.selectbox(
|
65 |
+
"Select your english accent",
|
66 |
+
(
|
67 |
+
"Default",
|
68 |
+
"India",
|
69 |
+
"United Kingdom",
|
70 |
+
"United States",
|
71 |
+
"Canada",
|
72 |
+
"Australia",
|
73 |
+
"Ireland",
|
74 |
+
"South Africa",
|
75 |
+
),
|
76 |
+
)
|
77 |
+
|
78 |
+
if english_accent == "Default":
|
79 |
+
tld = "ca"
|
80 |
+
elif english_accent == "India":
|
81 |
+
tld = "co.in"
|
82 |
+
|
83 |
+
elif english_accent == "United Kingdom":
|
84 |
+
tld = "co.uk"
|
85 |
+
elif english_accent == "United States":
|
86 |
+
tld = "com"
|
87 |
+
elif english_accent == "Canada":
|
88 |
+
tld = "ca"
|
89 |
+
elif english_accent == "Australia":
|
90 |
+
tld = "com.au"
|
91 |
+
elif english_accent == "Ireland":
|
92 |
+
tld = "ie"
|
93 |
+
elif english_accent == "South Africa":
|
94 |
+
tld = "co.za"
|
95 |
+
|
96 |
+
|
97 |
+
def text_to_speech(input_language, output_language, text, tld):
|
98 |
+
translation = translator.translate(text, src=input_language, dest=output_language)
|
99 |
+
trans_text = translation.text
|
100 |
+
tts = gTTS(trans_text, lang=output_language, tld=tld, slow=False)
|
101 |
+
try:
|
102 |
+
my_file_name = text[0:20]
|
103 |
+
except:
|
104 |
+
my_file_name = "audio"
|
105 |
+
tts.save(f"temp/{my_file_name}.mp3")
|
106 |
+
return my_file_name, trans_text
|
107 |
+
|
108 |
+
|
109 |
+
display_output_text = st.checkbox("Display output text")
|
110 |
+
|
111 |
+
if st.button("convert"):
|
112 |
+
result, output_text = text_to_speech(input_language, output_language, text, tld)
|
113 |
+
audio_file = open(f"temp/{result}.mp3", "rb")
|
114 |
+
audio_bytes = audio_file.read()
|
115 |
+
st.markdown(f"## Your audio:")
|
116 |
+
st.audio(audio_bytes, format="audio/mp3", start_time=0)
|
117 |
+
|
118 |
+
if display_output_text:
|
119 |
+
st.markdown(f"## Output text:")
|
120 |
+
st.write(f" {output_text}")
|
121 |
+
|
122 |
+
# Add download button for the generated MP3 file
|
123 |
+
st.download_button("Download MP3", data=audio_bytes, file_name=f"{result}.mp3")
|
124 |
+
|
125 |
+
|
126 |
+
def remove_files(n):
|
127 |
+
mp3_files = glob.glob("temp/*mp3")
|
128 |
+
if len(mp3_files) != 0:
|
129 |
+
now = time.time()
|
130 |
+
n_days = n * 86400
|
131 |
+
for f in mp3_files:
|
132 |
+
if os.stat(f).st_mtime < now - n_days:
|
133 |
+
os.remove(f)
|
134 |
+
print("Deleted ", f)
|
135 |
+
|
136 |
+
remove_files(7)
|
137 |
+
|
138 |
+
|
139 |
+
|
140 |
+
###MULTILINGUAL AI. FOR ADDING CAPTIONS TO VIDEOS###
|
141 |
+
|
142 |
+
|
143 |
+
|
144 |
+
from pydub import AudioSegment
|
145 |
+
|
146 |
+
#Download the model
|
147 |
+
model = whisper.load_model("tiny")
|
148 |
+
|
149 |
+
|
150 |
+
def video2mp3(video_file, output_ext="mp3"):
|
151 |
+
filename, ext = os.path.splitext(video_file)
|
152 |
+
audio_file = f"{filename}.{output_ext}"
|
153 |
+
|
154 |
+
video_clip = VideoFileClip(video_file)
|
155 |
+
audio_clip = video_clip.audio
|
156 |
+
audio_clip.write_audiofile(audio_file)
|
157 |
+
|
158 |
+
return audio_file
|
159 |
+
|
160 |
+
|
161 |
+
def translate(input_video):
|
162 |
+
audio_file = video2mp3(input_video)
|
163 |
+
|
164 |
+
options = dict(beam_size=5, best_of=5)
|
165 |
+
translate_options = dict(task="translate", **options)
|
166 |
+
result = model.transcribe(audio_file, **translate_options)
|
167 |
+
|
168 |
+
output_dir = './'
|
169 |
+
audio_path = audio_file.split(".")[0]
|
170 |
+
|
171 |
+
with open(os.path.join(output_dir, audio_path + ".vtt"), "w") as vtt:
|
172 |
+
write_vtt(result["segments"], file=vtt)
|
173 |
+
|
174 |
+
subtitle = audio_path + ".vtt"
|
175 |
+
output_video = audio_path + "_subtitled.mp4"
|
176 |
+
|
177 |
+
st.warning("Subtitle generation is not supported in this environment.")
|
178 |
+
st.warning("Please use a local environment with FFmpeg installed to generate the subtitled video.")
|
179 |
+
|
180 |
+
return output_video
|
181 |
+
|
182 |
+
|
183 |
+
st.title("MultiLingual AI: Add Caption to Videos")
|
184 |
+
|
185 |
+
uploaded_file = st.file_uploader("Upload your video", type=["mp4"])
|
186 |
+
|
187 |
+
if uploaded_file is not None:
|
188 |
+
st.video(uploaded_file)
|
189 |
+
if st.button("Generate Subtitle Video"):
|
190 |
+
# Save uploaded file to a temporary location
|
191 |
+
with open("temp_video.mp4", "wb") as f:
|
192 |
+
f.write(uploaded_file.read())
|
193 |
+
|
194 |
+
output_video = translate("temp_video.mp4")
|
195 |
+
|
196 |
+
# Display the output video
|
197 |
+
st.warning("Subtitle generation is not supported in this environment.")
|
198 |
+
st.warning("Please use a local environment with FFmpeg installed to display the subtitled video.")
|
199 |
+
|
200 |
+
# Remove temporary files
|
201 |
+
os.remove("temp_video.mp4")
|
202 |
+
|
203 |
+
st.markdown(
|
204 |
+
'''
|
205 |
+
<style>
|
206 |
+
.footer {
|
207 |
+
font-size: 12px;
|
208 |
+
color: #888888;
|
209 |
+
text-align: center;
|
210 |
+
}
|
211 |
+
</style>
|
212 |
+
<div class="footer">
|
213 |
+
<p>Powered by <a href="https://openai.com/" style="text-decoration: underline;" target="_blank">OpenAI</a> - Developer Tel: <a style="text-decoration: underline;" target="_blank">+254704205553</a>
|
214 |
+
</p>
|
215 |
+
</div>
|
216 |
+
''',
|
217 |
+
unsafe_allow_html=True
|
218 |
+
)
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit==1.22.0
|
2 |
+
gTTS==2.2.2
|
3 |
+
googletrans==3.1.0a0
|
4 |
+
openai-whisper==20230117
|