Spaces:
Runtime error
Runtime error
import streamlit as st | |
import pandas as pd | |
from sendgrid import SendGridAPIClient | |
from sendgrid.helpers.mail import Mail | |
import openai | |
import json | |
import whisper | |
from audiorecorder import audiorecorder | |
# whisper | |
model = whisper.load_model('base') | |
data_transcription = [] | |
def send_email(email, subject, body): | |
"""send the user an email with the answer""" | |
try: | |
if(subject == ''): | |
subject = 'GPT Email' | |
message = Mail( | |
# add the email connected to your sendgrid code here | |
from_email='daniel@judini.ai', | |
to_emails=email, | |
subject=subject, | |
html_content=body | |
) | |
st.write(message) | |
sg = SendGridAPIClient(st.secrets["SENDGRID_API_KEY"]) | |
response = sg.send(message) | |
st.write(response.status_code) | |
st.write(response.body) | |
st.write(response.headers) | |
except Exception as e: | |
st.write(f"An error occurred: {str(e)}") | |
st.title('GPT Sends Emails') | |
st.write('Instructions:') | |
st.write('Paste your OpenAI API Key') | |
st.write("Click on the 'Start recording' 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. Then click the same button to stop recording.") | |
st.write("English example: Send an email to dan.avila7@gmail.com reminding him that he must study the OpenAI Functions API for tomorrow's exam") | |
st.write("Ejemplo en Español: Envía un email a dan.avila7@gmail.com recordando que debe estudiar la API de funciones de OpenAI para el examen de mañana") | |
result = False | |
user_secret = '' | |
user_input = False | |
message = False | |
audio = 0 | |
button_run = False | |
user_secret = st.text_input(label = ":blue[OpenAI API key]", | |
value="", | |
placeholder = "Paste your openAI API key, sk-", | |
type = "password") | |
if user_secret: | |
audio = audiorecorder("Start recording", "Click to stop") | |
if len(audio) > 0: | |
# To play audio in frontend: | |
st.audio(audio.tobytes()) | |
# To save audio to a file: | |
wav_file = open("audio.mp3", "wb") | |
wav_file.write(audio.tobytes()) | |
# Whisper | |
output = model.transcribe("audio.mp3") | |
with st.spinner('Wait for it...'): | |
user_audio = output['text'] | |
user_input = st.text_area("Please EDIT your text BEFORE you SEND)", user_audio) | |
button_run = st.button("Run") | |
if(button_run): | |
openai.api_key = user_secret | |
response = openai.ChatCompletion.create( | |
model="gpt-3.5-turbo-0613", | |
messages=[ | |
{"role": "user", "content": user_input}], | |
functions=[ | |
{ | |
"name": "send_email", | |
"description": "Sends an email to a person", | |
"parameters": { | |
"type": "object", | |
"properties": { | |
"email": { | |
"type": "string", | |
"description": "A person to send the email", | |
}, | |
"body": {"type": "string"}, | |
"subject": {"type": "string"}, | |
}, | |
}, | |
} | |
], | |
function_call="auto", | |
) | |
message = response["choices"][0]["message"] | |
st.write('GPT: ', message) | |
if message.get("function_call"): | |
function_name = message["function_call"]["name"] | |
st.write('function_name: ', function_name) | |
if(function_name == 'send_email'): | |
# Access the arguments | |
arguments = json.loads(message['function_call']['arguments']) | |
email_arg = arguments['email'] | |
body_arg = arguments['body'] | |
if(arguments['subject']): | |
arguments['subject'] = 'GPT Email' | |
subject_arg = arguments['subject'] | |
# Step 3, call the function | |
function_response = send_email( | |
email_arg, subject_arg, body_arg | |
) | |
st.write('Function Send Email:') | |
st.write(function_response) | |
else: | |
st.warning('Paste your OpenAI API Key to continue') |