Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import openai
|
4 |
+
from text_speech_utils import * # Assuming this module exists for your audio functionality
|
5 |
+
|
6 |
+
# Initialize app
|
7 |
+
if 'messages' not in st.session_state:
|
8 |
+
st.session_state['messages'] = [{"role": "system", "content": "You are a helpful assistant."}]
|
9 |
+
|
10 |
+
# Allow user to input OpenAI API Key via Streamlit text input
|
11 |
+
openai.api_key = st.text_input("Enter your OpenAI API Key", type="password")
|
12 |
+
|
13 |
+
# Display a warning if API key is not provided
|
14 |
+
if not openai.api_key:
|
15 |
+
st.warning("Please enter your OpenAI API key to proceed.")
|
16 |
+
|
17 |
+
# UI components
|
18 |
+
st.title("My awesome personal assistant")
|
19 |
+
sec = st.slider("Select number of seconds of recording", min_value=2, max_value=8, value=5)
|
20 |
+
|
21 |
+
# Record audio + transcribe with Whisper + get GPT-3 response
|
22 |
+
if st.button('Record audio'):
|
23 |
+
if openai.api_key: # Proceed only if API key is provided
|
24 |
+
st.write("Recording...")
|
25 |
+
record_audio(input_audio_filename, sec)
|
26 |
+
|
27 |
+
transcription = transcribe_audio(input_audio_filename)
|
28 |
+
st.write(f"Me: {transcription['text']}")
|
29 |
+
st.session_state['messages'].append({"role": "user", "content": transcription['text']})
|
30 |
+
|
31 |
+
bot = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=st.session_state['messages'])
|
32 |
+
response = bot.choices[0].message.content
|
33 |
+
st.write(f"GPT: {response}")
|
34 |
+
|
35 |
+
save_text_as_audio(response, output_audio_filename)
|
36 |
+
play_audio(output_audio_filename)
|
37 |
+
|
38 |
+
st.session_state['messages'].append({"role": "assistant", "content": response})
|
39 |
+
else:
|
40 |
+
st.error("API key is required to interact with GPT.")
|
41 |
+
|
42 |
+
# Download conversation button
|
43 |
+
st.download_button(label="Download conversation",
|
44 |
+
data=pd.DataFrame(st.session_state['messages']).to_csv(index=False).encode('utf-8'),
|
45 |
+
file_name=output_conversation_filename)
|