Amirizaniani commited on
Commit
44f211e
1 Parent(s): cb55839

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -19
app.py CHANGED
@@ -14,6 +14,8 @@ from email.mime.text import MIMEText
14
  from email.mime.base import MIMEBase
15
  from email import encoders
16
  import os
 
 
17
 
18
  nltk.download('punkt')
19
  from nltk.tokenize import word_tokenize
@@ -144,6 +146,7 @@ def answer_question(prompt):
144
  generated_answer = hub_chain.run(input_data)
145
  return generated_answer
146
 
 
147
 
148
  def process_inputs(llm, file, relevance, diversity, email):
149
  # Check if file is uploaded
@@ -167,41 +170,51 @@ def process_inputs(llm, file, relevance, diversity, email):
167
  csv_file = "questions.csv"
168
  df.to_csv(csv_file, index=False)
169
 
170
- # Check network connectivity to the custom port
171
  try:
172
- socket.create_connection(("your.relay.server", 587), timeout=10) # Replace with your relay server and port
173
  except OSError:
174
  return "Network is unreachable. Unable to send email."
175
 
176
- # Email the CSV file
177
  sender_email = "[email protected]"
178
- sender_password = "opri fcxx crkh bvfj"
179
  receiver_email = email
180
  subject = "Your Submitted Questions"
181
  body = "Thank you for your submission. Please find attached the CSV file containing your questions."
182
 
183
- message = MIMEMultipart()
184
- message['From'] = sender_email
185
- message['To'] = receiver_email
186
- message['Subject'] = subject
187
- message.attach(MIMEText(body, 'plain'))
 
188
 
189
- with open(csv_file, "rb") as attachment:
190
- part = MIMEBase('application', 'octet-stream')
191
- part.set_payload(attachment.read())
192
- encoders.encode_base64(part)
193
- part.add_header('Content-Disposition', f"attachment; filename= questions.csv")
194
- message.attach(part)
 
 
 
 
 
 
195
 
196
  try:
197
- with smtplib.SMTP('your.relay.server', 587) as server: # Use your relay server and custom port
198
- server.starttls() # Upgrade the connection to a secure encrypted SSL/TLS connection
199
- server.login(sender_email, sender_password)
200
- server.sendmail(sender_email, receiver_email, message.as_string())
 
201
  except Exception as e:
 
202
  return f"Failed to send email: {e}"
203
 
204
  return "Submitted"
 
 
205
 
206
  text_list = []
207
 
 
14
  from email.mime.base import MIMEBase
15
  from email import encoders
16
  import os
17
+ from sendgrid import SendGridAPIClient
18
+ from sendgrid.helpers.mail import Mail, Attachment, FileContent, FileName, FileType, Disposition
19
 
20
  nltk.download('punkt')
21
  from nltk.tokenize import word_tokenize
 
146
  generated_answer = hub_chain.run(input_data)
147
  return generated_answer
148
 
149
+
150
 
151
  def process_inputs(llm, file, relevance, diversity, email):
152
  # Check if file is uploaded
 
170
  csv_file = "questions.csv"
171
  df.to_csv(csv_file, index=False)
172
 
173
+ # Check network connectivity to SendGrid
174
  try:
175
+ socket.create_connection(("smtp.sendgrid.net", 587), timeout=10)
176
  except OSError:
177
  return "Network is unreachable. Unable to send email."
178
 
179
+ # Email the CSV file using SendGrid
180
  sender_email = "[email protected]"
 
181
  receiver_email = email
182
  subject = "Your Submitted Questions"
183
  body = "Thank you for your submission. Please find attached the CSV file containing your questions."
184
 
185
+ message = Mail(
186
+ from_email=sender_email,
187
+ to_emails=receiver_email,
188
+ subject=subject,
189
+ html_content=body
190
+ )
191
 
192
+ with open(csv_file, "rb") as f:
193
+ data = f.read()
194
+ encoded_file = base64.b64encode(data).decode()
195
+
196
+ attached_file = Attachment(
197
+ FileContent(encoded_file),
198
+ FileName('questions.csv'),
199
+ FileType('text/csv'),
200
+ Disposition('attachment')
201
+ )
202
+
203
+ message.attachment = attached_file
204
 
205
  try:
206
+ sg = SendGridAPIClient(os.environ.get('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'))
207
+ response = sg.send(message)
208
+ print(response.status_code)
209
+ print(response.body)
210
+ print(response.headers)
211
  except Exception as e:
212
+ print(e.message)
213
  return f"Failed to send email: {e}"
214
 
215
  return "Submitted"
216
+
217
+
218
 
219
  text_list = []
220