awacke1's picture
Update app.py
293330f
raw
history blame
2.4 kB
import requests
import pytz
import streamlit as st
import os
from datetime import datetime
from audio_recorder_streamlit import audio_recorder
def generate_filename(prompt, file_type):
central = pytz.timezone('US/Central')
safe_date_time = datetime.now(central).strftime("%m%d_%H%M")
replaced_prompt = prompt.replace(" ", "_").replace("\n", "_")
safe_prompt = "".join(x for x in replaced_prompt if x.isalnum() or x == "_")[:90]
return f"{safe_date_time}_{safe_prompt}.{file_type}"
# 10. Audio recorder to Wav file:
def save_and_play_audio(audio_recorder):
audio_bytes = audio_recorder()
if audio_bytes:
filename = generate_filename("Recording", "wav")
with open(filename, 'wb') as f:
f.write(audio_bytes)
st.audio(audio_bytes, format="audio/wav")
return filename
# 9B. Speech transcription to file output - OPENAI Whisper
def transcribe_audio(file_path):
key=os.getenv('IE_KEY')
headers = {
"Authorization": f"Bearer {key}",
"Content-Type": "audio/wav"
}
with open(file_path, 'rb') as f:
data = {'file': f}
API_URL = "https://tonpixzfvq3791u9.us-east-1.aws.endpoints.huggingface.cloud"
response = requests.post(API_URL, headers=headers, data=data)
st.write(response)
if response.status_code == 200:
st.write('Reasoning with your transcription..')
try:
transcript=response.json().get('text')
except:
transcript=response
st.write(transcript)
#gptResponse = chat_with_model(transcript, systemPrompt=user_prompt_System, model=MODELCHOICE) # send transcript to ChatGPT - prompts, systemPrompt=SYSTEM_PROMPT, model="Gpt-4-32k"
#filename = generate_filename(transcript, choice)
#create_file(filename, transcript, gptResponse) # write output file
#return gptResponse
else:
#st.write(response.json())
#st.error("Error in API call.")
return None
def main():
st.title("Speech to Text")
st.write("Record your speech and get the text.")
# Audio, transcribe, GPT:
filename = save_and_play_audio(audio_recorder)
if filename is not None:
transcription = transcribe_audio(filename)
#st.sidebar.markdown(get_table_download_link(filename), unsafe_allow_html=True)
filename = None
if __name__ == "__main__":
main()