xiddiqui commited on
Commit
6ad853b
·
1 Parent(s): 1bb0c9b

updated app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -60
app.py CHANGED
@@ -39,19 +39,12 @@ KEYWORDS = {
39
  ]
40
  }
41
 
42
- def get_gmail_service(state_dict):
43
  """Creates Gmail API service"""
44
  creds = None
45
 
46
- # Check if token exists in state
47
- if 'token' in state_dict:
48
- creds = Credentials.from_authorized_user_info(state_dict['token'], SCOPES)
49
-
50
- # If credentials are invalid or don't exist
51
- if not creds or not creds.valid:
52
- if creds and creds.expired and creds.refresh_token:
53
- creds.refresh(Request())
54
- else:
55
  # Configure OAuth2 with redirect URI
56
  flow = InstalledAppFlow.from_client_config(
57
  CLIENT_CONFIG,
@@ -66,12 +59,12 @@ def get_gmail_service(state_dict):
66
  prompt='consent'
67
  )
68
 
69
- # Create an iframe for Google Sign-In
70
- return f"""
71
- <iframe src="{auth_url}" width="100%" height="600px" frameborder="0"></iframe>
72
- """
73
 
74
- return build('gmail', 'v1', credentials=creds)
75
 
76
  def get_email_content(service, msg_id):
77
  """Retrieves email content and metadata"""
@@ -135,13 +128,20 @@ def classify_email(email_data):
135
 
136
  def fetch_emails(days_back, auth_code="", include_job=True, include_personal=True, progress=gr.Progress()):
137
  """Main function to fetch and filter emails"""
138
- state_dict = {}
 
 
 
 
 
 
 
139
 
140
  try:
141
- service = get_gmail_service(state_dict)
142
- if isinstance(service, str):
143
- # This means we got an auth URL instead of a service
144
- return service
145
 
146
  # Search for recent emails
147
  query = f'after:{int((datetime.now() - timedelta(days=int(days_back))).timestamp())}'
@@ -200,47 +200,43 @@ def fetch_emails(days_back, auth_code="", include_job=True, include_personal=Tru
200
  return f"Error: {str(e)}"
201
 
202
  # Create Gradio interface
203
- def create_interface():
204
- with gr.Blocks(title="Email Filter") as demo:
205
- gr.Markdown("# 📧 Smart Email Filter")
206
- gr.Markdown("Connect to your Gmail account to filter important emails")
207
-
208
- # Add HTML component for OAuth iframe
209
- auth_html = gr.HTML()
210
-
211
- with gr.Row():
212
- days_back = gr.Slider(
213
- minimum=1,
214
- maximum=30,
215
- value=7,
216
- step=1,
217
- label="Days to look back"
218
- )
219
- include_job = gr.Checkbox(
220
- value=True,
221
- label="Include Job Related Emails"
222
- )
223
- include_personal = gr.Checkbox(
224
- value=True,
225
- label="Include Personal Emails"
226
- )
227
-
228
- fetch_button = gr.Button("Connect and Fetch Emails")
229
- output = gr.Textbox(
230
- label="Results",
231
- lines=20,
232
- show_copy_button=True
233
  )
234
-
235
- # Update the click handler
236
- fetch_button.click(
237
- fn=fetch_emails,
238
- inputs=[days_back, include_job, include_personal],
239
- outputs=[auth_html, output]
 
240
  )
241
 
242
- return demo
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
243
 
244
- if __name__ == "__main__":
245
- demo = create_interface()
246
- demo.launch()
 
39
  ]
40
  }
41
 
42
+ def get_gmail_service(auth_code=None):
43
  """Creates Gmail API service"""
44
  creds = None
45
 
46
+ if auth_code:
47
+ try:
 
 
 
 
 
 
 
48
  # Configure OAuth2 with redirect URI
49
  flow = InstalledAppFlow.from_client_config(
50
  CLIENT_CONFIG,
 
59
  prompt='consent'
60
  )
61
 
62
+ return auth_url
63
+
64
+ except Exception as e:
65
+ return f"Error in authentication: {str(e)}"
66
 
67
+ return None
68
 
69
  def get_email_content(service, msg_id):
70
  """Retrieves email content and metadata"""
 
128
 
129
  def fetch_emails(days_back, auth_code="", include_job=True, include_personal=True, progress=gr.Progress()):
130
  """Main function to fetch and filter emails"""
131
+ if not auth_code:
132
+ auth_url = get_gmail_service()
133
+ if auth_url:
134
+ return f"""Please visit this URL to authorize the application:
135
+
136
+ {auth_url}
137
+
138
+ After authorization, you will receive a code. Please paste it here and click 'Connect and Fetch Emails' again."""
139
 
140
  try:
141
+ # Process the auth code and fetch emails
142
+ flow = InstalledAppFlow.from_client_config(CLIENT_CONFIG, SCOPES)
143
+ flow.fetch_token(code=auth_code)
144
+ service = build('gmail', 'v1', credentials=flow.credentials)
145
 
146
  # Search for recent emails
147
  query = f'after:{int((datetime.now() - timedelta(days=int(days_back))).timestamp())}'
 
200
  return f"Error: {str(e)}"
201
 
202
  # Create Gradio interface
203
+ with gr.Blocks(title="Email Filter") as demo:
204
+ gr.Markdown("# 📧 Smart Email Filter")
205
+ gr.Markdown("Connect to your Gmail account to filter important emails")
206
+
207
+ with gr.Row():
208
+ days_back = gr.Slider(
209
+ minimum=1,
210
+ maximum=30,
211
+ value=7,
212
+ step=1,
213
+ label="Days to look back"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
214
  )
215
+ include_job = gr.Checkbox(
216
+ value=True,
217
+ label="Include Job Related Emails"
218
+ )
219
+ include_personal = gr.Checkbox(
220
+ value=True,
221
+ label="Include Personal Emails"
222
  )
223
 
224
+ auth_code = gr.Textbox(
225
+ label="Authorization Code",
226
+ placeholder="Enter the authorization code here after visiting the auth URL"
227
+ )
228
+
229
+ fetch_button = gr.Button("Connect and Fetch Emails")
230
+ output = gr.Textbox(
231
+ label="Results",
232
+ lines=20,
233
+ show_copy_button=True
234
+ )
235
+
236
+ fetch_button.click(
237
+ fn=fetch_emails,
238
+ inputs=[days_back, auth_code, include_job, include_personal],
239
+ outputs=output
240
+ )
241
 
242
+ demo.launch()