Update functions.py
Browse files- functions.py +97 -86
functions.py
CHANGED
@@ -145,9 +145,10 @@ def get_yt_audio(url):
|
|
145 |
'''Get YT video from given URL link'''
|
146 |
yt = YouTube(url)
|
147 |
|
|
|
|
|
148 |
# Get the first available audio stream and download it
|
149 |
audio_stream = yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first().download()
|
150 |
-
title = audio_stream.split('\\')[-1].split('.')[0]
|
151 |
|
152 |
return audio_stream, title
|
153 |
|
@@ -160,6 +161,101 @@ def load_whisper_api(audio):
|
|
160 |
|
161 |
return transcript
|
162 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
@st.cache_data
|
164 |
def process_corpus(corpus, title, embedding_model, chunk_size=1000, overlap=50):
|
165 |
|
@@ -299,91 +395,6 @@ def get_spacy():
|
|
299 |
nlp = en_core_web_lg.load()
|
300 |
return nlp
|
301 |
|
302 |
-
|
303 |
-
def inference(link, upload, _asr_model):
|
304 |
-
'''Convert Youtube video or Audio upload to text'''
|
305 |
-
|
306 |
-
try:
|
307 |
-
|
308 |
-
if validators.url(link):
|
309 |
-
|
310 |
-
audio_file, title = get_yt_audio(link)
|
311 |
-
# title = yt.title
|
312 |
-
|
313 |
-
if 'audio' not in st.session_state:
|
314 |
-
st.session_state['audio'] = audio_file
|
315 |
-
|
316 |
-
#Get size of audio file
|
317 |
-
audio_size = round(os.path.getsize(audio_file)/(1024*1024),1)
|
318 |
-
|
319 |
-
#Check if file is > 24mb, if not then use Whisper API
|
320 |
-
if audio_size <= 25:
|
321 |
-
|
322 |
-
#Use whisper API
|
323 |
-
results = load_whisper_api(audio_file)['text']
|
324 |
-
|
325 |
-
else:
|
326 |
-
|
327 |
-
st.warning('File size larger than 24mb, applying chunking and transcription',icon="⚠️")
|
328 |
-
|
329 |
-
song = AudioSegment.from_file(audio_file, format='mp3')
|
330 |
-
|
331 |
-
# PyDub handles time in milliseconds
|
332 |
-
twenty_minutes = 20 * 60 * 1000
|
333 |
-
|
334 |
-
chunks = song[::twenty_minutes]
|
335 |
-
|
336 |
-
transcriptions = []
|
337 |
-
|
338 |
-
for i, chunk in enumerate(chunks):
|
339 |
-
chunk.export(f'output/chunk_{i}.mp3', format='mp3')
|
340 |
-
transcriptions.append(load_whisper_api(f'output/chunk_{i}.mp3')['text'])
|
341 |
-
|
342 |
-
results = ','.join(transcriptions)
|
343 |
-
|
344 |
-
return results, title
|
345 |
-
|
346 |
-
elif _upload:
|
347 |
-
|
348 |
-
#Get size of audio file
|
349 |
-
audio_size = round(os.path.getsize(_upload)/(1024*1024),1)
|
350 |
-
|
351 |
-
#Check if file is > 24mb, if not then use Whisper API
|
352 |
-
if audio_size <= 25:
|
353 |
-
|
354 |
-
#Use whisper API
|
355 |
-
results = load_whisper_api(_upload)['text']
|
356 |
-
|
357 |
-
else:
|
358 |
-
|
359 |
-
st.write('File size larger than 24mb, applying chunking and transcription')
|
360 |
-
|
361 |
-
song = AudioSegment.from_file(_upload)
|
362 |
-
|
363 |
-
# PyDub handles time in milliseconds
|
364 |
-
twenty_minutes = 20 * 60 * 1000
|
365 |
-
|
366 |
-
chunks = song[::twenty_minutes]
|
367 |
-
|
368 |
-
transcriptions = []
|
369 |
-
|
370 |
-
for i, chunk in enumerate(chunks):
|
371 |
-
chunk.export(f'output/chunk_{i}.mp3', format='mp3')
|
372 |
-
transcriptions.append(load_whisper_api('output/chunk_{i}.mp3')['text'])
|
373 |
-
|
374 |
-
results = ','.join(transcriptions)
|
375 |
-
|
376 |
-
return results, "Transcribed Earnings Audio"
|
377 |
-
|
378 |
-
except Exception as e:
|
379 |
-
|
380 |
-
st.error(f'''Whisper API Error: {e},
|
381 |
-
Using Whisper module from GitHub, might take longer than expected''',icon="🚨")
|
382 |
-
|
383 |
-
results = _asr_model.transcribe(st.session_state['audio'], task='transcribe', language='en')
|
384 |
-
|
385 |
-
return results['text'], title
|
386 |
-
|
387 |
|
388 |
@st.cache_data
|
389 |
def sentiment_pipe(earnings_text):
|
|
|
145 |
'''Get YT video from given URL link'''
|
146 |
yt = YouTube(url)
|
147 |
|
148 |
+
title = yt.title
|
149 |
+
|
150 |
# Get the first available audio stream and download it
|
151 |
audio_stream = yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first().download()
|
|
|
152 |
|
153 |
return audio_stream, title
|
154 |
|
|
|
161 |
|
162 |
return transcript
|
163 |
|
164 |
+
def inference(link, upload, _asr_model):
|
165 |
+
'''Convert Youtube video or Audio upload to text'''
|
166 |
+
|
167 |
+
try:
|
168 |
+
|
169 |
+
if validators.url(link):
|
170 |
+
|
171 |
+
st.info("`Downloading YT audio...`")
|
172 |
+
|
173 |
+
audio_file, title = get_yt_audio(link)
|
174 |
+
|
175 |
+
if 'audio' not in st.session_state:
|
176 |
+
st.session_state['audio'] = audio_file
|
177 |
+
|
178 |
+
#Get size of audio file
|
179 |
+
audio_size = round(os.path.getsize(audio_file)/(1024*1024),1)
|
180 |
+
|
181 |
+
#Check if file is > 24mb, if not then use Whisper API
|
182 |
+
if audio_size <= 25:
|
183 |
+
|
184 |
+
st.info("`Transcribing YT audio...`")
|
185 |
+
|
186 |
+
#Use whisper API
|
187 |
+
results = load_whisper_api(audio_file)['text']
|
188 |
+
|
189 |
+
else:
|
190 |
+
|
191 |
+
st.warning('File size larger than 24mb, applying chunking and transcription',icon="⚠️")
|
192 |
+
|
193 |
+
song = AudioSegment.from_file(audio_file, format='mp4')
|
194 |
+
|
195 |
+
# PyDub handles time in milliseconds
|
196 |
+
twenty_minutes = 20 * 60 * 1000
|
197 |
+
|
198 |
+
chunks = song[::twenty_minutes]
|
199 |
+
|
200 |
+
transcriptions = []
|
201 |
+
|
202 |
+
for i, chunk in enumerate(chunks):
|
203 |
+
chunk.export(f'output/chunk_{i}.mp4', format='mp4')
|
204 |
+
transcriptions.append(load_whisper_api(f'output/chunk_{i}.mp4')['text'])
|
205 |
+
|
206 |
+
results = ','.join(transcriptions)
|
207 |
+
|
208 |
+
st.info("`YT Video transcription process complete...`")
|
209 |
+
|
210 |
+
return results, title
|
211 |
+
|
212 |
+
elif _upload:
|
213 |
+
|
214 |
+
#Get size of audio file
|
215 |
+
audio_size = round(os.path.getsize(_upload)/(1024*1024),1)
|
216 |
+
|
217 |
+
#Check if file is > 24mb, if not then use Whisper API
|
218 |
+
if audio_size <= 25:
|
219 |
+
|
220 |
+
st.info("`Transcribing uploaded audio...`")
|
221 |
+
|
222 |
+
#Use whisper API
|
223 |
+
results = load_whisper_api(_upload)['text']
|
224 |
+
|
225 |
+
else:
|
226 |
+
|
227 |
+
st.write('File size larger than 24mb, applying chunking and transcription')
|
228 |
+
|
229 |
+
song = AudioSegment.from_file(_upload)
|
230 |
+
|
231 |
+
# PyDub handles time in milliseconds
|
232 |
+
twenty_minutes = 20 * 60 * 1000
|
233 |
+
|
234 |
+
chunks = song[::twenty_minutes]
|
235 |
+
|
236 |
+
transcriptions = []
|
237 |
+
|
238 |
+
st.info("`Transcribing uploaded audio...`")
|
239 |
+
|
240 |
+
for i, chunk in enumerate(chunks):
|
241 |
+
chunk.export(f'output/chunk_{i}.mp3', format='mp3')
|
242 |
+
transcriptions.append(load_whisper_api('output/chunk_{i}.mp3')['text'])
|
243 |
+
|
244 |
+
results = ','.join(transcriptions)
|
245 |
+
|
246 |
+
st.info("`Uploaded audio transcription process complete...`")
|
247 |
+
|
248 |
+
return results, "Transcribed Earnings Audio"
|
249 |
+
|
250 |
+
except Exception as e:
|
251 |
+
|
252 |
+
st.error(f'''Whisper API Error: {e},
|
253 |
+
Using Whisper module from GitHub, might take longer than expected''',icon="🚨")
|
254 |
+
|
255 |
+
results = _asr_model.transcribe(st.session_state['audio'], task='transcribe', language='en')
|
256 |
+
|
257 |
+
return results['text'], title
|
258 |
+
|
259 |
@st.cache_data
|
260 |
def process_corpus(corpus, title, embedding_model, chunk_size=1000, overlap=50):
|
261 |
|
|
|
395 |
nlp = en_core_web_lg.load()
|
396 |
return nlp
|
397 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
398 |
|
399 |
@st.cache_data
|
400 |
def sentiment_pipe(earnings_text):
|