Spaces:
Runtime error
Runtime error
# 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 | |
try: | |
classifier = load_model("zero-shot-classification", "typeform/mobilebert-uncased-mnli") | |
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 | |
# 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 ''}" | |
try: | |
result = classifier(context, categories, multi_label=True) | |
except Exception as e: | |
logger.error(f"Error in suggestions model inference: {str(e)}") | |
suggestions['warnings'].append({'type': 'error', 'confidence': 0.0, 'details': {'title': 'Model Error', 'message': f'Model inference error: {str(e)}', 'priority': 'high'}}) | |
return suggestions | |
# 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': [{'type': 'error', 'confidence': 0.0, 'details': {'title': 'Error', 'message': f'Error generating suggestions: {str(e)}', 'priority': 'high'}}], | |
'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' | |
} | |