Spaces:
Sleeping
Sleeping
# helpers.py | |
import base64 | |
import streamlit as st | |
import os | |
import openai | |
from dotenv import load_dotenv | |
# Function to accept OpenAI API Key as input from the user | |
def get_api_key(): | |
"""Prompt the user for their OpenAI API Key.""" | |
api_key = st.text_input("Enter your OpenAI API Key", type="password") | |
if api_key: | |
openai.api_key = api_key | |
return api_key | |
else: | |
return None | |
def speech_to_text(audio_data): | |
with open(audio_data, "rb") as audio_file: | |
transcript = openai.Audio.transcriptions.create( | |
model="whisper-1", | |
response_format="text", | |
file=audio_file | |
) | |
return transcript | |
def text_to_speech(input_text): | |
response = openai.Audio.speech.create( | |
model="text-to-speech-1", # Adjust as necessary | |
voice="nova", | |
input=input_text | |
) | |
webm_file_path = "temp_audio_play.mp3" | |
with open(webm_file_path, "wb") as f: | |
response.stream_to_file(webm_file_path) | |
return webm_file_path | |
def autoplay_audio(file_path: str): | |
with open(file_path, "rb") as f: | |
data = f.read() | |
b64 = base64.b64encode(data).decode("utf-8") | |
md = f""" | |
<audio autoplay> | |
<source src="data:audio/mp3;base64,{b64}" type="audio/mp3"> | |
</audio> | |
""" | |
st.markdown(md, unsafe_allow_html=True) | |