faizanmumtaz commited on
Commit
96258d3
·
1 Parent(s): 91d4ac5

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +123 -0
app.py ADDED
@@ -0,0 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import openai
3
+ import json
4
+ import requests
5
+ import os
6
+ openai.api_key = ""
7
+
8
+ def send_email(email_id,subject,content):
9
+ import smtplib
10
+ from email.mime.text import MIMEText
11
+ from email.mime.multipart import MIMEMultipart
12
+
13
+ # Email configuration
14
+ sender_email = 'Your gmail id'
15
+ receiver_email = email_id
16
+ password = 'Use an app-specific password for security'
17
+ subject = subject
18
+ message_body = content
19
+
20
+ # Create a MIMEText object for the email message
21
+ message = MIMEMultipart()
22
+ message['From'] = sender_email
23
+ message['To'] = receiver_email
24
+ message['Subject'] = subject
25
+ message.attach(MIMEText(message_body, 'plain'))
26
+
27
+ # Connect to the SMTP server (for Gmail, use 'smtp.gmail.com')
28
+ smtp_server = 'smtp.gmail.com'
29
+ smtp_port = 587 # Use 465 for SSL or 587 for TLS
30
+
31
+ try:
32
+ server = smtplib.SMTP(smtp_server, smtp_port)
33
+ server.starttls()
34
+ server.login(sender_email, password)
35
+ server.sendmail(sender_email, receiver_email, message.as_string())
36
+ server.quit()
37
+ return('Email sent successfully')
38
+ except Exception as e:
39
+ return('Error sending email:', str(e))
40
+ # Attach the message body
41
+
42
+ def run_conversation():
43
+ functions = [
44
+ {
45
+ name: send_email,
46
+ description: send an email to given email address.,
47
+ parameters: {
48
+ type: object,
49
+ properties: {
50
+ email_id:{
51
+ type:string,
52
+ description:The id of email.
53
+ },
54
+ subject: {
55
+ type: string,
56
+ description: The subject of the email.,
57
+ },
58
+ content:
59
+ {type: string, description: The content of the email.},
60
+ },
61
+ required: [email_id,subject,content],
62
+ },
63
+ }]
64
+ response = openai.ChatCompletion.create(
65
+ model=gpt-3.5-turbo-0613,
66
+ messages=st.session_state.messages,
67
+ functions=functions,
68
+ function_call=auto,
69
+ )
70
+ response_message = response[choices][0][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
+ # Step 4: send the info on the function call and function response to GPT
86
+ # extend conversation with assistant's reply
87
+ st.session_state.messages.append(response_message)
88
+ st.session_state.messages.append(
89
+ {
90
+ role: function,
91
+ name: function_name,
92
+ content: function_response,
93
+ })
94
+ import time
95
+ time.sleep(5)
96
+ second_response = openai.ChatCompletion.create(
97
+ model=gpt-3.5-turbo-0613,
98
+ messages=st.session_state.messages)
99
+ second_response=second_response[choices][0][message][content]
100
+ st.session_state.messages.append({role: assistant, content: second_response})
101
+ return second_response
102
+ else:
103
+ response_message = response_message[content]
104
+ st.session_state.messages.append({role:assistant,content:response_message})
105
+ return response_message
106
+
107
+
108
+ if messages not in st.session_state.keys():
109
+ st.session_state.messages = [{role: assistant, content: WellCome To AI Email Sender!}]
110
+
111
+ if prompt := st.chat_input(Send Query...): # Prompt for user input and save to chat history
112
+ st.session_state.messages.append({role: user, content: prompt})
113
+
114
+ for message in st.session_state.messages: # Display the prior chat messages
115
+ with st.chat_message(message[role]):
116
+ st.write(message[content])
117
+
118
+
119
+ if st.session_state.messages[-1][role] != assistant:
120
+ with st.chat_message(assistant):
121
+ with st.spinner(Thinking...):
122
+ response_message = run_conversation()
123
+ st.write(response_message)