sksameermujahid commited on
Commit
adb96cf
·
verified ·
1 Parent(s): 9b6c153

Upload 15 files

Browse files
ai_recommendation_engine.py CHANGED
@@ -2289,13 +2289,31 @@ class AIRecommendationEngine:
2289
  urgency_level, recommendations, email_type
2290
  )
2291
 
2292
- # Send email
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2293
  success = self.send_email(recipient_email, email_content)
2294
 
2295
  return {
2296
  'success': success,
2297
  'recipient': recipient_email,
2298
  'subject': email_content['subject'],
 
 
2299
  'recommendations_count': len(recommendations),
2300
  'personalization': {
2301
  'personality_type': personality_type,
 
2289
  urgency_level, recommendations, email_type
2290
  )
2291
 
2292
+ # For preview mode, don't actually send the email
2293
+ if recipient_email == '[email protected]':
2294
+ return {
2295
+ 'success': True,
2296
+ 'recipient': recipient_email,
2297
+ 'subject': email_content['subject'],
2298
+ 'html_content': email_content['html_content'],
2299
+ 'text_content': email_content['text_content'],
2300
+ 'recommendations_count': len(recommendations),
2301
+ 'personalization': {
2302
+ 'personality_type': personality_type,
2303
+ 'buying_motivation': buying_motivation,
2304
+ 'urgency_level': urgency_level
2305
+ }
2306
+ }
2307
+
2308
+ # Send email for real recipients
2309
  success = self.send_email(recipient_email, email_content)
2310
 
2311
  return {
2312
  'success': success,
2313
  'recipient': recipient_email,
2314
  'subject': email_content['subject'],
2315
+ 'html_content': email_content['html_content'],
2316
+ 'text_content': email_content['text_content'],
2317
  'recommendations_count': len(recommendations),
2318
  'personalization': {
2319
  'personality_type': personality_type,
api_service_enhanced.py CHANGED
@@ -1793,19 +1793,25 @@ class EnhancedLeadQualificationAPI:
1793
  # Generate AI insights
1794
  ai_insights = ai_engine.analyze_user_behavior_with_ai(analysis_data)
1795
 
1796
- # Ensure ChromaDB has properties before generating recommendations
1797
- if hasattr(ai_engine, 'properties_collection') and ai_engine.properties_collection:
1798
- try:
 
 
 
 
 
1799
  collection_count = ai_engine.properties_collection.count()
 
1800
  if collection_count == 0:
1801
  logger.info("🚀 ChromaDB is empty, auto-fetching properties...")
1802
  ai_engine.auto_fetch_and_store_properties()
1803
- except Exception as count_error:
1804
- logger.warning(f"⚠️ Could not check ChromaDB count: {count_error}")
1805
  ai_engine.auto_fetch_and_store_properties()
1806
- else:
1807
- logger.warning("⚠️ ChromaDB not initialized, auto-initializing...")
1808
- ai_engine.initialize_chromadb()
1809
  ai_engine.auto_fetch_and_store_properties()
1810
 
1811
  # Email types to generate
@@ -1853,18 +1859,24 @@ class EnhancedLeadQualificationAPI:
1853
  if email_content.get('success'):
1854
  email_previews.append({
1855
  'email_type': email_type,
1856
- 'subject': self._get_custom_subject(email_type, customer_id, ai_insights),
1857
  'html_content': email_content.get('html_content', ''),
1858
  'text_content': email_content.get('text_content', ''),
1859
  'recommendations_count': len(recommendations),
1860
  'properties_included': [
1861
  {
1862
- 'name': rec.get('property_name', 'Property'),
1863
- 'price': rec.get('price', 0),
1864
- 'type': rec.get('property_type', 'N/A'),
1865
- 'score': rec.get('ai_score', 0)
1866
  } for rec in recommendations[:3]
1867
- ]
 
 
 
 
 
 
1868
  })
1869
  else:
1870
  email_previews.append({
 
1793
  # Generate AI insights
1794
  ai_insights = ai_engine.analyze_user_behavior_with_ai(analysis_data)
1795
 
1796
+ # Ensure ChromaDB is properly initialized and has properties
1797
+ if not hasattr(ai_engine, 'properties_collection') or ai_engine.properties_collection is None:
1798
+ logger.info("🚀 ChromaDB not initialized, initializing...")
1799
+ ai_engine.initialize_chromadb()
1800
+
1801
+ # Check if ChromaDB has properties
1802
+ try:
1803
+ if ai_engine.properties_collection:
1804
  collection_count = ai_engine.properties_collection.count()
1805
+ logger.info(f"📊 ChromaDB has {collection_count} properties")
1806
  if collection_count == 0:
1807
  logger.info("🚀 ChromaDB is empty, auto-fetching properties...")
1808
  ai_engine.auto_fetch_and_store_properties()
1809
+ else:
1810
+ logger.warning("⚠️ ChromaDB collection is None, auto-fetching properties...")
1811
  ai_engine.auto_fetch_and_store_properties()
1812
+ except Exception as count_error:
1813
+ logger.warning(f"⚠️ Could not check ChromaDB count: {count_error}")
1814
+ logger.info("🚀 Auto-fetching properties as fallback...")
1815
  ai_engine.auto_fetch_and_store_properties()
1816
 
1817
  # Email types to generate
 
1859
  if email_content.get('success'):
1860
  email_previews.append({
1861
  'email_type': email_type,
1862
+ 'subject': email_content.get('subject', self._get_custom_subject(email_type, customer_id, ai_insights)),
1863
  'html_content': email_content.get('html_content', ''),
1864
  'text_content': email_content.get('text_content', ''),
1865
  'recommendations_count': len(recommendations),
1866
  'properties_included': [
1867
  {
1868
+ 'name': rec.get('property_name', rec.get('name', rec.get('title', 'Property'))),
1869
+ 'price': rec.get('price', rec.get('marketValue', rec.get('amount', 0))),
1870
+ 'type': rec.get('property_type', rec.get('type', rec.get('propertyTypeName', 'N/A'))),
1871
+ 'score': rec.get('ai_score', rec.get('similarity_score', 0))
1872
  } for rec in recommendations[:3]
1873
+ ],
1874
+ 'ai_insights': {
1875
+ 'personality_type': email_content.get('personalization', {}).get('personality_type', 'N/A'),
1876
+ 'decision_style': ai_insights.get('recommendation_strategy', 'N/A'),
1877
+ 'urgency_level': email_content.get('personalization', {}).get('urgency_level', 'N/A'),
1878
+ 'peak_activity': ai_insights.get('peak_time', 'N/A')
1879
+ }
1880
  })
1881
  else:
1882
  email_previews.append({
templates/ai_lead_analysis.html CHANGED
@@ -1560,9 +1560,26 @@
1560
  </div>
1561
  <div class="mt-3">
1562
  <h6>Email Content Preview:</h6>
1563
- <div class="alert alert-light">
1564
- <small>This email would be sent to the customer based on their tracking data and AI analysis. The content is personalized based on their viewing behavior, preferences, and engagement patterns.</small>
1565
- </div>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1566
  </div>
1567
  ` : `
1568
  <div class="alert alert-danger">
 
1560
  </div>
1561
  <div class="mt-3">
1562
  <h6>Email Content Preview:</h6>
1563
+ ${result.html_content ? `
1564
+ <div class="email-preview-container" style="max-height: 400px; overflow-y: auto; border: 1px solid #ddd; border-radius: 8px; padding: 15px; background: white;">
1565
+ <div class="email-preview-header mb-3">
1566
+ <strong>Subject:</strong> ${result.subject || 'N/A'}<br>
1567
+ <strong>Properties:</strong> ${result.recommendations_count || 0} recommendations<br>
1568
+ <strong>AI Insights:</strong> ${result.ai_insights ? `
1569
+ Personality: ${result.ai_insights.personality_type || 'N/A'} |
1570
+ Urgency: ${result.ai_insights.urgency_level || 'N/A'} |
1571
+ Peak Time: ${result.ai_insights.peak_activity || 'N/A'}
1572
+ ` : 'N/A'}
1573
+ </div>
1574
+ <div class="email-content-preview">
1575
+ ${result.html_content}
1576
+ </div>
1577
+ </div>
1578
+ ` : `
1579
+ <div class="alert alert-light">
1580
+ <small>This email would be sent to the customer based on their tracking data and AI analysis. The content is personalized based on their viewing behavior, preferences, and engagement patterns.</small>
1581
+ </div>
1582
+ `}
1583
  </div>
1584
  ` : `
1585
  <div class="alert alert-danger">
templates/perfect_ai_dashboard.html CHANGED
@@ -779,9 +779,26 @@
779
  </div>
780
  <div class="mt-3">
781
  <h6>Email Content Preview:</h6>
782
- <div class="alert alert-light">
783
- <small>This email would be sent to the customer based on their tracking data and AI analysis. The content is personalized based on their viewing behavior, preferences, and engagement patterns.</small>
784
- </div>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
785
  </div>
786
  ` : `
787
  <div class="alert alert-danger">
 
779
  </div>
780
  <div class="mt-3">
781
  <h6>Email Content Preview:</h6>
782
+ ${result.html_content ? `
783
+ <div class="email-preview-container" style="max-height: 400px; overflow-y: auto; border: 1px solid #ddd; border-radius: 8px; padding: 15px; background: white;">
784
+ <div class="email-preview-header mb-3">
785
+ <strong>Subject:</strong> ${result.subject || 'N/A'}<br>
786
+ <strong>Properties:</strong> ${result.recommendations_count || 0} recommendations<br>
787
+ <strong>AI Insights:</strong> ${result.ai_insights ? `
788
+ Personality: ${result.ai_insights.personality_type || 'N/A'} |
789
+ Urgency: ${result.ai_insights.urgency_level || 'N/A'} |
790
+ Peak Time: ${result.ai_insights.peak_activity || 'N/A'}
791
+ ` : 'N/A'}
792
+ </div>
793
+ <div class="email-content-preview">
794
+ ${result.html_content}
795
+ </div>
796
+ </div>
797
+ ` : `
798
+ <div class="alert alert-light">
799
+ <small>This email would be sent to the customer based on their tracking data and AI analysis. The content is personalized based on their viewing behavior, preferences, and engagement patterns.</small>
800
+ </div>
801
+ `}
802
  </div>
803
  ` : `
804
  <div class="alert alert-danger">
templates/unified_ai_dashboard.html CHANGED
@@ -778,9 +778,26 @@
778
  </div>
779
  <div class="mt-3">
780
  <h6>Email Content Preview:</h6>
781
- <div class="alert alert-light">
782
- <small>This email would be sent to the customer based on their tracking data and AI analysis. The content is personalized based on their viewing behavior, preferences, and engagement patterns.</small>
783
- </div>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
784
  </div>
785
  ` : `
786
  <div class="alert alert-danger">
 
778
  </div>
779
  <div class="mt-3">
780
  <h6>Email Content Preview:</h6>
781
+ ${result.html_content ? `
782
+ <div class="email-preview-container" style="max-height: 400px; overflow-y: auto; border: 1px solid #ddd; border-radius: 8px; padding: 15px; background: white;">
783
+ <div class="email-preview-header mb-3">
784
+ <strong>Subject:</strong> ${result.subject || 'N/A'}<br>
785
+ <strong>Properties:</strong> ${result.recommendations_count || 0} recommendations<br>
786
+ <strong>AI Insights:</strong> ${result.ai_insights ? `
787
+ Personality: ${result.ai_insights.personality_type || 'N/A'} |
788
+ Urgency: ${result.ai_insights.urgency_level || 'N/A'} |
789
+ Peak Time: ${result.ai_insights.peak_activity || 'N/A'}
790
+ ` : 'N/A'}
791
+ </div>
792
+ <div class="email-content-preview">
793
+ ${result.html_content}
794
+ </div>
795
+ </div>
796
+ ` : `
797
+ <div class="alert alert-light">
798
+ <small>This email would be sent to the customer based on their tracking data and AI analysis. The content is personalized based on their viewing behavior, preferences, and engagement patterns.</small>
799
+ </div>
800
+ `}
801
  </div>
802
  ` : `
803
  <div class="alert alert-danger">