Justin Grammens commited on
Commit
8a56a77
·
1 Parent(s): 5827eb5

updated to use sendgrid

Browse files
Files changed (2) hide show
  1. app.py +25 -24
  2. requirements.txt +2 -1
app.py CHANGED
@@ -6,8 +6,15 @@ from langchain.prompts import PromptTemplate
6
  from langchain_openai import ChatOpenAI
7
  from langchain.schema import HumanMessage
8
 
 
 
 
9
  secretcode = os.environ["SECRETCODE"]
10
 
 
 
 
 
11
  # Initialize the OpenAI model
12
  llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=.8)
13
 
@@ -49,32 +56,26 @@ prompt = PromptTemplate(
49
  template=template,
50
  )
51
 
52
- def send_simple_message(essay, email):
53
- print("SENT THIS TEXT", essay, "to this email", email)
54
- print(os.environ["MAILGUN_POST_URL"])
55
-
56
- jj = requests.post(
57
- os.environ["MAILGUN_POST_URL"],
58
- auth=("api", os.environ["MAILGUN_API_KEY"]),
59
- data={"from": os.environ["PLP_RETURN_EMAIL_ADDRESS"],
60
- "to": [email, email],
61
- "subject": "PLP Essay Results",
62
- "text": essay}
63
- )
64
-
65
- print(jj.content)
66
- print(jj.reason)
67
- print(jj.status_code)
68
- print(jj.text)
69
- print(jj.headers)
70
- return jj
71
 
72
  # Add an alert box to notify the user if the email was sent correctly
73
  def show_alert(result):
74
- if result.status_code == 200:
75
- gr.Info("Emails sent")
76
- else:
77
- gr.Info("Failed to send emails")
78
 
79
  # Function to generate the essay using the template and answers
80
  def stream_leadership_essay(*answers):
@@ -113,7 +114,7 @@ def stream_leadership_essay(*answers):
113
  essay += chunk.content
114
  yield essay, filled_prompt # Yield partial essay to the Gradio output in real-time
115
 
116
- result = send_simple_message(essay, answers[28])
117
  print(result)
118
  show_alert(result)
119
 
 
6
  from langchain_openai import ChatOpenAI
7
  from langchain.schema import HumanMessage
8
 
9
+ from sendgrid import SendGridAPIClient
10
+ from sendgrid.helpers.mail import Mail
11
+
12
  secretcode = os.environ["SECRETCODE"]
13
 
14
+
15
+ # Replace with your verified sender email
16
+ from_email = '[email protected]'
17
+
18
  # Initialize the OpenAI model
19
  llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=.8)
20
 
 
56
  template=template,
57
  )
58
 
59
+ def send_sendgrid_email(essay, email):
60
+ message = Mail(
61
+ from_email=from_email,
62
+ to_emails=email,
63
+ subject='PLP Essay Results',
64
+ html_content=essay)
65
+ try:
66
+ sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
67
+ response = sg.send(message)
68
+ print(response.status_code)
69
+ print(response.body)
70
+ print(response.headers)
71
+ return "Email Sent"
72
+ except Exception as e:
73
+ print(e)
74
+ return "Email Failed"
 
 
 
75
 
76
  # Add an alert box to notify the user if the email was sent correctly
77
  def show_alert(result):
78
+ gr.Info(result)
 
 
 
79
 
80
  # Function to generate the essay using the template and answers
81
  def stream_leadership_essay(*answers):
 
114
  essay += chunk.content
115
  yield essay, filled_prompt # Yield partial essay to the Gradio output in real-time
116
 
117
+ result = send_sendgrid_email(essay, answers[28])
118
  print(result)
119
  show_alert(result)
120
 
requirements.txt CHANGED
@@ -1,2 +1,3 @@
1
  langchain
2
- langchain_openai
 
 
1
  langchain
2
+ langchain_openai
3
+ sendgrid