Seth commited on
Commit
7c86e14
·
1 Parent(s): 03708bb
Files changed (2) hide show
  1. backend/app/gpt_service.py +21 -9
  2. 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
- # Extract variables from contact (handle empty strings and None)
16
- first_name = (contact.get("first_name") or contact.get("First Name") or "").strip()
17
- last_name = (contact.get("last_name") or contact.get("Last Name") or "").strip()
18
- company = (contact.get("company") or contact.get("Company") or contact.get("Organization") or "").strip()
19
- email = (contact.get("email") or contact.get("Email") or "").strip()
20
- title = (contact.get("title") or contact.get("Title") or contact.get("Job Title") or "").strip()
21
- industry = (contact.get("industry") or contact.get("Industry") or "").strip()
22
- location = (contact.get("location") or contact.get("Location") or "").strip()
23
- company_size = (contact.get("company_size") or contact.get("Company Size") or "").strip()
 
 
 
 
 
 
 
 
 
 
 
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
- # Ensure first_name and last_name are not None/empty
399
- first_name = contact.get('first_name', '').strip() or 'Contact'
400
- last_name = contact.get('last_name', '').strip() or ''
401
- company = contact.get('company', '').strip() or ''
402
- title = contact.get('title', '').strip() or ''
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 and subject.strip():
410
- custom_variables[f'subject_{i}'] = subject
 
411
  if i in contact.get('bodies', {}):
412
  body = contact['bodies'][i]
413
- if body and body.strip():
414
- custom_variables[f'body_{i}'] = body
 
415
 
416
  lead = {
417
- "email": contact['email'].strip(),
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,