davila7 commited on
Commit
abd0b35
1 Parent(s): 0dbdead
Files changed (3) hide show
  1. app.py +71 -75
  2. audio.mp3 +0 -0
  3. requirements.txt +2 -2
app.py CHANGED
@@ -1,11 +1,16 @@
1
  import streamlit as st
2
- from bokeh.models.widgets import Button
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
- result = False
44
- if(user_secret):
45
- stt_button = Button(label="Start talking", button_type="success")
46
- stt_button.js_on_event("button_click", CustomJS(code="""
47
- var recognition = new webkitSpeechRecognition();
48
- recognition.continuous = true;
49
- recognition.interimResults = true;
50
-
51
- recognition.onresult = function (e) {
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
- result = streamlit_bokeh_events(
66
- stt_button,
67
- events="GET_TEXT",
68
- key="listen",
69
- refresh_on_update=False,
70
- override_height=75,
71
- debounce_time=0)
72
 
73
- if result:
74
- if "GET_TEXT" in result:
75
- user_input = result.get("GET_TEXT")
76
- st.write('Audio Input: ', user_input)
77
-
78
- openai.api_key = user_secret
79
- response = openai.ChatCompletion.create(
80
- model="gpt-3.5-turbo-0613",
81
- messages=[
82
- {"role": "user", "content": user_input}],
83
- functions=[
84
- {
85
- "name": "send_email",
86
- "description": "Sends an email to a person",
87
- "parameters": {
88
- "type": "object",
89
- "properties": {
90
- "email": {
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
- function_call="auto",
101
- )
102
- message = response["choices"][0]["message"]
103
 
104
 
105
-
106
- st.write('GPT: ', message)
107
 
108
- if message.get("function_call"):
109
- function_name = message["function_call"]["name"]
110
- print('function_name: ', function_name)
111
 
112
- if(function_name == 'send_email'):
113
- # Access the arguments
114
- arguments = json.loads(message['function_call']['arguments'])
115
- email_arg = arguments['email']
116
- body_arg = arguments['body']
117
- subject_arg = arguments['subject']
118
 
119
- # Step 3, call the function
120
- function_response = send_email(
121
- email_arg, subject_arg, body_arg
122
- )
 
 
 
 
 
123
 
124
- print(function_response)
 
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-bokeh-events
 
 
2
  openai
3
  streamlit
4
  streamlit_chat
 
5
  sendgrid
6
+ streamlit-audiorecorder
7
+ git+https://github.com/openai/whisper.git