File size: 5,803 Bytes
14cb7ae |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# models/suggestions.py
from .model_loader import load_model
from .logging_config import logger
def generate_suggestions(text, data=None):
try:
# Ensure text is string
text = str(text) if text is not None else ""
# Safely convert data values
if data:
processed_data = {}
for key, value in data.items():
if isinstance(value, (int, float)):
processed_data[key] = str(value)
else:
processed_data[key] = str(value) if value is not None else ""
data = processed_data
# Initialize suggestions
suggestions = {
'improvements': [],
'warnings': [],
'recommendations': [],
'confidence': 0.0
}
# Load model for analysis
classifier = load_model("zero-shot-classification", "typeform/mobilebert-uncased-mnli")
# Define suggestion categories
categories = [
"property description improvement",
"price adjustment needed",
"documentation required",
"verification needed",
"legal compliance issue",
"location verification needed",
"property specification update",
"image quality improvement",
"market value adjustment",
"contact information update"
]
# Analyze text with context
context = f"{text} property_data:{str(data) if data else ''}"
result = classifier(context, categories, multi_label=True)
# Process results
for label, score in zip(result['labels'], result['scores']):
if score > 0.3: # Only include high confidence suggestions
suggestion = {
'type': label,
'confidence': float(score),
'details': generate_suggestion_details(label, text, data)
}
if 'improvement' in label or 'update' in label:
suggestions['improvements'].append(suggestion)
elif 'warning' in label or 'issue' in label:
suggestions['warnings'].append(suggestion)
else:
suggestions['recommendations'].append(suggestion)
# Calculate overall confidence
if result['scores']:
suggestions['confidence'] = float(max(result['scores']))
return suggestions
except Exception as e:
logger.error(f"Error generating suggestions: {str(e)}")
return {
'improvements': [],
'warnings': [],
'recommendations': [],
'confidence': 0.0,
'error': str(e)
}
def generate_suggestion_details(suggestion_type, text, data):
"""Generate detailed suggestions based on the type."""
try:
details = {
'property description improvement': {
'title': 'Improve Property Description',
'message': 'Add more detailed information about the property features and amenities.',
'priority': 'medium'
},
'price adjustment needed': {
'title': 'Review Property Price',
'message': 'Consider adjusting the price based on market conditions and property specifications.',
'priority': 'high'
},
'documentation required': {
'title': 'Additional Documentation Needed',
'message': 'Please provide more property-related documents for verification.',
'priority': 'high'
},
'verification needed': {
'title': 'Property Verification Required',
'message': 'Additional verification steps are needed for property authenticity.',
'priority': 'high'
},
'legal compliance issue': {
'title': 'Legal Compliance Check',
'message': 'Review property legal documentation and compliance status.',
'priority': 'high'
},
'location verification needed': {
'title': 'Location Verification',
'message': 'Verify property location details and coordinates.',
'priority': 'medium'
},
'property specification update': {
'title': 'Update Property Specifications',
'message': 'Review and update property specifications for accuracy.',
'priority': 'medium'
},
'image quality improvement': {
'title': 'Improve Image Quality',
'message': 'Add more high-quality images of the property.',
'priority': 'low'
},
'market value adjustment': {
'title': 'Market Value Review',
'message': 'Review and adjust market value based on current market conditions.',
'priority': 'high'
},
'contact information update': {
'title': 'Update Contact Information',
'message': 'Ensure contact information is complete and up-to-date.',
'priority': 'low'
}
}
return details.get(suggestion_type, {
'title': 'General Suggestion',
'message': 'Review property listing for improvements.',
'priority': 'medium'
})
except Exception as e:
logger.error(f"Error generating suggestion details: {str(e)}")
return {
'title': 'Error',
'message': 'Could not generate detailed suggestion.',
'priority': 'low'
}
|