|
|
|
|
|
from .model_loader import load_model |
|
from .logging_config import logger |
|
from .utils import summarize_text |
|
|
|
def validate_and_format_data(data): |
|
"""Validate and format property data""" |
|
|
|
try: |
|
sq_ft = float(data.get('sq_ft', 0)) |
|
if sq_ft < 100: |
|
sq_ft *= 100 |
|
data['sq_ft'] = int(sq_ft) |
|
except: |
|
data['sq_ft'] = 0 |
|
|
|
|
|
try: |
|
market_value = float(data.get('market_value', 0)) |
|
if market_value > 1000000000: |
|
market_value = market_value / 100 |
|
data['market_value'] = int(market_value) |
|
except: |
|
data['market_value'] = 0 |
|
|
|
|
|
if data.get('amenities'): |
|
if isinstance(data['amenities'], str): |
|
amenities = [a.strip() for a in data['amenities'].split(',') if a.strip()] |
|
data['amenities'] = amenities |
|
elif isinstance(data['amenities'], list): |
|
data['amenities'] = [a.strip() for a in data['amenities'] if a.strip()] |
|
else: |
|
data['amenities'] = [] |
|
|
|
return data |
|
|
|
def format_price(price): |
|
"""Format price in Indian currency format""" |
|
try: |
|
price = float(price) |
|
if price >= 10000000: |
|
return f"₹{price/10000000:.2f} Cr" |
|
elif price >= 100000: |
|
return f"₹{price/100000:.2f} L" |
|
else: |
|
return f"₹{price:,.2f}" |
|
except: |
|
return f"₹{price}" |
|
|
|
def generate_static_summary(data): |
|
"""Generate a conversational property summary""" |
|
|
|
data = validate_and_format_data(data) |
|
price = format_price(data.get('market_value', '0')) |
|
|
|
|
|
property_type = data.get('property_type', 'property').strip() |
|
if property_type.lower() == 'apartment': |
|
property_type = '2 BHK Apartment' |
|
|
|
summary = f""" |
|
Namaste! Let me tell you about this wonderful {property_type} that's {data.get('status', 'available').lower()}. |
|
|
|
This beautiful property is located in {data.get('city', 'the city')}, {data.get('state', '')}. It's a spacious {property_type} spanning {data.get('sq_ft', '0')} square feet, perfect for your family. |
|
|
|
Key Highlights: |
|
• Price: {price} |
|
• Bedrooms: {data.get('bedrooms', '0')} spacious bedrooms |
|
• Bathrooms: {data.get('bathrooms', '0')} modern bathrooms |
|
• Year Built: {data.get('year_built', 'N/A')} |
|
• Parking: {data.get('parking_spaces', '0')} covered parking spaces |
|
""" |
|
|
|
|
|
if data.get('property_description'): |
|
summary += f"\n\nProperty Description:\n{data.get('property_description')}" |
|
|
|
|
|
if data.get('possession_date'): |
|
summary += f"\n• Ready for possession from: {data.get('possession_date')}" |
|
|
|
|
|
if data.get('amenities'): |
|
amenities = data['amenities'] |
|
if len(amenities) > 0: |
|
summary += "\n\nNearby Amenities:\n• " + "\n• ".join(amenities) |
|
|
|
|
|
if data.get('nearby_landmarks'): |
|
landmarks = data['nearby_landmarks'] |
|
if isinstance(landmarks, str): |
|
landmarks = [l.strip() for l in landmarks.split(',') if l.strip()] |
|
if len(landmarks) > 0: |
|
summary += "\n\nNearby Landmarks:\n• " + "\n• ".join(landmarks) |
|
|
|
|
|
summary += "\n\nThis property offers excellent value for money and is located in a prime area. Would you like to know more details about this property?" |
|
|
|
return summary.strip() |
|
|
|
def generate_property_summary(data): |
|
try: |
|
|
|
data = validate_and_format_data(data) |
|
|
|
|
|
property_context = f""" |
|
Property Details: |
|
Name: {data.get('property_name', '')} |
|
Type: {data.get('property_type', '')} |
|
Status: {data.get('status', '')} |
|
Location: {data.get('address', '')}, {data.get('city', '')}, {data.get('state', '')}, {data.get('country', '')} |
|
Size: {data.get('sq_ft', '')} sq. ft. |
|
Price: {format_price(data.get('market_value', '0'))} |
|
Bedrooms: {data.get('bedrooms', '')} |
|
Bathrooms: {data.get('bathrooms', '')} |
|
Year Built: {data.get('year_built', '')} |
|
Parking: {data.get('parking_spaces', '')} spaces |
|
Description: {data.get('property_description', '')} |
|
Possession Date: {data.get('possession_date', '')} |
|
Amenities: {', '.join(data.get('amenities', []))} |
|
Nearby Landmarks: {data.get('nearby_landmarks', '')} |
|
""" |
|
|
|
|
|
try: |
|
summarizer = load_model("summarization", "sshleifer/distilbart-cnn-6-6") |
|
summary_result = summarizer(property_context, max_length=500, min_length=100, do_sample=False) |
|
initial_summary = summary_result[0]['summary_text'] |
|
except Exception as model_error: |
|
logger.warning(f"Model generation failed, using static summary: {str(model_error)}") |
|
initial_summary = generate_static_summary(data) |
|
|
|
|
|
key_features = [] |
|
|
|
|
|
if data.get('property_type') and data.get('status'): |
|
key_features.append(f"This {data['property_type']} is {data['status'].lower()}") |
|
|
|
|
|
location_parts = [] |
|
if data.get('city'): |
|
location_parts.append(data['city']) |
|
if data.get('state'): |
|
location_parts.append(data['state']) |
|
if location_parts: |
|
key_features.append(f"Located in {', '.join(location_parts)}") |
|
|
|
|
|
if data.get('sq_ft'): |
|
key_features.append(f"Spans {data['sq_ft']} sq. ft.") |
|
if data.get('market_value'): |
|
key_features.append(f"Priced at {format_price(data['market_value'])}") |
|
|
|
|
|
rooms_info = [] |
|
if data.get('bedrooms'): |
|
rooms_info.append(f"{data['bedrooms']} bedroom{'s' if data['bedrooms'] != '1' else ''}") |
|
if data.get('bathrooms'): |
|
rooms_info.append(f"{data['bathrooms']} bathroom{'s' if data['bathrooms'] != '1' else ''}") |
|
if rooms_info: |
|
key_features.append(f"Features {' and '.join(rooms_info)}") |
|
|
|
|
|
if data.get('parking_spaces'): |
|
key_features.append(f"Includes {data['parking_spaces']} covered parking space{'s' if data['parking_spaces'] != '1' else ''}") |
|
|
|
|
|
if data.get('possession_date'): |
|
key_features.append(f"Ready for possession from {data['possession_date']}") |
|
|
|
|
|
if data.get('amenities'): |
|
amenities = data['amenities'] |
|
if len(amenities) > 0: |
|
key_features.append(f"Amenities: {', '.join(amenities)}") |
|
|
|
|
|
enhanced_summary = initial_summary |
|
if key_features: |
|
enhanced_summary += "\n\nKey Features:\n• " + "\n• ".join(key_features) |
|
|
|
|
|
enhanced_summary = enhanced_summary.replace(" ", " ").strip() |
|
|
|
return enhanced_summary |
|
except Exception as e: |
|
logger.error(f"Error generating property summary: {str(e)}") |
|
return generate_static_summary(data) |
|
|