azeus commited on
Commit
3f02f09
Β·
1 Parent(s): f4fea85

updated reqs

Browse files
Files changed (1) hide show
  1. app.py +93 -21
app.py CHANGED
@@ -1,10 +1,20 @@
1
  # app.py
2
  import gradio as gr
3
  from transformers import pipeline
4
- import imaplib
5
  import email
6
  from email.header import decode_header
7
  import traceback
 
 
 
 
 
 
 
 
 
 
8
 
9
 
10
  def create_summarizer():
@@ -15,9 +25,26 @@ def create_summarizer():
15
  def connect_to_email(email_address, app_password, imap_server="imap.gmail.com"):
16
  """Connect to email server using IMAP"""
17
  try:
 
 
 
 
18
  mail = imaplib.IMAP4_SSL(imap_server)
19
- mail.login(email_address, app_password)
20
- return mail, None
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  except Exception as e:
22
  return None, f"Error connecting to email: {str(e)}"
23
 
@@ -47,10 +74,16 @@ def get_emails(mail, num_emails=5):
47
  if email_message.is_multipart():
48
  for part in email_message.walk():
49
  if part.get_content_type() == "text/plain":
50
- body = part.get_payload(decode=True).decode()
51
- break
 
 
 
52
  else:
53
- body = email_message.get_payload(decode=True).decode()
 
 
 
54
 
