Spaces:
Sleeping
Sleeping
import gradio as gr | |
import pandas as pd | |
# Load CSV | |
df = pd.read_csv("data.csv") | |
# Convert to lowercase for easier matching | |
df['description'] = df['description'].astype(str).str.lower() | |
df['location'] = df['location'].astype(str).str.lower() | |
df['neighborhood'] = df['neighborhood'].astype(str).str.lower() | |
# Arabic-to-English mapping | |
type_map = { | |
"شقة": "Apartment", | |
"فيلا": "Villa", | |
"مكتب": "Office" | |
} | |
neighborhood_map = { | |
"المعادي": "Maadi", | |
"6 أكتوبر": "6th of October", | |
"المنصورة": "Mansoura", | |
"أسيوط": "Assiut", | |
"الإسكندرية": "Alexandria" | |
} | |
location_map = neighborhood_map # same values for simplicity | |
# Translate Arabic to English in message | |
def translate_message(msg): | |
for ar, en in type_map.items(): | |
if ar in msg: | |
msg = msg.replace(ar, en) | |
for ar, en in neighborhood_map.items(): | |
if ar in msg: | |
msg = msg.replace(ar, en) | |
for ar, en in location_map.items(): | |
if ar in msg: | |
msg = msg.replace(ar, en) | |
return msg | |
# Chat function | |
def real_estate_agent(message, history): | |
message = translate_message(message.lower()) | |
# Match based on description, location, or neighborhood | |
matched = df[ | |
df['description'].str.contains(message) | | |
df['location'].str.contains(message) | | |
df['neighborhood'].str.contains(message) | |
] | |
if matched.empty: | |
return "❌ Sorry, I couldn't find any properties matching your request." | |
# Return top 3 matches | |
response = "✅ Here are some properties you might like:\n" | |
for _, row in matched.head(3).iterrows(): | |
response += f"\n🏠 {row['type']} in {row['location']} ({row['neighborhood']})\n" | |
response += f"💵 Price: {row['price']} {row['currency']}\n" | |
response += f"🛏 Bedrooms: {row['bedrooms']}, 🛁 Bathrooms: {row['bathrooms']}, 📐 Area: {row['area_m2']} m²\n" | |
response += f"📄 Description: {row['description']}\n" | |
response += "-"*30 + "\n" | |
return response | |
# Launch chat interface | |
gr.ChatInterface(fn=real_estate_agent, title="💬 Real Estate AI Agent (دردشة عقارات)").launch() | |