Update app.py
Browse files
app.py
CHANGED
|
@@ -17,6 +17,20 @@ app.secret_key = os.environ.get("FLASK_SECRET_KEY")
|
|
| 17 |
REDIRECT_URI = "https://huggingface.co/spaces/curiousgeorge1292/Custom_Profile_Email_Generator/oauth2callback"
|
| 18 |
GMAIL_SCOPES = ["https://www.googleapis.com/auth/gmail.compose"]
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
# Retrieve JSON string from the environment variable
|
| 21 |
client_secrets_json = os.environ.get("GMAIL_OAUTH_SECRET_JSON")
|
| 22 |
|
|
@@ -34,9 +48,18 @@ with open("temp_client_secrets.json", "w") as temp_file:
|
|
| 34 |
flow = Flow.from_client_secrets_file(
|
| 35 |
'temp_client_secrets.json', # Path to the temporary JSON file
|
| 36 |
scopes=['https://www.googleapis.com/auth/gmail.compose'],
|
| 37 |
-
redirect_uri=
|
| 38 |
)
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
@app.route('/oauth2callback')
|
| 41 |
def oauth2callback():
|
| 42 |
flow = Flow.from_client_secrets_file(
|
|
@@ -48,7 +71,7 @@ def oauth2callback():
|
|
| 48 |
|
| 49 |
# Save the credentials to the session
|
| 50 |
credentials = flow.credentials
|
| 51 |
-
|
| 52 |
'token': credentials.token,
|
| 53 |
'refresh_token': credentials.refresh_token,
|
| 54 |
'token_uri': credentials.token_uri,
|
|
@@ -56,26 +79,30 @@ def oauth2callback():
|
|
| 56 |
'client_secret': credentials.client_secret,
|
| 57 |
'scopes': credentials.scopes
|
| 58 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
return "Authorization successful. You can now save emails to Gmail drafts"
|
| 60 |
|
| 61 |
-
@app.route('/authenticate_gmail')
|
| 62 |
-
def authenticate_gmail():
|
| 63 |
# Gmail OAuth2 logic here
|
| 64 |
-
credentials = get_credentials_from_oauth()
|
| 65 |
-
if 'user_id' not in flask.session:
|
| 66 |
-
flask.session['user_id'] = str(uuid.uuid4()) # Generate a unique ID if not already present
|
| 67 |
-
user_id = flask.session.get('user_id') # Get user-specific ID
|
| 68 |
-
redis_store.set(user_id, credentials) # Save credentials to Redis (or a database)
|
| 69 |
# After successful authentication:
|
| 70 |
-
session['credentials'] = credentials
|
| 71 |
-
return "Authentication successful!"
|
| 72 |
|
| 73 |
-
@app.route('/check_credentials')
|
| 74 |
-
def check_credentials():
|
| 75 |
-
if 'credentials' in session:
|
| 76 |
-
return "Credentials are stored in session."
|
| 77 |
-
else:
|
| 78 |
-
return "No credentials found. Please authorize first."
|
| 79 |
|
| 80 |
|
| 81 |
def save_to_gmail_drafts(credentials_info, subject, body, recipient):
|
|
@@ -296,17 +323,24 @@ def email_agent(credentials_info, name, email, prospect_name, linkedin_url, webs
|
|
| 296 |
# Generate the email content
|
| 297 |
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)
|
| 298 |
|
| 299 |
-
#
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 310 |
|
| 311 |
except Exception as e:
|
| 312 |
# Handle errors during email generation
|
|
@@ -410,11 +444,7 @@ with gr.Blocks() as ui:
|
|
| 410 |
return session.get('user_id', "default_user")
|
| 411 |
|
| 412 |
email_button.click(
|
| 413 |
-
|
| 414 |
-
email_agent(
|
| 415 |
-
get_user_id(), # Fetch and pass the user's ID dynamically
|
| 416 |
-
name, email, prospect_name, linkedin_url, website_url, context_url, word_count, email_purpose, interested_position, company_url, professional_title, personal_background
|
| 417 |
-
),
|
| 418 |
inputs=[name, email, prospect_name, linkedin_url, website_url, context_url, word_count, email_purpose, interested_position, company_url, professional_title, personal_background
|
| 419 |
],
|
| 420 |
outputs=[email_status, generated_email]
|
|
|
|
| 17 |
REDIRECT_URI = "https://huggingface.co/spaces/curiousgeorge1292/Custom_Profile_Email_Generator/oauth2callback"
|
| 18 |
GMAIL_SCOPES = ["https://www.googleapis.com/auth/gmail.compose"]
|
| 19 |
|
| 20 |
+
# Create a simple credential storage mechanism
|
| 21 |
+
class CredentialStore:
|
| 22 |
+
def __init__(self):
|
| 23 |
+
self.credentials = {}
|
| 24 |
+
|
| 25 |
+
def set_credentials(self, user_email, creds):
|
| 26 |
+
self.credentials[user_email] = creds
|
| 27 |
+
|
| 28 |
+
def get_credentials(self, user_email):
|
| 29 |
+
return self.credentials.get(user_email)
|
| 30 |
+
|
| 31 |
+
# Create a global instance
|
| 32 |
+
credential_store = CredentialStore()
|
| 33 |
+
|
| 34 |
# Retrieve JSON string from the environment variable
|
| 35 |
client_secrets_json = os.environ.get("GMAIL_OAUTH_SECRET_JSON")
|
| 36 |
|
|
|
|
| 48 |
flow = Flow.from_client_secrets_file(
|
| 49 |
'temp_client_secrets.json', # Path to the temporary JSON file
|
| 50 |
scopes=['https://www.googleapis.com/auth/gmail.compose'],
|
| 51 |
+
redirect_uri=REDIRECT_URI
|
| 52 |
)
|
| 53 |
|
| 54 |
+
def get_user_email_from_credentials(credentials):
|
| 55 |
+
try:
|
| 56 |
+
service = build('gmail', 'v1', credentials=credentials)
|
| 57 |
+
user_info = service.users().getProfile(userId='me').execute()
|
| 58 |
+
return user_info.get('emailAddress')
|
| 59 |
+
except Exception as e:
|
| 60 |
+
print(f"Error getting user email: {e}")
|
| 61 |
+
return None
|
| 62 |
+
|
| 63 |
@app.route('/oauth2callback')
|
| 64 |
def oauth2callback():
|
| 65 |
flow = Flow.from_client_secrets_file(
|
|
|
|
| 71 |
|
| 72 |
# Save the credentials to the session
|
| 73 |
credentials = flow.credentials
|
| 74 |
+
creds_dict = {
|
| 75 |
'token': credentials.token,
|
| 76 |
'refresh_token': credentials.refresh_token,
|
| 77 |
'token_uri': credentials.token_uri,
|
|
|
|
| 79 |
'client_secret': credentials.client_secret,
|
| 80 |
'scopes': credentials.scopes
|
| 81 |
}
|
| 82 |
+
user_email = get_user_email_from_credentials(credentials)
|
| 83 |
+
if user_email:
|
| 84 |
+
credential_store.set_credentials(user_email, creds_dict)
|
| 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:
|
| 93 |
+
#flask.session['user_id'] = str(uuid.uuid4()) # Generate a unique ID if not already present
|
| 94 |
+
#user_id = flask.session.get('user_id') # Get user-specific ID
|
| 95 |
+
#redis_store.set(user_id, credentials) # Save credentials to Redis (or a database)
|
| 96 |
# After successful authentication:
|
| 97 |
+
#session['credentials'] = credentials
|
| 98 |
+
#return "Authentication successful!"
|
| 99 |
|
| 100 |
+
#@app.route('/check_credentials')
|
| 101 |
+
#def check_credentials():
|
| 102 |
+
#if 'credentials' in session:
|
| 103 |
+
#return "Credentials are stored in session."
|
| 104 |
+
#else:
|
| 105 |
+
#return "No credentials found. Please authorize first."
|
| 106 |
|
| 107 |
|
| 108 |
def save_to_gmail_drafts(credentials_info, subject, body, recipient):
|
|
|
|
| 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
|
|
|
|
| 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]
|