Update app.py
Browse files
app.py
CHANGED
|
@@ -23,17 +23,79 @@ credentials = Credentials.from_service_account_info(service_account_info, scopes
|
|
| 23 |
service = build('sheets', 'v4', credentials=credentials)
|
| 24 |
sheet = service.spreadsheets()
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
def save_user_info(email, name, professional_title, industry, target_audience, personal_background, company_url):
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
# Step 1: User profile management UI
|
| 39 |
def user_profile(email, name, professional_title, industry, target_audience, personal_background, company_url):
|
|
@@ -130,22 +192,86 @@ def generate_email(prospect_name, linkedin_url, website_url, context_url, word_c
|
|
| 130 |
def email_agent(prospect_name, linkedin_url, website_url, context_url, word_count, email_purpose, interested_position):
|
| 131 |
return "<div style='font-style: italic;'>Hold on tight, your personalized email is on the way...</div>", generate_email(prospect_name, linkedin_url, website_url, context_url, word_count, email_purpose, interested_position)
|
| 132 |
|
| 133 |
-
# Gradio
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
profile_iface = gr.Interface(
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
gr.Textbox(label="
|
| 138 |
-
gr.
|
| 139 |
-
gr.Textbox(label="
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
gr.Textbox(label="
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
email_iface = gr.Interface(
|
| 151 |
fn=email_agent,
|
|
|
|
| 23 |
service = build('sheets', 'v4', credentials=credentials)
|
| 24 |
sheet = service.spreadsheets()
|
| 25 |
|
| 26 |
+
def fetch_user_details(email):
|
| 27 |
+
"""
|
| 28 |
+
Fetch user details from Google Sheet based on email.
|
| 29 |
+
"""
|
| 30 |
+
try:
|
| 31 |
+
result = sheet.values().get(spreadsheetId=SPREADSHEET_ID, range="Sheet1!A:G").execute()
|
| 32 |
+
rows = result.get('values', [])
|
| 33 |
+
for row in rows:
|
| 34 |
+
if row[0] == email: # Assuming email is in the first column (A)
|
| 35 |
+
return {
|
| 36 |
+
"email": row[0],
|
| 37 |
+
"name": row[1],
|
| 38 |
+
"professional_title": row[2],
|
| 39 |
+
"industry": row[3],
|
| 40 |
+
"target_audience": row[4],
|
| 41 |
+
"personal_background": row[5]
|
| 42 |
+
"company_url": row[6]
|
| 43 |
+
}
|
| 44 |
+
return None
|
| 45 |
+
except Exception as e:
|
| 46 |
+
return f"Error fetching details: {str(e)}"
|
| 47 |
+
|
| 48 |
def save_user_info(email, name, professional_title, industry, target_audience, personal_background, company_url):
|
| 49 |
+
"""
|
| 50 |
+
Save or update user details in Google Sheet.
|
| 51 |
+
"""
|
| 52 |
+
try:
|
| 53 |
+
result = sheet.values().get(spreadsheetId=SPREADSHEET_ID, range="Sheet1!A:G").execute()
|
| 54 |
+
rows = result.get('values', [])
|
| 55 |
+
updated = False
|
| 56 |
+
|
| 57 |
+
# Check if email exists and update the row
|
| 58 |
+
for i, row in enumerate(rows):
|
| 59 |
+
if row[0] == email:
|
| 60 |
+
rows[i] = [email, name, professional_title, industry, target_audience, personal_background, company_url]
|
| 61 |
+
updated = True
|
| 62 |
+
break
|
| 63 |
+
|
| 64 |
+
if not updated: # Append a new row if email doesn't exist
|
| 65 |
+
rows.append([email, name, professional_title, industry, target_audience, personal_background, company_url])
|
| 66 |
+
|
| 67 |
+
# Save updated rows back to the sheet
|
| 68 |
+
body = {"values": rows}
|
| 69 |
+
sheet.values().update(
|
| 70 |
+
spreadsheetId=SPREADSHEET_ID, range="Sheet1!A:G",
|
| 71 |
+
valueInputOption="RAW", body=body
|
| 72 |
+
).execute()
|
| 73 |
+
return "Details saved successfully."
|
| 74 |
+
except Exception as e:
|
| 75 |
+
return f"Error saving details: {str(e)}"
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
def handle_user_details(email, name=None, professional_title=None, industry=None, target_audience=None, personal_background=None, company_url=None, action="verify"):
|
| 80 |
+
if action == "verify":
|
| 81 |
+
# Fetch details based on email
|
| 82 |
+
user_details = fetch_user_details(email)
|
| 83 |
+
if user_details:
|
| 84 |
+
return (
|
| 85 |
+
f"Record found for {email}. You can update details if needed.",
|
| 86 |
+
user_details # Return existing details
|
| 87 |
+
)
|
| 88 |
+
else:
|
| 89 |
+
return (
|
| 90 |
+
f"No record found for {email}. Please enter details and save.",
|
| 91 |
+
{} # Return empty fields
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
elif action == "save":
|
| 95 |
+
# Save or update details
|
| 96 |
+
result = save_user_info(email, name, professional_title, industry, target_audience, personal_background, company_url)
|
| 97 |
+
return result, {}
|
| 98 |
+
|
| 99 |
|
| 100 |
# Step 1: User profile management UI
|
| 101 |
def user_profile(email, name, professional_title, industry, target_audience, personal_background, company_url):
|
|
|
|
| 192 |
def email_agent(prospect_name, linkedin_url, website_url, context_url, word_count, email_purpose, interested_position):
|
| 193 |
return "<div style='font-style: italic;'>Hold on tight, your personalized email is on the way...</div>", generate_email(prospect_name, linkedin_url, website_url, context_url, word_count, email_purpose, interested_position)
|
| 194 |
|
| 195 |
+
# Gradio Interface
|
| 196 |
+
def verify_user(email):
|
| 197 |
+
# Logic for verifying the email
|
| 198 |
+
message, user_details = handle_user_details(email, action="verify")
|
| 199 |
+
# Populate fields if user details are found
|
| 200 |
+
if user_details:
|
| 201 |
+
return (
|
| 202 |
+
message,
|
| 203 |
+
gr.update(visible=True, value=user_details.get("name", "")),
|
| 204 |
+
gr.update(visible=True, value=user_details.get("professional_title", "")),
|
| 205 |
+
gr.update(visible=True, value=user_details.get("industry", "")),
|
| 206 |
+
gr.update(visible=True, value=user_details.get("target_audience", "")),
|
| 207 |
+
gr.update(visible=True, value=user_details.get("personal_background", "")),
|
| 208 |
+
gr.update(visible=True, value=user_details.get("company_url", ""))
|
| 209 |
+
)
|
| 210 |
+
else:
|
| 211 |
+
# Show empty fields for new user
|
| 212 |
+
return (
|
| 213 |
+
message,
|
| 214 |
+
gr.update(visible=True, value=""),
|
| 215 |
+
gr.update(visible=True, value=""),
|
| 216 |
+
gr.update(visible=True, value=""),
|
| 217 |
+
gr.update(visible=True, value=""),
|
| 218 |
+
gr.update(visible=True, value=""),
|
| 219 |
+
gr.update(visible=True, value="")
|
| 220 |
+
)
|
| 221 |
+
|
| 222 |
+
|
| 223 |
+
def save_user(email, name, professional_title, industry, target_audience, personal_background, company_url):
|
| 224 |
+
# Logic for saving the user details
|
| 225 |
+
message, _ = handle_user_details(email, name, professional_title, industry, target_audience, personal_background, company_url, action="save")
|
| 226 |
+
return message
|
| 227 |
+
|
| 228 |
profile_iface = gr.Interface(
|
| 229 |
+
with gr.Blocks() as ui:
|
| 230 |
+
with gr.Row():
|
| 231 |
+
email_input = gr.Textbox(label="Email", placeholder="Enter your email")
|
| 232 |
+
verify_button = gr.Button("Verify")
|
| 233 |
+
status = gr.Textbox(label="Status", interactive=False)
|
| 234 |
+
|
| 235 |
+
# Other fields are initially hidden
|
| 236 |
+
with gr.Row(visible=False) as details_section:
|
| 237 |
+
name_input = gr.Textbox(label="Name")
|
| 238 |
+
professional_title_input = gr.Textbox(label="Professional Title")
|
| 239 |
+
industry_input = gr.Textbox(label="Industry")
|
| 240 |
+
target_audience_input = gr.Textbox(label="Target Audience")
|
| 241 |
+
personal_background_input = gr.Textbox(label="Personal Background")
|
| 242 |
+
company_url_input = gr.Textbox(label="Company URL")
|
| 243 |
+
save_button = gr.Button("Save")
|
| 244 |
+
|
| 245 |
+
# Events
|
| 246 |
+
verify_button.click(
|
| 247 |
+
verify_user,
|
| 248 |
+
inputs=[email_input],
|
| 249 |
+
outputs=[
|
| 250 |
+
status,
|
| 251 |
+
name_input,
|
| 252 |
+
professional_title_input,
|
| 253 |
+
industry_input,
|
| 254 |
+
target_audience_input,
|
| 255 |
+
personal_background_input,
|
| 256 |
+
company_url_input,
|
| 257 |
+
]
|
| 258 |
+
title="Step 1: User Profile",
|
| 259 |
+
description="Enter and save your personal and company information."
|
| 260 |
+
)
|
| 261 |
+
|
| 262 |
+
save_button.click(
|
| 263 |
+
save_user,
|
| 264 |
+
inputs=[
|
| 265 |
+
email_input,
|
| 266 |
+
name_input,
|
| 267 |
+
professional_title_input,
|
| 268 |
+
industry_input,
|
| 269 |
+
target_audience_input,
|
| 270 |
+
personal_background_input,
|
| 271 |
+
company_url_input,
|
| 272 |
+
],
|
| 273 |
+
outputs=[status]
|
| 274 |
+
)
|
| 275 |
|
| 276 |
email_iface = gr.Interface(
|
| 277 |
fn=email_agent,
|