Seth commited on
Commit ·
7c86e14
1
Parent(s): 03708bb
update
Browse files- backend/app/gpt_service.py +21 -9
- backend/app/main.py +33 -10
backend/app/gpt_service.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
from openai import OpenAI
|
| 3 |
from typing import Dict, List
|
| 4 |
import re
|
|
@@ -12,15 +13,26 @@ def generate_email_sequence(contact: Dict, prompt_template: str, product_name: s
|
|
| 12 |
Generate a personalized email sequence for a contact using GPT.
|
| 13 |
"""
|
| 14 |
try:
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
# Validate required fields
|
| 26 |
if not email:
|
|
|
|
| 1 |
import os
|
| 2 |
+
import math
|
| 3 |
from openai import OpenAI
|
| 4 |
from typing import Dict, List
|
| 5 |
import re
|
|
|
|
| 13 |
Generate a personalized email sequence for a contact using GPT.
|
| 14 |
"""
|
| 15 |
try:
|
| 16 |
+
# Helper function to safely convert to string and strip
|
| 17 |
+
def safe_str(value):
|
| 18 |
+
if value is None:
|
| 19 |
+
return ""
|
| 20 |
+
if isinstance(value, float):
|
| 21 |
+
# Handle NaN and other float values
|
| 22 |
+
if math.isnan(value):
|
| 23 |
+
return ""
|
| 24 |
+
return str(value).strip()
|
| 25 |
+
return str(value).strip() if value else ""
|
| 26 |
+
|
| 27 |
+
# Extract variables from contact (handle empty strings, None, and float/NaN values)
|
| 28 |
+
first_name = safe_str(contact.get("first_name") or contact.get("First Name") or "")
|
| 29 |
+
last_name = safe_str(contact.get("last_name") or contact.get("Last Name") or "")
|
| 30 |
+
company = safe_str(contact.get("company") or contact.get("Company") or contact.get("Organization") or "")
|
| 31 |
+
email = safe_str(contact.get("email") or contact.get("Email") or "")
|
| 32 |
+
title = safe_str(contact.get("title") or contact.get("Title") or contact.get("Job Title") or "")
|
| 33 |
+
industry = safe_str(contact.get("industry") or contact.get("Industry") or "")
|
| 34 |
+
location = safe_str(contact.get("location") or contact.get("Location") or "")
|
| 35 |
+
company_size = safe_str(contact.get("company_size") or contact.get("Company Size") or "")
|
| 36 |
|
| 37 |
# Validate required fields
|
| 38 |
if not email:
|
backend/app/main.py
CHANGED
|
@@ -13,6 +13,7 @@ import concurrent.futures
|
|
| 13 |
from typing import Dict, List
|
| 14 |
import json
|
| 15 |
import asyncio
|
|
|
|
| 16 |
from datetime import datetime
|
| 17 |
|
| 18 |
from .database import get_db, UploadedFile, Prompt, GeneratedSequence, SmartleadRun
|
|
@@ -395,26 +396,48 @@ async def push_to_smartlead(request: SmartleadPushRequest, db: Session = Depends
|
|
| 395 |
failed_count += 1
|
| 396 |
continue
|
| 397 |
|
| 398 |
-
#
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 403 |
|
| 404 |
# Build custom variables
|
| 405 |
custom_variables = {}
|
| 406 |
for i in range(1, 11):
|
| 407 |
if i in contact.get('subjects', {}):
|
| 408 |
subject = contact['subjects'][i]
|
| 409 |
-
if subject
|
| 410 |
-
|
|
|
|
| 411 |
if i in contact.get('bodies', {}):
|
| 412 |
body = contact['bodies'][i]
|
| 413 |
-
if body
|
| 414 |
-
|
|
|
|
| 415 |
|
| 416 |
lead = {
|
| 417 |
-
"email":
|
| 418 |
"first_name": first_name,
|
| 419 |
"last_name": last_name,
|
| 420 |
"company": company,
|
|
|
|
| 13 |
from typing import Dict, List
|
| 14 |
import json
|
| 15 |
import asyncio
|
| 16 |
+
import math
|
| 17 |
from datetime import datetime
|
| 18 |
|
| 19 |
from .database import get_db, UploadedFile, Prompt, GeneratedSequence, SmartleadRun
|
|
|
|
| 396 |
failed_count += 1
|
| 397 |
continue
|
| 398 |
|
| 399 |
+
# Helper function to safely convert to string and strip
|
| 400 |
+
def safe_str(value):
|
| 401 |
+
if value is None:
|
| 402 |
+
return ""
|
| 403 |
+
if isinstance(value, float):
|
| 404 |
+
# Handle NaN and other float values
|
| 405 |
+
if math.isnan(value):
|
| 406 |
+
return ""
|
| 407 |
+
return str(value).strip()
|
| 408 |
+
return str(value).strip() if value else ""
|
| 409 |
+
|
| 410 |
+
# Ensure first_name and last_name are not None/empty/float
|
| 411 |
+
first_name = safe_str(contact.get('first_name', '')) or 'Contact'
|
| 412 |
+
last_name = safe_str(contact.get('last_name', ''))
|
| 413 |
+
company = safe_str(contact.get('company', ''))
|
| 414 |
+
title = safe_str(contact.get('title', ''))
|
| 415 |
+
email = safe_str(contact.get('email', ''))
|
| 416 |
+
|
| 417 |
+
if not email:
|
| 418 |
+
errors.append({
|
| 419 |
+
"email": "unknown",
|
| 420 |
+
"error": "Missing email address"
|
| 421 |
+
})
|
| 422 |
+
failed_count += 1
|
| 423 |
+
continue
|
| 424 |
|
| 425 |
# Build custom variables
|
| 426 |
custom_variables = {}
|
| 427 |
for i in range(1, 11):
|
| 428 |
if i in contact.get('subjects', {}):
|
| 429 |
subject = contact['subjects'][i]
|
| 430 |
+
subject_str = safe_str(subject) if subject else ""
|
| 431 |
+
if subject_str:
|
| 432 |
+
custom_variables[f'subject_{i}'] = subject_str
|
| 433 |
if i in contact.get('bodies', {}):
|
| 434 |
body = contact['bodies'][i]
|
| 435 |
+
body_str = safe_str(body) if body else ""
|
| 436 |
+
if body_str:
|
| 437 |
+
custom_variables[f'body_{i}'] = body_str
|
| 438 |
|
| 439 |
lead = {
|
| 440 |
+
"email": email,
|
| 441 |
"first_name": first_name,
|
| 442 |
"last_name": last_name,
|
| 443 |
"company": company,
|