|
|
|
|
|
from .model_loader import load_model |
|
from .logging_config import logger |
|
|
|
def generate_suggestions(text, data=None): |
|
try: |
|
|
|
text = str(text) if text is not None else "" |
|
|
|
|
|
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 |
|
|
|
|
|
suggestions = { |
|
'improvements': [], |
|
'warnings': [], |
|
'recommendations': [], |
|
'confidence': 0.0 |
|
} |
|
|
|
|
|
classifier = load_model("zero-shot-classification", "typeform/mobilebert-uncased-mnli") |
|
|
|
|
|
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" |
|
] |
|
|
|
|
|
context = f"{text} property_data:{str(data) if data else ''}" |
|
result = classifier(context, categories, multi_label=True) |
|
|
|
|
|
for label, score in zip(result['labels'], result['scores']): |
|
if score > 0.3: |
|
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) |
|
|
|
|
|
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' |
|
} |
|
|