Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,86 @@
|
|
1 |
-
import
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import imaplib
|
2 |
+
import email
|
3 |
+
from transformers import BartForConditionalGeneration, BartTokenizer, pipeline
|
4 |
+
import torch
|
5 |
+
import email.header
|
6 |
+
|
7 |
+
model_name = 'facebook/bart-large-cnn'
|
8 |
+
tokenizer = BartTokenizer.from_pretrained(model_name)
|
9 |
+
model = BartForConditionalGeneration.from_pretrained(model_name)
|
10 |
+
|
11 |
+
sentiment_analyzer = pipeline('sentiment-analysis', model='distilbert-base-uncased')
|
12 |
+
|
13 |
+
mail = imaplib.IMAP4_SSL('imap.gmail.com')
|
14 |
+
mail.login('[email protected]', 'fwqw pnmq ulip umjl')
|
15 |
+
mail.select('inbox')
|
16 |
+
|
17 |
+
def generate_summary(email_text, max_length=20):
|
18 |
+
inputs = tokenizer([email_text], return_tensors='pt', max_length=1024, truncation=True)
|
19 |
+
|
20 |
+
with torch.no_grad():
|
21 |
+
summary_ids = model.generate(**inputs, max_length=max_length)
|
22 |
+
|
23 |
+
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
24 |
+
return summary
|
25 |
+
|
26 |
+
from datetime import date
|
27 |
+
today = date.today()
|
28 |
+
today_date = today.strftime("%d-%b-%Y")
|
29 |
+
|
30 |
+
status, email_ids = mail.search(None, 'SINCE', today_date)
|
31 |
+
email_ids = email_ids[0].split()
|
32 |
+
|
33 |
+
for email_id in email_ids:
|
34 |
+
status, msg_data = mail.fetch(email_id, '(RFC822)')
|
35 |
+
raw_email = msg_data[0][1]
|
36 |
+
msg = email.message_from_bytes(raw_email)
|
37 |
+
sender = msg['From']
|
38 |
+
|
39 |
+
subject = email.header.decode_header(msg['Subject'])
|
40 |
+
subject_str = ""
|
41 |
+
for part, encoding in subject:
|
42 |
+
if isinstance(part, bytes):
|
43 |
+
if encoding:
|
44 |
+
subject_str += part.decode(encoding)
|
45 |
+
else:
|
46 |
+
subject_str += part.decode('utf-8')
|
47 |
+
else:
|
48 |
+
subject_str += part
|
49 |
+
|
50 |
+
body = ""
|
51 |
+
|
52 |
+
if msg.is_multipart():
|
53 |
+
for part in msg.walk():
|
54 |
+
if part.get_content_type() == "text/plain":
|
55 |
+
body = part.get_payload(decode=True).decode()
|
56 |
+
break
|
57 |
+
else:
|
58 |
+
body = msg.get_payload(decode=True).decode()
|
59 |
+
|
60 |
+
if body:
|
61 |
+
word_count = len(body.split())
|
62 |
+
if word_count < 10:
|
63 |
+
summary = body
|
64 |
+
else:
|
65 |
+
if word_count < 50:
|
66 |
+
summary = generate_summary(body, max_length=20)
|
67 |
+
else:
|
68 |
+
summary = generate_summary(body, max_length=50)
|
69 |
+
|
70 |
+
sentiment_result = sentiment_analyzer(summary)
|
71 |
+
label = sentiment_result[0]['label']
|
72 |
+
score = sentiment_result[0]['score']
|
73 |
+
|
74 |
+
if score >= 0.53:
|
75 |
+
email_label = "Important"
|
76 |
+
else:
|
77 |
+
email_label = "Not Important"
|
78 |
+
|
79 |
+
print(f"From: {sender}")
|
80 |
+
print(f"Email Subject: {subject_str}")
|
81 |
+
print(f"Generated Summary: {summary}")
|
82 |
+
print(f"Sentiment Label: {email_label}")
|
83 |
+
print(f"Sentiment Score: {score}")
|
84 |
+
print("-" * 50)
|
85 |
+
|
86 |
+
mail.logout()
|