55
  emails.append({
56
  'subject': subject,
@@ -82,6 +115,10 @@ def summarize_text(summarizer, text, max_length=130, min_length=30):
82
  def process_emails(email_address, app_password, num_emails=5):
83
  """Main function to process and summarize emails"""
84
  try:
 
 
 
 
85
  # Initialize summarizer
86
  summarizer = create_summarizer()
87
 
@@ -96,10 +133,10 @@ def process_emails(email_address, app_password, num_emails=5):
96
  return error
97
 
98
  # Process summaries
99
- output = ""
100
  for i, email_data in enumerate(emails, 1):
101
  summary = summarize_text(summarizer, email_data['body'])
102
- output += f"\nEmail {i}:\n"
103
  output += f"Subject: {email_data['subject']}\n"
104
  output += f"Summary: {summary}\n"
105
  output += "-" * 50 + "\n"
@@ -110,29 +147,57 @@ def process_emails(email_address, app_password, num_emails=5):
110
  return f"Error: {str(e)}\n{traceback.format_exc()}"
111
  finally:
112
  if 'mail' in locals():
113
- mail.logout()
 
 
 
114
 
115
 
116
  # Create Gradio interface
117
  def create_interface():
118
- with gr.Blocks() as demo:
119
  gr.Markdown("""
120
- # Email Summarizer
121
 
122
- This app connects to your email account and summarizes your recent emails using AI.
123
 
124
- **Note:**
125
- - For Gmail, use an App Password (not your regular password)
126
- - Go to Google Account > Security > 2-Step Verification > App Passwords to generate one
 
 
 
 
 
 
127
  """)
128
 
129
  with gr.Row():
130
- email_input = gr.Textbox(label="Email Address")
131
- password_input = gr.Textbox(label="App Password", type="password")
132
- num_emails = gr.Number(label="Number of Emails to Summarize", value=5, minimum=1, maximum=20)
133
-
134
- summarize_btn = gr.Button("Summarize Emails")
135
- output = gr.Textbox(label="Summaries", lines=10)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
136
 
137
  summarize_btn.click(
138
  fn=process_emails,
@@ -140,6 +205,13 @@ def create_interface():
140
  outputs=output
141
  )
142
 
 
 
 
 
 
 
 
143
  return demo
144
 
145
 
 
1
  # app.py
2
  import gradio as gr
3
  from transformers import pipeline
4
+ import imaplib # Using built-in imaplib instead of imaplib-ng
5
  import email
6
  from email.header import decode_header
7
  import traceback
8
+ from email_validator import validate_email, EmailNotValidError
9
+
10
+
11
+ def validate_email_address(email_address):
12
+ """Validate email address format"""
13
+ try:
14
+ validate_email(email_address)
15
+ return True
16
+ except EmailNotValidError:
17
+ return False
18
 
19
 
20
  def create_summarizer():
 
25
  def connect_to_email(email_address, app_password, imap_server="imap.gmail.com"):
26
  """Connect to email server using IMAP"""
27
  try:
28
+ if not validate_email_address(email_address):
29
+ return None, "Invalid email address format"
30
+
31
+ print(f"Attempting to connect to {imap_server}...")
32
  mail = imaplib.IMAP4_SSL(imap_server)
33
+ print("Connection established, attempting login...")
34
+
35
+ try:
36
+ mail.login(email_address, app_password)
37
+ print("Login successful!")
38
+ return mail, None
39
+ except imaplib.IMAP4.error as e:
40
+ if "Invalid credentials" in str(e):
41
+ return None, "Invalid email or password. For Gmail, make sure you're using an App Password and have 2-Step Verification enabled."
42
+ elif "IMAP not enabled" in str(e):
43
+ return None, "IMAP is not enabled for this account. Please enable IMAP in your email settings."
44
+ else:
45
+ return None, f"Login error: {str(e)}"
46
+ except imaplib.IMAP4.error as e:
47
+ return None, f"IMAP error: {str(e)}"
48
  except Exception as e:
49
  return None, f"Error connecting to email: {str(e)}"
50
 
 
74
  if email_message.is_multipart():
75
  for part in email_message.walk():
76
  if part.get_content_type() == "text/plain":
77
+ try:
78
+ body = part.get_payload(decode=True).decode()
79
+ break
80
+ except:
81
+ continue
82
  else:
83
+ try:
84
+ body = email_message.get_payload(decode=True).decode()
85
+ except:
86
+ body = email_message.get_payload()
87
 
88
  emails.append({
89
  'subject': subject,
 
115
  def process_emails(email_address, app_password, num_emails=5):
116
  """Main function to process and summarize emails"""
117
  try:
118
+ # Basic validation
119
+ if not email_address or not app_password:
120
+ return "Please provide both email address and password."
121
+
122
  # Initialize summarizer
123
  summarizer = create_summarizer()
124
 
 
133
  return error
134
 
135
  # Process summaries
136
+ output = "πŸ“§ Email Summaries:\n" + "=" * 50 + "\n"
137
  for i, email_data in enumerate(emails, 1):
138
  summary = summarize_text(summarizer, email_data['body'])
139
+ output += f"\nπŸ“Œ Email {i}:\n"
140
  output += f"Subject: {email_data['subject']}\n"
141
  output += f"Summary: {summary}\n"
142
  output += "-" * 50 + "\n"
 
147
  return f"Error: {str(e)}\n{traceback.format_exc()}"
148
  finally:
149
  if 'mail' in locals():
150
+ try:
151
+ mail.logout()
152
+ except:
153
+ pass
154
 
155
 
156
  # Create Gradio interface
157
  def create_interface():
158
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
159
  gr.Markdown("""
160
+ # πŸ“§ Email Summarizer AI
161
 
162
+ This app connects to your email account and provides AI-powered summaries of your recent emails.
163
 
164
+ ### Setup Instructions:
165
+ 1. For Gmail users:
166
+ - Enable 2-Step Verification in your Google Account
167
+ - Generate an App Password: Google Account β†’ Security β†’ 2-Step Verification β†’ App Passwords
168
+ - Use the generated App Password (not your regular password)
169
+
170
+ 2. For other email providers:
171
+ - Make sure IMAP is enabled
172
+ - Use your regular email and password
173
  """)
174
 
175
  with gr.Row():
176
+ with gr.Column():
177
+ email_input = gr.Textbox(
178
+ label="Email Address",
179
+ placeholder="[email protected]"
180
+ )
181
+ password_input = gr.Textbox(
182
+ label="App Password",
183
+ type="password",
184
+ placeholder="Enter your app password"
185
+ )
186
+ num_emails = gr.Slider(
187
+ label="Number of Emails to Summarize",
188
+ minimum=1,
189
+ maximum=20,
190
+ value=5,
191
+ step=1
192
+ )
193
+ summarize_btn = gr.Button("πŸš€ Summarize Emails", variant="primary")
194
+
195
+ with gr.Column():
196
+ output = gr.Textbox(
197
+ label="Email Summaries",
198
+ lines=20,
199
+ placeholder="Summaries will appear here..."
200
+ )
201
 
202
  summarize_btn.click(
203
  fn=process_emails,
 
205
  outputs=output
206
  )
207
 
208
+ gr.Markdown("""
209
+ ### πŸ”’ Privacy Notice
210
+ - Your credentials are only used for the current session
211
+ - No data is stored permanently
212
+ - All processing happens in real-time
213
+ """)
214
+
215
  return demo
216
 
217