nickmuchi commited on
Commit
136e24e
1 Parent(s): a0aab75

Update functions.py

Browse files
Files changed (1) hide show
  1. functions.py +80 -40
functions.py CHANGED
@@ -70,7 +70,7 @@ output_parser = RegexParser(
70
  )
71
 
72
  system_template="""Use only the following pieces of finance context to answer the users question thoroughly.
73
- Do not use any information not provided in the context.
74
  If you don't know the answer, just say that you don't know, don't try to make up an answer.
75
  ALWAYS return a "SOURCES" part in your answer.
76
  The "SOURCES" part should be a reference to the source of the document from which you got your answer.
@@ -126,6 +126,13 @@ def load_asr_model(asr_model_name):
126
 
127
  return asr_model
128
 
 
 
 
 
 
 
 
129
  @st.experimental_singleton(suppress_st_warning=True)
130
  def process_corpus(corpus, title, embedding_model, chunk_size=1000, overlap=50):
131
 
@@ -217,54 +224,87 @@ def get_spacy():
217
  @st.experimental_memo(suppress_st_warning=True)
218
  def inference(link, upload, _asr_model):
219
  '''Convert Youtube video or Audio upload to text'''
 
 
 
 
 
 
 
220
 
221
- if validators.url(link):
 
222
 
223
- yt = YouTube(link)
224
- title = yt.title
225
-
226
- #Get audio file from YT
227
- audio_file = yt.streams.filter(only_audio=True)[0].download(filename="audio.mp4")
228
-
229
- #Get size of audio file
230
- audio_size = round(os.path.getsize(path)/(1024*1024),1)
231
-
232
- #Check if file is > 24mb, if not then use Whisper API
233
- if audio_size <= 24:
234
-
235
- #Use whisper API
236
- transcript = openai.Audio.translate("whisper-1", audio_file)
237
-
238
- else:
 
 
 
 
 
 
 
 
 
 
 
239
 
240
- st.write('File size larger than 24mb, applying chunking and transcription')
241
 
242
- # load the audio file
243
- audio_file = AudioSegment.from_file(path, format="mp4")
244
-
245
- # set chunk size to 24mb (in bytes)
246
- chunk_size = 24 * 1024 * 1024
247
 
248
- # create a directory to store the output files
249
- if not os.path.exists("audio_output"):
250
- os.mkdir("audio_output")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
251
 
252
- audio_chunks = []
253
-
254
- # iterate over each chunk and export it as a separate file
255
- for i, chunk in enumerate(audio_file[::chunk_size]):
256
- chunk.export(f"output/chunk_{i}.mp4", format="mp4")
257
- audio_chunks.append(f"output/chunk_{i}.mp4")
258
 
 
259
 
260
- results = _asr_model.transcribe(path, task='transcribe', language='en')
261
-
262
- return results['text'], yt.title
263
-
264
- elif upload:
265
- results = _asr_model.trasncribe(upload, task='transcribe', language='en')
266
 
267
- return results['text'], "Transcribed Earnings Audio"
 
268
 
269
  @st.experimental_memo(suppress_st_warning=True)
270
  def sentiment_pipe(earnings_text):
 
70
  )
71
 
72
  system_template="""Use only the following pieces of finance context to answer the users question thoroughly.
73
+ Do not use any information not provided in the context and remember you are a finance expert.
74
  If you don't know the answer, just say that you don't know, don't try to make up an answer.
75
  ALWAYS return a "SOURCES" part in your answer.
76
  The "SOURCES" part should be a reference to the source of the document from which you got your answer.
 
126
 
127
  return asr_model
128
 
129
+ @st.experimental_singleton(suppress_st_warning=True)
130
+ def load_whisper_api(audio):
131
+ file = open(audio, "rb")
132
+ transcript = openai.Audio.translate("whisper-1", file)
133
+
134
+ return transcript
135
+
136
  @st.experimental_singleton(suppress_st_warning=True)
137
  def process_corpus(corpus, title, embedding_model, chunk_size=1000, overlap=50):
138
 
 
224
  @st.experimental_memo(suppress_st_warning=True)
225
  def inference(link, upload, _asr_model):
226
  '''Convert Youtube video or Audio upload to text'''
227
+
228
+ try:
229
+
230
+ if validators.url(link):
231
+
232
+ yt = YouTube(link)
233
+ title = yt.title
234
 
235
+ #Get audio file from YT
236
+ audio_file = yt.streams.filter(only_audio=True)[0].download(filename="audio.mp4")
237
 
238
+ #Get size of audio file
239
+ audio_size = round(os.path.getsize(path)/(1024*1024),1)
240
+
241
+ #Check if file is > 24mb, if not then use Whisper API
242
+ if audio_size <= 25:
243
+
244
+ #Use whisper API
245
+ results = load_whisper_api(audio_file)['text']
246
+
247
+ else:
248
+
249
+ st.write('File size larger than 24mb, applying chunking and transcription')
250
+
251
+ song = AudioSegment.from_file("audio.mp4", format='mp4')
252
+
253
+ # PyDub handles time in milliseconds
254
+ twenty_minutes = 20 * 60 * 1000
255
+
256
+ chunks = song[::twenty_minutes]
257
+
258
+ transcriptions = []
259
+
260
+ for i, chunk in enumerate(chunks):
261
+ chunk.export(f'output/chunk_{i}.mp4', format='mp4')
262
+ transcriptions.append(load_whisper_api('output/chunk_{i}.mp4')['text'])
263
+
264
+ results = ','.join(transcriptions)
265
 
266
+ return results, yt.title
267
 
268
+ elif upload:
 
 
 
 
269
 
270
+ #Get size of audio file
271
+ audio_size = round(os.path.getsize(path)/(1024*1024),1)
272
+
273
+ #Check if file is > 24mb, if not then use Whisper API
274
+ if audio_size <= 25:
275
+
276
+ #Use whisper API
277
+ results = load_whisper_api(audio_file)['text']
278
+
279
+ else:
280
+
281
+ st.write('File size larger than 24mb, applying chunking and transcription')
282
+
283
+ song = AudioSegment.from_file("audio.mp4", format='mp4')
284
+
285
+ # PyDub handles time in milliseconds
286
+ twenty_minutes = 20 * 60 * 1000
287
+
288
+ chunks = song[::twenty_minutes]
289
+
290
+ transcriptions = []
291
+
292
+ for i, chunk in enumerate(chunks):
293
+ chunk.export(f'output/chunk_{i}.mp4', format='mp4')
294
+ transcriptions.append(load_whisper_api('output/chunk_{i}.mp4')['text'])
295
+
296
+ results = ','.join(transcriptions)
297
 
298
+ return results, "Transcribed Earnings Audio"
 
 
 
 
 
299
 
300
+ except:
301
 
302
+ st.write('Whisper API Error, using Whisper module from GitHub, might take longer than expected')
303
+
304
+ results = _asr_model.transcribe(path, task='transcribe', language='en')
 
 
 
305
 
306
+ return results['text'], yt.title
307
+
308
 
309
  @st.experimental_memo(suppress_st_warning=True)
310
  def sentiment_pipe(earnings_text):