awacke1 commited on
Commit
c6ef66c
1 Parent(s): 3040d19

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -38
app.py CHANGED
@@ -387,66 +387,52 @@ def get_zip_download_link(zip_file):
387
  href = f'<a href="data:application/zip;base64,{b64}" download="{zip_file}">Download All</a>'
388
  return href
389
 
390
- def whisper(filename):
 
 
 
 
 
 
 
391
  with open(filename, "rb") as f:
392
- data = f.read
393
- API_URL = f'https://tonpixzfvq3791u9.us-east-1.aws.endpoints.huggingface.cloud'
394
- headers = {
395
- "Authorization": "Bearer XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
396
- "Content-Type": "audio/wav"
397
- }
398
  response = requests.post(API_URL, headers=headers, data=data)
399
- st.write(response)
400
  return response.json()
401
 
402
- def whisper_generate_filename(prompt, file_type):
403
  central = pytz.timezone('US/Central')
404
  safe_date_time = datetime.now(central).strftime("%m%d_%H%M")
405
  replaced_prompt = prompt.replace(" ", "_").replace("\n", "_")
406
  safe_prompt = "".join(x for x in replaced_prompt if x.isalnum() or x == "_")[:90]
407
  return f"{safe_date_time}_{safe_prompt}.{file_type}"
408
 
409
- def whisper_save_and_play_audio(audio_recorder):
410
- audio_bytes = audio_recorder(key='whisper_audio_recorder')
 
411
  if audio_bytes:
412
- filename = whisper_generate_filename("Recording", "wav")
413
  with open(filename, 'wb') as f:
414
  f.write(audio_bytes)
415
  st.audio(audio_bytes, format="audio/wav")
416
- st.markdown(f'Written file ' + filename)
417
  return filename
418
 
419
- def whisper_transcribe_audio(filename):
420
- output = whisper(filename)
 
421
  return output
422
 
423
- def whisper_save_transcription(transcription, file_path):
424
- with open(file_path, 'a') as f:
425
- f.write(f"{transcription}\n")
426
-
427
- def whisper_load_previous_transcriptions(file_path):
428
- if os.path.exists(file_path):
429
- with open(file_path, 'r') as f:
430
- return f.read()
431
- return ""
432
 
433
  def whisper_main():
434
- st.title("AI Whisperer Speech to Text 🎤📝")
435
- st.write("Record your speech and get the text. 🗨️")
436
-
437
- file_path = 'text_output.txt'
438
- previous_transcriptions = whisper_load_previous_transcriptions(file_path)
439
- text_area = st.text_area("Transcriptions:", previous_transcriptions, height=400)
440
 
441
- filename = whisper_save_and_play_audio(audio_recorder)
 
442
  if filename is not None:
443
- #try:
444
- transcription = whisper_transcribe_audio(filename)
445
- updated_transcriptions = f"{previous_transcriptions}\n{transcription}"
446
- st.text_area("Transcriptions:", updated_transcriptions, height=400)
447
- whisper_save_transcription(transcription, file_path)
448
- #except:
449
- # st.write('Whisperer loading..')
450
 
451
  def main():
452
 
 
387
  href = f'<a href="data:application/zip;base64,{b64}" download="{zip_file}">Download All</a>'
388
  return href
389
 
390
+
391
+ API_URL = f'https://tonpixzfvq3791u9.us-east-1.aws.endpoints.huggingface.cloud'
392
+ headers = {
393
+ "Authorization": "Bearer XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
394
+ "Content-Type": "audio/wav"
395
+ }
396
+
397
+ def query(filename):
398
  with open(filename, "rb") as f:
399
+ data = f.read()
 
 
 
 
 
400
  response = requests.post(API_URL, headers=headers, data=data)
 
401
  return response.json()
402
 
403
+ def generate_filename(prompt, file_type):
404
  central = pytz.timezone('US/Central')
405
  safe_date_time = datetime.now(central).strftime("%m%d_%H%M")
406
  replaced_prompt = prompt.replace(" ", "_").replace("\n", "_")
407
  safe_prompt = "".join(x for x in replaced_prompt if x.isalnum() or x == "_")[:90]
408
  return f"{safe_date_time}_{safe_prompt}.{file_type}"
409
 
410
+ # 10. Audio recorder to Wav file:
411
+ def save_and_play_audio(audio_recorder):
412
+ audio_bytes = audio_recorder()
413
  if audio_bytes:
414
+ filename = generate_filename("Recording", "wav")
415
  with open(filename, 'wb') as f:
416
  f.write(audio_bytes)
417
  st.audio(audio_bytes, format="audio/wav")
 
418
  return filename
419
 
420
+ # 9B. Speech transcription to file output - OPENAI Whisper
421
+ def transcribe_audio(filename):
422
+ output = query(filename)
423
  return output
424
 
 
 
 
 
 
 
 
 
 
425
 
426
  def whisper_main():
427
+ st.title("Speech to Text")
428
+ st.write("Record your speech and get the text.")
 
 
 
 
429
 
430
+ # Audio, transcribe, GPT:
431
+ filename = save_and_play_audio(audio_recorder)
432
  if filename is not None:
433
+ transcription = transcribe_audio(filename)
434
+ st.write(transcription)
435
+
 
 
 
 
436
 
437
  def main():
438