curiousgeorge1292 commited on
Commit
60839be
·
verified ·
1 Parent(s): 070fca3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -26
app.py CHANGED
@@ -85,8 +85,13 @@ def oauth2callback():
85
 
86
  return "Authorization successful. You can now save emails to Gmail drafts"
87
 
88
- #@app.route('/authenticate_gmail')
89
- #def authenticate_gmail():
 
 
 
 
 
90
  # Gmail OAuth2 logic here
91
  #credentials = get_credentials_from_oauth()
92
  #if 'user_id' not in flask.session:
@@ -320,32 +325,31 @@ def generate_email(name, email, prospect_name, linkedin_url, website_url, contex
320
  def email_agent(credentials_info, name, email, prospect_name, linkedin_url, website_url, context_url, word_count, email_purpose, interested_position, company_url, professional_title, personal_background):
321
 
322
  try:
 
 
 
 
 
323
  # Generate the email content
324
  email_content = generate_email(name, email, prospect_name, linkedin_url, website_url, context_url, word_count, email_purpose, interested_position, company_url, professional_title, personal_background)
325
 
326
- # Get credentials from our store
327
- credentials_info = credential_store.get_credentials(email)
 
 
 
 
 
 
 
 
 
 
 
328
 
329
- if credentials_info:
330
- # Save to Gmail drafts
331
- try:
332
- save_to_gmail_drafts(credentials_info,
333
- subject="Generated Email Subject",
334
- body=email_content,
335
- recipient=email # Replace with the recipient's email
336
- )
337
- return ("<div style='color: green;'>Email saved to Gmail drafts successfully.</div>", email_content)
338
- except Exception as e:
339
- return (f"<div style='color: red;'>Error saving to Gmail drafts: {str(e)}</div>", email_content)
340
-
341
- else:
342
- return ("<div style='color: red;'>Please authenticate with Gmail first.</div>",
343
- email_content)
344
-
345
- except Exception as e:
346
- # Handle errors during email generation
347
- error_message = f"<div style='color: red;'>Error generating email: {str(e)}</div>"
348
- return error_message, "" # Return an empty email content on error
349
 
350
  # Gradio Interface
351
  def verify_user(email):
@@ -427,6 +431,13 @@ with gr.Blocks() as ui:
427
  )
428
 
429
  with gr.Tab("Step 2: Email Generation"):
 
 
 
 
 
 
 
430
  prospect_name = gr.Textbox(label="Prospect's Name")
431
  linkedin_url = gr.Textbox(label="LinkedIn Profile URL")
432
  website_url = gr.Textbox(label="Website URL")
@@ -444,8 +455,11 @@ with gr.Blocks() as ui:
444
  return session.get('user_id', "default_user")
445
 
446
  email_button.click(
447
- email_agent,
448
- inputs=[name, email, prospect_name, linkedin_url, website_url, context_url, word_count, email_purpose, interested_position, company_url, professional_title, personal_background
 
 
 
449
  ],
450
  outputs=[email_status, generated_email]
451
  )
 
85
 
86
  return "Authorization successful. You can now save emails to Gmail drafts"
87
 
88
+ @app.route('/authenticate_gmail')
89
+ def authenticate_gmail():
90
+ authorization_url, state = flow.authorization_url(
91
+ access_type='offline',
92
+ include_granted_scopes='true'
93
+ )
94
+ return redirect(authorization_url)
95
  # Gmail OAuth2 logic here
96
  #credentials = get_credentials_from_oauth()
97
  #if 'user_id' not in flask.session:
 
325
  def email_agent(credentials_info, name, email, prospect_name, linkedin_url, website_url, context_url, word_count, email_purpose, interested_position, company_url, professional_title, personal_background):
326
 
327
  try:
328
+ # First check if we have valid credentials
329
+ credentials_info = credential_store.get_credentials(email)
330
+ if not credentials_info:
331
+ return ("<div style='color: red;'>Please click the 'Authenticate with Gmail' button above to connect your Gmail account.</div>", "")
332
+
333
  # Generate the email content
334
  email_content = generate_email(name, email, prospect_name, linkedin_url, website_url, context_url, word_count, email_purpose, interested_position, company_url, professional_title, personal_background)
335
 
336
+ if not email_content:
337
+ return ("<div style='color: red;'>Failed to generate email content.</div>", "")
338
+
339
+ # Save to Gmail drafts
340
+ try:
341
+ save_to_gmail_drafts(credentials_info,
342
+ subject="Generated Email Subject",
343
+ body=email_content,
344
+ recipient=email # Replace with the recipient's email
345
+ )
346
+ return ("<div style='color: green;'>Email saved to Gmail drafts successfully.</div>", email_content)
347
+ except Exception as e:
348
+ return (f"<div style='color: red;'>Error saving to Gmail drafts: {str(e)}</div>", email_content)
349
 
350
+ else:
351
+ return ("<div style='color: red;'>Please authenticate with Gmail first.</div>",
352
+ email_content)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
353
 
354
  # Gradio Interface
355
  def verify_user(email):
 
431
  )
432
 
433
  with gr.Tab("Step 2: Email Generation"):
434
+ auth_link = gr.HTML(f"""
435
+ <div style="margin-bottom: 20px;">
436
+ <a href="/authenticate_gmail" target="_blank" style="background-color: #4285f4; color: white; padding: 10px 20px; text-decoration: none; border-radius: 5px;">
437
+ Authenticate with Gmail
438
+ </a>
439
+ </div>
440
+ """)
441
  prospect_name = gr.Textbox(label="Prospect's Name")
442
  linkedin_url = gr.Textbox(label="LinkedIn Profile URL")
443
  website_url = gr.Textbox(label="Website URL")
 
455
  return session.get('user_id', "default_user")
456
 
457
  email_button.click(
458
+ lambda *args: email_agent(None, *args), # Pass None as credentials_info
459
+ inputs=[
460
+ name, email, prospect_name, linkedin_url, website_url, context_url,
461
+ word_count, email_purpose, interested_position, company_url,
462
+ professional_title, personal_background
463
  ],
464
  outputs=[email_status, generated_email]
465
  )