Spaces:
Runtime error
Runtime error
whisper
Browse files- app.py +71 -75
- audio.mp3 +0 -0
- requirements.txt +2 -2
app.py
CHANGED
@@ -1,11 +1,16 @@
|
|
1 |
import streamlit as st
|
2 |
-
|
3 |
-
from bokeh.models import CustomJS
|
4 |
-
from streamlit_bokeh_events import streamlit_bokeh_events
|
5 |
from sendgrid import SendGridAPIClient
|
6 |
from sendgrid.helpers.mail import Mail
|
7 |
import openai
|
8 |
import json
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
def send_email(email, subject, body):
|
11 |
"""send the user an email with the answer"""
|
@@ -36,89 +41,80 @@ st.write('Instructions:')
|
|
36 |
st.write('Paste your OpenAI API Key')
|
37 |
st.write("Click on the 'Start Talking' button and allow the browser permission to use the microphone. Say a sentence requesting to send an email with a message. You must say the person's full email address.")
|
38 |
st.write("Example: Send an email to dan.avila7@gmail.com reminding him that he must study the OpenAI Functions API for tomorrow's exam")
|
|
|
|
|
|
|
|
|
39 |
user_secret = st.text_input(label = ":blue[OpenAI API key]",
|
40 |
value="",
|
41 |
placeholder = "Paste your openAI API key, sk-",
|
42 |
type = "password")
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
var value = "";
|
53 |
-
for (var i = e.resultIndex; i < e.results.length; ++i) {
|
54 |
-
if (e.results[i].isFinal) {
|
55 |
-
value += e.results[i][0].transcript;
|
56 |
-
}
|
57 |
-
}
|
58 |
-
if ( value != "") {
|
59 |
-
document.dispatchEvent(new CustomEvent("GET_TEXT", {detail: value}));
|
60 |
-
}
|
61 |
-
}
|
62 |
-
recognition.start();
|
63 |
-
"""))
|
64 |
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
debounce_time=0)
|
72 |
|
73 |
-
if
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
"type": "string",
|
92 |
-
"description": "A person to send the email",
|
93 |
},
|
94 |
-
"body": {"type": "string"},
|
95 |
-
"subject": {"type": "string"},
|
96 |
},
|
97 |
-
}
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
|
104 |
|
105 |
-
|
106 |
-
st.write('GPT: ', message)
|
107 |
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
subject_arg = arguments['subject']
|
118 |
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
|
|
|
|
|
|
|
|
|
|
123 |
|
124 |
-
|
|
|
1 |
import streamlit as st
|
2 |
+
import pandas as pd
|
|
|
|
|
3 |
from sendgrid import SendGridAPIClient
|
4 |
from sendgrid.helpers.mail import Mail
|
5 |
import openai
|
6 |
import json
|
7 |
+
import whisper
|
8 |
+
from audiorecorder import audiorecorder
|
9 |
+
|
10 |
+
|
11 |
+
# whisper
|
12 |
+
model = whisper.load_model('small')
|
13 |
+
data_transcription = []
|
14 |
|
15 |
def send_email(email, subject, body):
|
16 |
"""send the user an email with the answer"""
|
|
|
41 |
st.write('Paste your OpenAI API Key')
|
42 |
st.write("Click on the 'Start Talking' button and allow the browser permission to use the microphone. Say a sentence requesting to send an email with a message. You must say the person's full email address.")
|
43 |
st.write("Example: Send an email to dan.avila7@gmail.com reminding him that he must study the OpenAI Functions API for tomorrow's exam")
|
44 |
+
|
45 |
+
result = False
|
46 |
+
user_secret = ''
|
47 |
+
user_input = False
|
48 |
user_secret = st.text_input(label = ":blue[OpenAI API key]",
|
49 |
value="",
|
50 |
placeholder = "Paste your openAI API key, sk-",
|
51 |
type = "password")
|
52 |
+
if user_secret:
|
53 |
+
audio = audiorecorder("Click to record", "Click to stop")
|
54 |
+
if len(audio) > 0:
|
55 |
+
# To play audio in frontend:
|
56 |
+
st.audio(audio.tobytes())
|
57 |
+
|
58 |
+
# To save audio to a file:
|
59 |
+
wav_file = open("audio.mp3", "wb")
|
60 |
+
wav_file.write(audio.tobytes())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
+
# Whisper
|
63 |
+
with st.spinner('Loading...'):
|
64 |
+
output = model.transcribe("audio.mp3")
|
65 |
+
user_input = output['text']
|
66 |
+
st.write('Audio Input: ', user_input)
|
67 |
+
st.success('Done!')
|
|
|
68 |
|
69 |
+
if user_input and user_input:
|
70 |
+
openai.api_key = user_secret
|
71 |
+
response = openai.ChatCompletion.create(
|
72 |
+
model="gpt-3.5-turbo-0613",
|
73 |
+
messages=[
|
74 |
+
{"role": "user", "content": user_input}],
|
75 |
+
functions=[
|
76 |
+
{
|
77 |
+
"name": "send_email",
|
78 |
+
"description": "Sends an email to a person",
|
79 |
+
"parameters": {
|
80 |
+
"type": "object",
|
81 |
+
"properties": {
|
82 |
+
"email": {
|
83 |
+
"type": "string",
|
84 |
+
"description": "A person to send the email",
|
85 |
+
},
|
86 |
+
"body": {"type": "string"},
|
87 |
+
"subject": {"type": "string"},
|
|
|
88 |
},
|
|
|
|
|
89 |
},
|
90 |
+
}
|
91 |
+
],
|
92 |
+
function_call="auto",
|
93 |
+
)
|
94 |
+
message = response["choices"][0]["message"]
|
95 |
+
|
96 |
|
97 |
|
98 |
+
st.write('GPT: ', message)
|
|
|
99 |
|
100 |
+
if message.get("function_call"):
|
101 |
+
function_name = message["function_call"]["name"]
|
102 |
+
print('function_name: ', function_name)
|
103 |
|
104 |
+
if(function_name == 'send_email'):
|
105 |
+
# Access the arguments
|
106 |
+
arguments = json.loads(message['function_call']['arguments'])
|
107 |
+
email_arg = arguments['email']
|
108 |
+
body_arg = arguments['body']
|
|
|
109 |
|
110 |
+
if(arguments['subject']):
|
111 |
+
arguments['subject'] = 'GPT Email'
|
112 |
+
|
113 |
+
subject_arg = arguments['subject']
|
114 |
+
|
115 |
+
# Step 3, call the function
|
116 |
+
function_response = send_email(
|
117 |
+
email_arg, subject_arg, body_arg
|
118 |
+
)
|
119 |
|
120 |
+
print(function_response)
|
audio.mp3
ADDED
Binary file (128 kB). View file
|
|
requirements.txt
CHANGED
@@ -2,6 +2,6 @@ langchain
|
|
2 |
openai
|
3 |
streamlit
|
4 |
streamlit_chat
|
5 |
-
bokeh
|
6 |
sendgrid
|
7 |
-
streamlit-
|
|
|
|
2 |
openai
|
3 |
streamlit
|
4 |
streamlit_chat
|
|
|
5 |
sendgrid
|
6 |
+
streamlit-audiorecorder
|
7 |
+
git+https://github.com/openai/whisper.git
|