Commit
·
3cc9137
1
Parent(s):
4e5a6d5
Update README.md
Browse files
README.md
CHANGED
@@ -1,6 +1,63 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import imaplib
|
2 |
+
import email
|
3 |
+
from transformers import BartForConditionalGeneration, BartTokenizer, pipeline
|
4 |
+
|
5 |
+
# Load pre-trained model and tokenizer for summarization
|
6 |
+
model_name = 'facebook/bart-large-cnn'
|
7 |
+
tokenizer = BartTokenizer.from_pretrained(model_name)
|
8 |
+
model = BartForConditionalGeneration.from_pretrained(model_name)
|
9 |
+
|
10 |
+
# Load sentiment analysis model
|
11 |
+
sentiment_analyzer = pipeline('sentiment-analysis', model='distilbert-base-uncased')
|
12 |
+
|
13 |
+
# Connect to your email account
|
14 |
+
mail = imaplib.IMAP4_SSL('imap.gmail.com') # Example for Gmail, adjust accordingly
|
15 |
+
mail.login('[email protected]', 'your_password')
|
16 |
+
mail.select('inbox') # Select the mailbox you want to retrieve emails from
|
17 |
+
|
18 |
+
# Function to generate summary
|
19 |
+
def generate_summary(email_text):
|
20 |
+
inputs = tokenizer([email_text], return_tensors='pt', max_length=1024, truncation=True)
|
21 |
+
|
22 |
+
with torch.no_grad():
|
23 |
+
summary_ids = model.generate(**inputs)
|
24 |
+
|
25 |
+
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
26 |
+
return summary
|
27 |
+
|
28 |
+
# Search for all emails
|
29 |
+
status, messages = mail.search(None, 'ALL')
|
30 |
+
message_ids = messages[0].split()
|
31 |
+
|
32 |
+
# Process and summarize the latest 10 emails received today
|
33 |
+
for msg_id in message_ids[-10:]:
|
34 |
+
status, msg_data = mail.fetch(msg_id, '(RFC822)')
|
35 |
+
raw_email = msg_data[0][1]
|
36 |
+
msg = email.message_from_bytes(raw_email)
|
37 |
+
sender = msg['From']
|
38 |
+
subject = msg['subject']
|
39 |
+
body = ""
|
40 |
+
|
41 |
+
if msg.is_multipart():
|
42 |
+
for part in msg.walk():
|
43 |
+
if part.get_content_type() == "text/plain":
|
44 |
+
body = part.get_payload(decode=True).decode()
|
45 |
+
break
|
46 |
+
else:
|
47 |
+
body = msg.get_payload(decode=True).decode()
|
48 |
+
|
49 |
+
if body:
|
50 |
+
summary = generate_summary(body)
|
51 |
+
|
52 |
+
# Perform sentiment analysis on the summary
|
53 |
+
sentiment_result = sentiment_analyzer(summary)
|
54 |
+
label = sentiment_result[0]['label']
|
55 |
+
score = sentiment_result[0]['score']
|
56 |
+
|
57 |
+
print(f"From: {sender}")
|
58 |
+
print(f"Email Subject: {subject}")
|
59 |
+
print(f"Generated Summary: {summary}")
|
60 |
+
print(f"Sentiment: {label}, Score: {score}")
|
61 |
+
print("-" * 50)
|
62 |
+
|
63 |
+
mail.logout()
|