Spaces:
Sleeping
Sleeping
Commit
·
db1f79d
1
Parent(s):
194e044
Update app.py
Browse files
app.py
CHANGED
@@ -1,124 +0,0 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import openai
|
3 |
-
import json
|
4 |
-
import requests
|
5 |
-
import os
|
6 |
-
|
7 |
-
def send_email(email_id,subject,content):
|
8 |
-
import smtplib
|
9 |
-
from email.mime.text import MIMEText
|
10 |
-
from email.mime.multipart import MIMEMultipart
|
11 |
-
|
12 |
-
# Email configuration
|
13 |
-
sender_email = '[email protected]'
|
14 |
-
receiver_email = email_id
|
15 |
-
password = 'wvurqdfbpbjiqzjq'
|
16 |
-
subject = subject
|
17 |
-
message_body = content
|
18 |
-
|
19 |
-
# Create a MIMEText object for the email message
|
20 |
-
message = MIMEMultipart()
|
21 |
-
message['From'] = sender_email
|
22 |
-
message['To'] = receiver_email
|
23 |
-
message['Subject'] = subject
|
24 |
-
message.attach(MIMEText(message_body, 'plain'))
|
25 |
-
|
26 |
-
# Connect to the SMTP server (for Gmail, use 'smtp.gmail.com')
|
27 |
-
smtp_server = 'smtp.gmail.com'
|
28 |
-
smtp_port = 587 # Use 465 for SSL or 587 for TLS
|
29 |
-
|
30 |
-
try:
|
31 |
-
server = smtplib.SMTP(smtp_server, smtp_port)
|
32 |
-
server.starttls()
|
33 |
-
server.login(sender_email, password)
|
34 |
-
server.sendmail(sender_email, receiver_email, message.as_string())
|
35 |
-
server.quit()
|
36 |
-
return('Email sent successfully')
|
37 |
-
except Exception as e:
|
38 |
-
return('Error sending email:', str(e))
|
39 |
-
# Attach the message body
|
40 |
-
|
41 |
-
def run_conversation():
|
42 |
-
functions = [
|
43 |
-
{
|
44 |
-
"name": "send_email",
|
45 |
-
"description": "send an email to given email address.",
|
46 |
-
"parameters": {
|
47 |
-
"type": "object",
|
48 |
-
"properties": {
|
49 |
-
"email_id":{
|
50 |
-
"type":"string",
|
51 |
-
"description":"The id of email."
|
52 |
-
},
|
53 |
-
"subject": {
|
54 |
-
"type": "string",
|
55 |
-
"description": "The subject of the email.",
|
56 |
-
},
|
57 |
-
"content":
|
58 |
-
{"type": "string", "description": "The content of the email."},
|
59 |
-
},
|
60 |
-
"required": ["email_id","subject","content"],
|
61 |
-
},
|
62 |
-
}]
|
63 |
-
response = openai.ChatCompletion.create(
|
64 |
-
model="gpt-3.5-turbo-1106",
|
65 |
-
messages=st.session_state.messages,
|
66 |
-
functions=functions,
|
67 |
-
function_call="auto",
|
68 |
-
)
|
69 |
-
response_message = response["choices"][0]["message"]
|
70 |
-
print(response_message)
|
71 |
-
# Step 2: check if GPT wanted to call a function
|
72 |
-
if response_message.get("function_call"):
|
73 |
-
# Step 3: call the function
|
74 |
-
# Note: the JSON response may not always be valid; be sure to handle errors
|
75 |
-
available_functions = {
|
76 |
-
"send_email": send_email
|
77 |
-
} # only one function in this example, but you can have multiple
|
78 |
-
function_name = response_message["function_call"]["name"]
|
79 |
-
function_to_call = available_functions[function_name]
|
80 |
-
function_args = json.loads(response_message["function_call"]["arguments"])
|
81 |
-
function_response=function_to_call(
|
82 |
-
email_id=function_args.get("email_id"),
|
83 |
-
subject=function_args.get("subject"),
|
84 |
-
content=function_args.get("content"))
|
85 |
-
st.write(function_response)
|
86 |
-
# Step 4: send the info on the function call and function response to GPT
|
87 |
-
# extend conversation with assistant's reply
|
88 |
-
# st.session_state.messages.append(response_message)
|
89 |
-
st.session_state.messages.append(
|
90 |
-
{
|
91 |
-
"role": "function",
|
92 |
-
"name": function_name,
|
93 |
-
"content": function_response,
|
94 |
-
})
|
95 |
-
import time
|
96 |
-
time.sleep(5)
|
97 |
-
second_response = openai.ChatCompletion.create(
|
98 |
-
model="gpt-3.5-turbo-1106",
|
99 |
-
messages=st.session_state.messages)
|
100 |
-
second_response=second_response["choices"][0]["message"]["content"]
|
101 |
-
st.session_state.messages.append({"role": "assistant", "content": second_response})
|
102 |
-
return second_response
|
103 |
-
else:
|
104 |
-
response_message = response_message["content"]
|
105 |
-
st.session_state.messages.append({"role":"assistant","content":response_message})
|
106 |
-
return response_message
|
107 |
-
|
108 |
-
|
109 |
-
if "messages" not in st.session_state.keys():
|
110 |
-
st.session_state.messages = [{"role": "assistant", "content": "WellCome To AI Email Sender!"}]
|
111 |
-
|
112 |
-
if prompt := st.chat_input("Send Query..."): # Prompt for user input and save to chat history
|
113 |
-
st.session_state.messages.append({"role": "user", "content": prompt})
|
114 |
-
|
115 |
-
for message in st.session_state.messages: # Display the prior chat messages
|
116 |
-
with st.chat_message(message["role"]):
|
117 |
-
st.write(message["content"])
|
118 |
-
|
119 |
-
|
120 |
-
if st.session_state.messages[-1]["role"] != "assistant":
|
121 |
-
with st.chat_message("assistant"):
|
122 |
-
with st.spinner("Thinking..."):
|
123 |
-
response_message = run_conversation()
|
124 |
-
st.write(response_message)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|