Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
@@ -1,65 +0,0 @@
|
|
1 |
-
#
|
2 |
-
import streamlit as st
|
3 |
-
import openai
|
4 |
-
from text_speech_utils import * # Import the new functions from text_speech_utils.py
|
5 |
-
|
6 |
-
# Define filenames for audio and conversation output
|
7 |
-
input_audio_filename = 'input.wav'
|
8 |
-
output_audio_filename = 'chatgpt_response.wav'
|
9 |
-
output_conversation_filename = 'ChatGPT_conversation.txt'
|
10 |
-
|
11 |
-
# Initialize app
|
12 |
-
if 'messages' not in st.session_state:
|
13 |
-
st.session_state['messages'] = [{"role": "system", "content": "You are a helpful assistant."}]
|
14 |
-
|
15 |
-
# Allow user to input OpenAI API Key via Streamlit text input
|
16 |
-
openai.api_key = st.text_input("Enter your OpenAI API Key", type="password")
|
17 |
-
|
18 |
-
# Display a warning if API key is not provided
|
19 |
-
if not openai.api_key:
|
20 |
-
st.warning("Please enter your OpenAI API key to proceed.")
|
21 |
-
|
22 |
-
# UI components
|
23 |
-
st.title("My awesome personal assistant")
|
24 |
-
sec = st.slider("Select number of seconds of recording", min_value=2, max_value=8, value=5)
|
25 |
-
|
26 |
-
# Record audio + transcribe with Whisper + get GPT-3 response
|
27 |
-
if st.button('Record audio'):
|
28 |
-
if openai.api_key: # Proceed only if API key is provided
|
29 |
-
st.write("Recording...")
|
30 |
-
record_audio(input_audio_filename, sec)
|
31 |
-
|
32 |
-
transcription = transcribe_audio(input_audio_filename)
|
33 |
-
st.write(f"Me: {transcription['text']}")
|
34 |
-
st.session_state['messages'].append({"role": "user", "content": transcription['text']})
|
35 |
-
|
36 |
-
bot = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=st.session_state['messages'])
|
37 |
-
response = bot.choices[0].message.content
|
38 |
-
st.write(f"GPT: {response}")
|
39 |
-
|
40 |
-
save_text_as_audio(response, output_audio_filename)
|
41 |
-
play_audio(output_audio_filename)
|
42 |
-
|
43 |
-
st.session_state['messages'].append({"role": "assistant", "content": response})
|
44 |
-
else:
|
45 |
-
st.error("API key is required to interact with GPT.")
|
46 |
-
|
47 |
-
# Function to generate conversation as plain text
|
48 |
-
def generate_conversation_text(messages):
|
49 |
-
conversation_text = ""
|
50 |
-
for message in messages:
|
51 |
-
if message["role"] == "user":
|
52 |
-
conversation_text += f"Me: {message['content']}\n"
|
53 |
-
elif message["role"] == "assistant":
|
54 |
-
conversation_text += f"GPT: {message['content']}\n"
|
55 |
-
elif message["role"] == "system":
|
56 |
-
conversation_text += f"System: {message['content']}\n"
|
57 |
-
return conversation_text
|
58 |
-
|
59 |
-
# Download conversation button
|
60 |
-
st.download_button(
|
61 |
-
label="Download conversation",
|
62 |
-
data=generate_conversation_text(st.session_state['messages']).encode('utf-8'),
|
63 |
-
file_name=output_conversation_filename,
|
64 |
-
mime="text/plain"
|
65 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|