shukdevdatta123 commited on
Commit
ed6dab8
·
verified ·
1 Parent(s): 3c5c809

Create helpers.py

Browse files
Files changed (1) hide show
  1. helpers.py +51 -0
helpers.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # helpers.py
2
+
3
+ import base64
4
+ import streamlit as st
5
+ import os
6
+ import openai
7
+ from openai import OpenAI
8
+ from dotenv import load_dotenv
9
+
10
+ # Function to accept OpenAI API Key as input from the user
11
+ def get_api_key():
12
+ """Prompt the user for their OpenAI API Key."""
13
+ api_key = st.text_input("Enter your OpenAI API Key", type="password")
14
+ if api_key:
15
+ openai.api_key = api_key
16
+ return api_key
17
+ else:
18
+ return None
19
+
20
+ def speech_to_text(audio_data):
21
+ with open(audio_data, "rb") as audio_file:
22
+ transcript = openai.Audio.transcriptions.create(
23
+ model="whisper-1",
24
+ response_format="text",
25
+ file=audio_file
26
+ )
27
+ return transcript
28
+
29
+
30
+ def text_to_speech(input_text):
31
+ response = openai.Audio.speech.create(
32
+ model="text-to-speech-1", # Adjust as necessary
33
+ voice="nova",
34
+ input=input_text
35
+ )
36
+ webm_file_path = "temp_audio_play.mp3"
37
+ with open(webm_file_path, "wb") as f:
38
+ response.stream_to_file(webm_file_path)
39
+ return webm_file_path
40
+
41
+
42
+ def autoplay_audio(file_path: str):
43
+ with open(file_path, "rb") as f:
44
+ data = f.read()
45
+ b64 = base64.b64encode(data).decode("utf-8")
46
+ md = f"""
47
+ <audio autoplay>
48
+ <source src="data:audio/mp3;base64,{b64}" type="audio/mp3">
49
+ </audio>
50
+ """
51
+ st.markdown(md, unsafe_allow_html=True)