|
|
|
|
|
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, |
|
'model_used': 'static_fallback' |
|
} |
|
|
|
|
|
try: |
|
classifier = load_model("zero-shot-classification") |
|
except Exception as e: |
|
logger.error(f"Error loading model in suggestions: {str(e)}") |
|
suggestions['warnings'].append({'type': 'error', 'confidence': 0.0, 'details': {'title': 'Model Error', 'message': f'Model loading error: {str(e)}', 'priority': 'high'}}) |
|
return suggestions |
|
|
|
|
|
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 ''}" |
|
|
|
try: |
|
result = classifier(context[:1000], categories, multi_label=True) |
|
|
|
|
|
for label, score in zip(result['labels'], result['scores']): |
|
if score > 0.3: |
|
suggestion_details = generate_suggestion_details(label, text, data) |
|
if suggestion_details: |
|
if 'improvement' in label.lower(): |
|
suggestions['improvements'].append(suggestion_details) |
|
elif 'warning' in label.lower() or 'issue' in label.lower(): |
|
suggestions['warnings'].append(suggestion_details) |
|
else: |
|
suggestions['recommendations'].append(suggestion_details) |
|
|
|
|
|
if result['scores']: |
|
suggestions['confidence'] = max(result['scores']) |
|
|
|
suggestions['model_used'] = getattr(classifier, 'fallback_model', 'primary_model') |
|
|
|
except Exception as e: |
|
logger.error(f"Error in suggestions analysis: {str(e)}") |
|
suggestions['warnings'].append({'type': 'error', 'confidence': 0.0, 'details': {'title': 'Analysis Error', 'message': f'Analysis error: {str(e)}', 'priority': 'medium'}}) |
|
|
|
return suggestions |
|
|
|
except Exception as e: |
|
logger.error(f"Error generating suggestions: {str(e)}") |
|
return { |
|
'improvements': [], |
|
'warnings': [{'type': 'error', 'confidence': 0.0, 'details': {'title': 'System Error', 'message': f'System error: {str(e)}', 'priority': 'high'}}], |
|
'recommendations': [], |
|
'confidence': 0.0, |
|
'model_used': 'static_fallback' |
|
} |
|
|
|
def generate_suggestion_details(suggestion_type, text, data): |
|
"""Generate detailed suggestions based on type""" |
|
try: |
|
if 'description improvement' in suggestion_type.lower(): |
|
return { |
|
'type': 'description_improvement', |
|
'confidence': 0.8, |
|
'details': { |
|
'title': 'Improve Property Description', |
|
'message': 'Add more details about amenities, location benefits, and unique features.', |
|
'priority': 'medium', |
|
'suggestions': [ |
|
'Include nearby landmarks and transportation', |
|
'Describe interior features and finishes', |
|
'Mention parking and security features', |
|
'Add information about neighborhood' |
|
] |
|
} |
|
} |
|
elif 'price adjustment' in suggestion_type.lower(): |
|
return { |
|
'type': 'price_adjustment', |
|
'confidence': 0.7, |
|
'details': { |
|
'title': 'Review Property Price', |
|
'message': 'Consider adjusting the price based on market conditions and property features.', |
|
'priority': 'high', |
|
'suggestions': [ |
|
'Compare with similar properties in the area', |
|
'Consider current market trends', |
|
'Factor in property condition and age', |
|
'Include all amenities in pricing' |
|
] |
|
} |
|
} |
|
elif 'documentation required' in suggestion_type.lower(): |
|
return { |
|
'type': 'documentation_required', |
|
'confidence': 0.9, |
|
'details': { |
|
'title': 'Additional Documentation Needed', |
|
'message': 'Provide more documents to increase property verification.', |
|
'priority': 'high', |
|
'suggestions': [ |
|
'Upload property title documents', |
|
'Include recent utility bills', |
|
'Add property tax receipts', |
|
'Provide floor plan or layout' |
|
] |
|
} |
|
} |
|
elif 'verification needed' in suggestion_type.lower(): |
|
return { |
|
'type': 'verification_needed', |
|
'confidence': 0.8, |
|
'details': { |
|
'title': 'Property Verification Required', |
|
'message': 'Additional verification steps needed for property authenticity.', |
|
'priority': 'high', |
|
'suggestions': [ |
|
'Verify property ownership', |
|
'Check for any legal disputes', |
|
'Confirm property dimensions', |
|
'Validate address details' |
|
] |
|
} |
|
} |
|
else: |
|
return { |
|
'type': 'general_suggestion', |
|
'confidence': 0.6, |
|
'details': { |
|
'title': 'General Improvement', |
|
'message': 'Consider improving overall property listing quality.', |
|
'priority': 'medium', |
|
'suggestions': [ |
|
'Add more high-quality images', |
|
'Include detailed specifications', |
|
'Provide contact information', |
|
'Update property status regularly' |
|
] |
|
} |
|
} |
|
except Exception as e: |
|
logger.error(f"Error generating suggestion details: {str(e)}") |
|
return None |
|
|