File size: 4,484 Bytes
1189815
abd0b35
1189815
 
 
 
abd0b35
 
 
 
 
42c5809
abd0b35
1189815
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42c5809
1189815
 
 
 
42c5809
 
 
abd0b35
 
 
 
42c5809
 
 
 
1189815
42c5809
 
 
abd0b35
42c5809
 
abd0b35
 
 
 
 
 
 
1189815
abd0b35
42c5809
7ceb8a6
42c5809
 
 
1189815
7ceb8a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1189815
7ceb8a6
 
1189815
7ceb8a6
 
 
 
 
 
 
abd0b35
7ceb8a6
 
 
980cc76
7ceb8a6
 
 
 
 
1189815
7ceb8a6
 
1189815
7ceb8a6
1189815
7ceb8a6
 
 
 
 
 
42c5809
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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("Message (You can edit the text if you want)", 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')