| import os |
| os.environ["ANONYMIZED_TELEMETRY"] = "False" |
|
|
| from fastapi import FastAPI, HTTPException, BackgroundTasks |
| from fastapi.middleware.cors import CORSMiddleware |
| from fastapi.staticfiles import StaticFiles |
| from pydantic import BaseModel |
| from typing import Optional, List, Dict |
| import chromadb |
| from chromadb.utils import embedding_functions |
| import uuid |
| from openai import OpenAI |
| from scrapers import scrape_all |
| import requests |
| import base64 |
| import json |
| import os |
| from database import log_interaction, create_user, verify_user, save_chat_message, get_chat_history |
| from collaborative_filter import get_collaborative_recommendations |
|
|
| |
| app = FastAPI(title="Darak AI Real Estate Engine") |
|
|
| |
| app.add_middleware( |
| CORSMiddleware, |
| allow_origins=["*"], |
| allow_credentials=True, |
| allow_methods=["*"], |
| allow_headers=["*"], |
| ) |
|
|
| |
| chroma_client = chromadb.PersistentClient(path="./chroma_db") |
|
|
| |
| OPENROUTER_KEY = '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' |
| openai_client = OpenAI( |
| api_key=OPENROUTER_KEY, |
| base_url="https://openrouter.ai/api/v1" |
| ) |
|
|
| |
| |
| default_ef = embedding_functions.DefaultEmbeddingFunction() |
|
|
| |
| collection = chroma_client.get_or_create_collection( |
| name="egypt_properties", |
| embedding_function=default_ef |
| ) |
|
|
| |
|
|
| |
| |
| |
| class Property(BaseModel): |
| title: str |
| type: str |
| location: str |
| price: str |
| status: str |
| description: str |
| lat: float |
| lng: float |
| image: str |
| matterport_id: str = "" |
|
|
| class UserQuery(BaseModel): |
| goal: str |
| property_type: str |
| budget: str |
| location: str |
| user_id: str = "guest" |
|
|
| class Interaction(BaseModel): |
| user_id: str |
| property_id: str |
| interaction_type: str |
|
|
| class DesignRequest(BaseModel): |
| image_base64: str |
| style: str |
| instructions: str = "" |
|
|
| class ChatRequest(BaseModel): |
| message: str |
| history: list = [] |
|
|
| class PropertyEvaluationRequest(BaseModel): |
| prop_type: str |
| location: str |
| area: float |
| finish: str |
| price: float |
|
|
| class PropertyInsightRequest(BaseModel): |
| title: str |
| price: str |
| location: str |
| type: str |
| description: str |
|
|
| class PropertyCompareRequest(BaseModel): |
| prop1: dict |
| prop2: dict |
|
|
| class AuthRequest(BaseModel): |
| username: str |
| password: str |
|
|
| class AuthenticatedChatRequest(BaseModel): |
| message: Optional[str] = "" |
| user_id: str = "guest" |
| file_data: Optional[str] = None |
| file_name: Optional[str] = None |
| file_type: Optional[str] = None |
|
|
| |
| |
| |
|
|
|
|
| @app.post("/api/auth/register") |
| async def register(req: AuthRequest): |
| user_id = create_user(req.username, req.password) |
| if not user_id: |
| raise HTTPException(status_code=400, detail="Username already exists") |
| return {"token": user_id, "username": req.username} |
|
|
| @app.post("/api/auth/login") |
| async def login(req: AuthRequest): |
| user_id = verify_user(req.username, req.password) |
| if not user_id: |
| raise HTTPException(status_code=401, detail="Invalid username or password") |
| return {"token": user_id, "username": req.username} |
|
|
| @app.post("/api/chat") |
| async def chat_assistant(request: AuthenticatedChatRequest): |
| """ |
| General chat endpoint for the Dark AI assistant. |
| Uses database history and Gemini 2.5 Flash API to provide conversational responses in Arabic. |
| """ |
| try: |
| messages = [ |
| { |
| "role": "system", |
| "content": "أنت مساعد ذكي اسمك 'Dark' متخصص في العقارات في مصر. مهمتك مساعدة المستخدمين في العثور على عقارات، الإجابة على استفساراتهم العقارية، وتقديم نصائح للاستثمار العقاري. يجب أن تكون إجاباتك قصيرة، ودودة، ومفيدة، ودائماً باللغة العربية." |
| } |
| ] |
| |
| |
| history = [] |
| if request.user_id != "guest": |
| history = get_chat_history(request.user_id, limit=10) |
| |
| |
| for msg in history: |
| messages.append({"role": msg.get("role", "user"), "content": msg.get("content", "")}) |
| |
| msg_text = (request.message or "").strip() |
| if request.file_data: |
| f_type = (request.file_type or "").lower() |
| f_name = request.file_name or "attachment" |
| |
| |
| if "image" in f_type or request.file_data.startswith("data:image"): |
| url_data = request.file_data if request.file_data.startswith("data:") else f"data:{f_type or 'image/jpeg'};base64,{request.file_data}" |
| prompt_text = msg_text if msg_text else f"يرجى تحليل هذه الصورة المرفقة ({f_name}) وشرح ما يتعلق بالعقارات والتصميم." |
| user_content = [ |
| {"type": "text", "text": prompt_text}, |
| {"type": "image_url", "image_url": {"url": url_data}} |
| ] |
| messages.append({"role": "user", "content": user_content}) |
| |
| |
| elif "pdf" in f_type or f_name.endswith(".pdf"): |
| extracted_text = "" |
| try: |
| import pypdf, io, base64 |
| b64_str = request.file_data.split(",")[-1] if "," in request.file_data else request.file_data |
| pdf_bytes = base64.b64decode(b64_str) |
| reader = pypdf.PdfReader(io.BytesIO(pdf_bytes)) |
| for page in reader.pages: |
| t = page.extract_text() |
| if t: extracted_text += t + "\n" |
| except Exception as e: |
| print(f"[PDF Extraction Error]: {e}") |
| extracted_text = "[تعذر استخراج النص من ملف PDF]" |
| |
| prompt_text = msg_text if msg_text else "قم بتحليل ملف PDF المرفق بالتفصيل." |
| full_text = f"{prompt_text}\n\n--- [محتوى ملف PDF: {f_name}] ---\n{extracted_text}" |
| messages.append({"role": "user", "content": full_text}) |
| |
| |
| else: |
| try: |
| import base64 |
| b64_str = request.file_data.split(",")[-1] if "," in request.file_data else request.file_data |
| doc_text = base64.b64decode(b64_str).decode("utf-8", errors="ignore") |
| except Exception: |
| doc_text = "[تعذر قراءة محتوى الملف]" |
| |
| prompt_text = msg_text if msg_text else f"قم بتحليل الملف المرفق ({f_name})." |
| full_text = f"{prompt_text}\n\n--- [محتوى الملف: {f_name}] ---\n{doc_text}" |
| messages.append({"role": "user", "content": full_text}) |
| else: |
| messages.append({"role": "user", "content": msg_text or "مرحباً"}) |
|
|
| |
| if request.user_id != "guest": |
| log_text = msg_text if msg_text else f"[ملف مرفق: {request.file_name}]" |
| save_chat_message(request.user_id, "user", log_text) |
|
|
| |
| response = openai_client.chat.completions.create( |
| model="google/gemini-2.5-flash", |
| messages=messages, |
| max_tokens=300 |
| ) |
| |
| reply = response.choices[0].message.content |
| if not reply: |
| return {"reply": 'عذراً، لم أتمكن من معالجة طلبك الآن.'} |
| |
| |
| if request.user_id != "guest": |
| save_chat_message(request.user_id, "assistant", reply) |
| |
| return {"reply": reply} |
| |
| |
| except Exception as e: |
| print(f"[Chat Fatal Error] {str(e)}") |
| return {"reply": "عذراً، حدث خطأ غير متوقع."} |
|
|
| @app.post("/api/evaluate") |
| async def evaluate_property(request: PropertyEvaluationRequest): |
| """ |
| Intelligently analyzes a property's price based on its features using the LLM. |
| """ |
| try: |
| |
| prompt = f"""أنت خبير عقاري محترف. قم بتقييم هذا العقار المعروض للبيع. |
| نوع العقار: {request.prop_type} |
| المنطقة: {request.location} |
| المساحة: {request.area} متر مربع |
| التشطيب: {request.finish} |
| السعر المعروض: {request.price} جنيه |
| |
| قم بتحليل السعر وأرجع الرد بصيغة JSON فقط بالهيكل التالي (لا تكتب أي كلام آخر غير JSON): |
| {{ |
| "verdict_title": "عنوان التقييم (مثال: سعر ممتاز جداً، عادل، أو مبالغ فيه)", |
| "verdict_description": "وصف قصير عن التقييم والسبب", |
| "score_percentage": 85, |
| "average_sqm_price": 20000, |
| "estimated_value": 3000000, |
| "smart_tip": "نصيحة استثمارية سريعة بخصوص هذا العقار" |
| }} |
| """ |
| response = openai_client.chat.completions.create( |
| model="google/gemini-2.5-flash", |
| messages=[{"role": "user", "content": prompt}], |
| max_tokens=800 |
| ) |
| |
| reply = response.choices[0].message.content |
| import re |
| import json |
| |
| content = re.sub(r'^```[a-zA-Z]*\s*', '', reply.strip()) |
| content = re.sub(r'```\s*$', '', content.strip()) |
| |
| json_match = re.search(r'\{[\s\S]*\}', content) |
| if json_match: |
| data = json.loads(json_match.group(0)) |
| return {"success": True, "data": data} |
| else: |
| return {"success": False, "error": "Invalid response format from AI"} |
| |
| except Exception as e: |
| err = str(e) |
| print(f"[Evaluate Error] {err}") |
| if "429" in err or "RESOURCE_EXHAUSTED" in err: |
| return {"success": False, "error": "تجاوزت حصة الذكاء الاصطناعي اليومية. يرجى المحاولة لاحقاً."} |
| return {"success": False, "error": err} |
|
|
| @app.post("/api/property/insight") |
| async def property_insight(request: PropertyInsightRequest): |
| """ |
| Generates a dynamic AI valuation, investment score, and intelligently extracts specs for a specific property. |
| """ |
| try: |
| prompt = f"""أنت مستشار عقاري خبير في السوق المصري. قم بتحليل هذا العقار واستخراج تفاصيله بدقة: |
| العنوان: {request.title} |
| السعر: {request.price} |
| المنطقة: {request.location} |
| النوع: {request.type} |
| الوصف: {request.description} |
| |
| بناءً على ذلك، أرجع الرد بصيغة JSON فقط بالهيكل التالي (لا تكتب أي نصوص أخرى إطلاقاً). إذا لم تكن بعض التفاصيل (مثل عدد الغرف) مذكورة بوضوح، قم بوضع تقدير منطقي بناءً على السعر والمساحة: |
| {{ |
| "valuation_title": "عنوان التقييم (مثال: فرصة ممتازة، سعر عادل، أو أعلى من السوق)", |
| "valuation_text": "جملة واحدة تشرح تقييم السعر.", |
| "roi_percentage": "رقم مئوي (مثال: 12% سنوي)", |
| "roi_progress": 80, |
| "growth_percentage": "رقم مئوي (مثال: + 25% متوقع)", |
| "growth_progress": 90, |
| "specs": {{ |
| "beds": "عدد الغرف المستنتج أو الحقيقي (رقم فقط)", |
| "baths": "عدد الحمامات المستنتج (رقم فقط)", |
| "area": "المساحة بالمتر المربع المستنتجة أو الحقيقية (مثال: 150)", |
| "parking": "عدد مواقف السيارات المستنتج (رقم فقط، مثلا: 1 أو 2)" |
| }} |
| }} |
| """ |
| response = openai_client.chat.completions.create( |
| model="google/gemini-2.5-flash", |
| messages=[{"role": "user", "content": prompt}], |
| max_tokens=600 |
| ) |
| |
| reply = response.choices[0].message.content |
| import re |
| import json |
| content = re.sub(r'^```[a-zA-Z]*\s*', '', reply.strip()) |
| content = re.sub(r'```\s*$', '', content.strip()) |
| |
| json_match = re.search(r'\{[\s\S]*\}', content) |
| if json_match: |
| data = json.loads(json_match.group(0)) |
| return {"success": True, "data": data} |
| else: |
| return {"success": False, "error": "Invalid response format"} |
| |
| except Exception as e: |
| err = str(e) |
| print(f"[Insight Error] {err}") |
| if "429" in err or "RESOURCE_EXHAUSTED" in err: |
| return {"success": False, "error": "تجاوزت حصة الذكاء الاصطناعي اليومية. يرجى المحاولة لاحقاً."} |
| return {"success": False, "error": err} |
|
|
| @app.post("/api/property/compare") |
| async def property_compare(request: PropertyCompareRequest): |
| """ |
| Generates a dynamic AI comparison between two properties and extracts their details. |
| """ |
| try: |
| prompt = f"""أنت مستشار عقاري خبير. قم بإنشاء تقرير مقارنة بين العقارين، مع استخراج (أو استنتاج منطقي بناءً على السعر والوصف) لتفاصيلها. |
| أرجع الرد بصيغة JSON فقط كالتالي (تأكد أن يكون بصيغة JSON صحيحة بدون نصوص أخرى): |
| {{ |
| "summary": "رأيك كمستشار عن أيهما أفضل للاستثمار وأيهما أفضل للسكن...", |
| "prop1": {{ |
| "area": "مساحة تقديرية أو حقيقية (مثال: ١٥٠ م٢)", |
| "rooms": "عدد غرف تقديري (مثال: ٣ نوم)", |
| "finish": "نوع التشطيب (مثال: سوبر لوكس)", |
| "roi": "نسبة مئوية (مثال: ١٠٪)" |
| }}, |
| "prop2": {{ |
| "area": "مساحة تقديرية أو حقيقية", |
| "rooms": "عدد غرف تقديري", |
| "finish": "نوع التشطيب", |
| "roi": "نسبة مئوية" |
| }} |
| }} |
| |
| العقار الأول: |
| العنوان: {request.prop1.get('title')} ({request.prop1.get('location')}) - السعر: {request.prop1.get('price')} |
| الوصف: {request.prop1.get('description')} |
| |
| العقار الثاني: |
| العنوان: {request.prop2.get('title')} ({request.prop2.get('location')}) - السعر: {request.prop2.get('price')} |
| الوصف: {request.prop2.get('description')} |
| """ |
| |
| response = openai_client.chat.completions.create( |
| model="google/gemini-2.5-flash", |
| messages=[{"role": "user", "content": prompt}], |
| max_tokens=600 |
| ) |
| |
| reply = response.choices[0].message.content |
| import re |
| import json |
| content = re.sub(r'^```[a-zA-Z]*\s*', '', reply.strip()) |
| content = re.sub(r'```\s*$', '', content.strip()) |
| json_match = re.search(r'\{[\s\S]*\}', content) |
| |
| if json_match: |
| data = json.loads(json_match.group(0)) |
| return {"success": True, "data": data} |
| else: |
| return {"success": False, "error": "Invalid JSON from AI"} |
| except Exception as e: |
| err = str(e) |
| print(f"[Compare Error] {err}") |
| if "429" in err or "RESOURCE_EXHAUSTED" in err: |
| return {"success": False, "error": "تجاوزت حصة الذكاء الاصطناعي اليومية. يرجى المحاولة لاحقاً."} |
| return {"success": False, "error": err} |
|
|
| @app.post("/api/design/analyze") |
| async def design_analyze(request: DesignRequest): |
| """ |
| Analyzes a room image and suggests design changes using Gemini 2.5 Flash Vision. |
| """ |
| try: |
| print(f"[Design] Analyzing room with style: {request.style}") |
| |
| extra = f"Extra instructions: {request.instructions}" if request.instructions else "" |
| analysis_prompt = ( |
| f"You are an expert interior designer. Analyze this room image and redesign it in {request.style} style. " |
| f"{extra} " |
| "Respond with ONLY a raw JSON object (no markdown, no code fences, no explanation). " |
| "Use this exact structure:\n" |
| '{"room_type": "living room", "design_description": "وصف بالعربية", ' |
| '"image_gen_prompt": "photorealistic modern interior design", ' |
| '"furniture": [{"category": "أريكة", "name": "اسم بالعربية", "price": "9500", "furniture_type": "sofa"}]}' |
| ) |
|
|
| |
| if request.image_base64.startswith('data:'): |
| image_url = request.image_base64 |
| else: |
| image_url = f"data:image/jpeg;base64,{request.image_base64}" |
| |
| print("[Design] Calling OpenRouter API...") |
| headers = { |
| "Authorization": f"Bearer {OPENROUTER_KEY}", |
| "HTTP-Referer": "http://localhost:3000", |
| "X-Title": "Darak" |
| } |
| payload = { |
| "model": "google/gemini-2.5-flash", |
| "messages": [ |
| { |
| "role": "user", |
| "content": [ |
| {"type": "text", "text": analysis_prompt}, |
| {"type": "image_url", "image_url": {"url": image_url}} |
| ] |
| } |
| ] |
| } |
| resp = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=payload) |
| resp_json = resp.json() |
| |
| if "error" in resp_json: |
| print(f"[Design Error] OpenRouter API Error: {resp_json['error']}") |
| return get_mock_design() |
| |
| content = resp_json['choices'][0]['message']['content'] |
| |
| if not content: |
| print("[Design Error] No content in response") |
| return get_mock_design() |
| |
| print(f"[Design] Got response ({len(content)} chars)") |
| |
| import re |
| content = re.sub(r'^```[a-zA-Z]*\s*', '', content.strip()) |
| content = re.sub(r'```\s*$', '', content.strip()) |
| json_match = re.search(r'\{[\s\S]*\}', content) |
| |
| if not json_match: |
| print(f"[Design Error] No JSON found in response: {content[:200]}") |
| return get_mock_design() |
| |
| json_str = json_match.group(0) |
| try: |
| design_data = json.loads(json_str) |
| print("[Design] ✓ Successfully parsed design data") |
| return {"success": True, "data": design_data} |
| except json.JSONDecodeError as e: |
| print(f"[Design Error] Failed to parse JSON: {str(e)}") |
| repaired = repair_json(json_str) |
| if repaired: |
| try: |
| design_data = json.loads(repaired) |
| print("[Design] ✓ Successfully repaired and parsed JSON") |
| return {"success": True, "data": design_data} |
| except: |
| pass |
| return get_mock_design() |
| |
| except Exception as e: |
| print(f"[Design Fatal Error] {str(e)}") |
| return get_mock_design() |
|
|
|
|
| def repair_json(s): |
| """Attempt to repair malformed JSON""" |
| import re |
| |
| s = re.sub(r',(\s*[}\]])', r'\1', s) |
| |
| try: |
| json.loads(s) |
| return s |
| except: |
| return None |
|
|
|
|
| def get_mock_design(): |
| """Return mock design data when API fails""" |
| return { |
| "success": True, |
| "data": { |
| "room_type": "living room", |
| "design_description": "تصميم حديث وأنيق مع أثاث عملي ومريح يتناسب مع ذوقك", |
| "image_gen_prompt": "photorealistic modern interior design", |
| "furniture": [ |
| {"category": "أريكة", "name": "أريكة جلدية سوداء حديثة", "price": "9500", "furniture_type": "sofa"}, |
| {"category": "طاولة قهوة", "name": "طاولة خشبية أنيقة", "price": "4200", "furniture_type": "coffee table"}, |
| {"category": "إضاءة", "name": "مصباح أرضي ذهبي", "price": "1800", "furniture_type": "lamp"}, |
| {"category": "سجادة", "name": "سجادة فاخرة رمادية", "price": "2300", "furniture_type": "rug"} |
| ] |
| } |
| } |
|
|
| @app.post("/api/ingest") |
| async def ingest_property(prop: Property): |
| """ |
| Takes a scraped property and saves it into the Vector Database. |
| The description and location are automatically converted into AI Vectors. |
| """ |
| prop_id = str(uuid.uuid4()) |
| |
| |
| ai_context = f"{prop.title}. A {prop.type} located in {prop.location}. {prop.description}. Price: {prop.price}." |
| |
| collection.add( |
| documents=[ai_context], |
| metadatas=[prop.dict()], |
| ids=[prop_id] |
| ) |
| |
| return {"message": "Property ingested into AI Vector Database successfully", "id": prop_id} |
|
|
| @app.post("/api/interact") |
| async def track_interaction(interaction: Interaction): |
| """ |
| Logs user interactions (like/view) into the SQLite database. |
| """ |
| try: |
| log_interaction(interaction.user_id, interaction.property_id, interaction.interaction_type) |
| return {"message": "Interaction logged"} |
| except Exception as e: |
| print(f"[!] DB Error logging interaction: {e}") |
| return {"error": "Failed to log interaction"} |
|
|
| @app.post("/api/recommend") |
| async def recommend_properties(query: UserQuery): |
| """ |
| Takes the user's answers from the Onboarding Quiz, converts them into a Vector, |
| and searches the Vector Database for the closest Semantic Matches. |
| Also injects Collaborative Filtering if the user has history. |
| """ |
| if collection.count() == 0: |
| |
| ingest_dummy_data() |
| |
| |
| search_text = f"I am looking for a {query.property_type} in {query.location} for {query.goal}. My budget is {query.budget}." |
| |
| |
| results = collection.query( |
| query_texts=[search_text], |
| n_results=10 |
| ) |
| |
| |
| semantic_matches = [] |
| if results['metadatas']: |
| for i, meta in enumerate(results['metadatas'][0]): |
| distance = results['distances'][0][i] |
| match_score = max(50, int(100 - (distance * 30))) |
| meta['matchScore'] = match_score |
| |
| meta['property_id'] = results['ids'][0][i] |
| semantic_matches.append(meta) |
| |
| |
| collab_boosted = [] |
| collab_ids = [] |
| if query.user_id != "guest": |
| |
| collab_ids = get_collaborative_recommendations(query.user_id, all_properties=None, top_k=3) |
| print(f"[*] Collaborative matches for {query.user_id}: {collab_ids}") |
| |
| |
| for match in semantic_matches: |
| if match.get('property_id') in collab_ids: |
| match['matchScore'] = min(99, match['matchScore'] + 15) |
| match['isCollab'] = True |
| |
| |
| semantic_matches.sort(key=lambda x: x['matchScore'], reverse=True) |
| |
| |
| |
| |
| return {"recommendations": semantic_matches[:5]} |
|
|
|
|
| def ingest_dummy_data(): |
| """ Helper function to populate the Vector DB if it's empty """ |
| print("Database empty. Ingesting realistic Egyptian properties...") |
| dummy_properties = [ |
| Property(title="شقة فاخرة في تاج سيتي", type="Apartment", location="التجمع الخامس", price="٤٬٥٠٠٬٠٠٠", status="للبيع", description="شقة رائعة بالقرب من المدارس الدولية بمساحة 180 متر مربع. تشطيب سوبر لوكس.", lat=30.0682, lng=31.3653, image="https://images.unsplash.com/photo-1512917774080-9991f1c4c750?w=600"), |
| Property(title="فيلا مستقلة بكمبوند ميفيدا", type="Villa", location="التجمع الخامس", price="١٨٬٠٠٠٬٠٠٠", status="للبيع", description="فيلا مستقلة بحمام سباحة وحديقة خاصة في شارع التسعين. مساحة المبنى 400 متر والحديقة 200 متر.", lat=30.0125, lng=31.4552, image="https://images.unsplash.com/photo-1600596542815-ffad4c1539a9?w=600"), |
| Property(title="تاون هاوس بيفرلي هيلز", type="Townhouse", location="الشيخ زايد", price="٨٬٢٠٠٬٠٠٠", status="للبيع", description="تاون هاوس حديث بتشطيب الترا سوبر لوكس داخل كمبوند. مساحة 250 متر.", lat=30.0469, lng=30.9850, image="https://images.unsplash.com/photo-1600607687931-cebf5871f585?w=600"), |
| Property(title="شقة بمدينتي مجموعة B", type="Apartment", location="مدينتي", price="٣٬٢٠٠٬٠٠٠", status="للبيع", description="شقة مميزة بمدينتي تطل على الوايد جاردن مساحة 140 متر مربع، 3 غرف نوم.", lat=30.0934, lng=31.6222, image="https://images.unsplash.com/photo-1522708323590-d24dbb6b0267?w=600"), |
| Property(title="شاليه بقرية مراسي", type="Chalet", location="الساحل الشمالي", price="١٢٬٥٠٠٬٠٠٠", status="للبيع", description="شاليه يرى البحر مباشرة بقرية مراسي الساحل الشمالي مساحة 120 متر مع رووف خاص.", lat=30.8250, lng=28.9500, image="https://images.unsplash.com/photo-1499793983690-e29da59ef1c2?w=600"), |
| Property(title="مكتب إداري بالعاصمة", type="Commercial", location="العاصمة الإدارية", price="٢٬٨٠٠٬٠٠٠", status="للبيع", description="مكتب إداري في منطقة الأعمال المركزية بالعاصمة الإدارية بمساحة 60 متر، تشطيب كامل.", lat=30.0055, lng=31.7251, image="https://images.unsplash.com/photo-1497366216548-37526070297c?w=600"), |
| Property(title="شقة بكمبوند زد", type="Apartment", location="الشيخ زايد", price="٧٬٠٠٠٬٠٠٠", status="للبيع", description="شقة فاخرة جداً في أبراج زد الشيخ زايد، إطلالة على البارك، مساحة 160 متر.", lat=30.0469, lng=30.9850, image="https://images.unsplash.com/photo-1545324418-cc1a3fa10c00?w=600"), |
| Property(title="توين هاوس بالجونة", type="Townhouse", location="الجونة", price="١٥٬٠٠٠٬٠٠٠", status="للبيع", description="توين هاوس على اللاجون في الجونة بتشطيب كامل، جاهز للتسليم.", lat=27.3942, lng=33.6783, image="https://images.unsplash.com/photo-1564013799919-ab600027ffc6?w=600"), |
| Property(title="شقة للإيجار بالمعادي", type="Apartment", location="المعادي", price="٢٥٬٠٠٠", status="للإيجار", description="شقة مفروشة بالكامل تطل على النيل بالمعادي، غرفتين نوم.", lat=29.9538, lng=31.2585, image="https://images.unsplash.com/photo-1502672260266-1c1de2d9d0cb?w=600") |
| ] |
| for p in dummy_properties: |
| collection.add( |
| documents=[f"{p.title}. A {p.type} located in {p.location}. {p.description}. Price: {p.price}."], |
| metadatas=[p.dict()], |
| ids=[str(uuid.uuid4())] |
| ) |
|
|
| @app.post("/api/scrape") |
| async def scrape_and_ingest(background_tasks: BackgroundTasks): |
| """ |
| Triggers live scraping from all 3 sources and ingests results into ChromaDB. |
| """ |
| def _run(): |
| properties = scrape_all() |
| for p in properties: |
| try: |
| collection.add( |
| documents=[f"{p['title']}. A {p['type']} located in {p['location']}. {p['description']}. Price: {p['price']}."], |
| metadatas=[p], |
| ids=[str(uuid.uuid4())] |
| ) |
| except Exception as e: |
| print(f"[ingest error] {e}") |
| print(f"[+] Ingested {len(properties)} live properties into ChromaDB") |
| background_tasks.add_task(_run) |
| return {"message": "Scraping started in background"} |
|
|
|
|
| @app.get("/api/search") |
| async def semantic_search(q: str): |
| """ |
| Intelligent semantic search using ChromaDB. |
| """ |
| try: |
| results = collection.query( |
| query_texts=[q], |
| n_results=20 |
| ) |
| |
| matches = [] |
| if results['metadatas']: |
| for i, meta in enumerate(results['metadatas'][0]): |
| distance = results['distances'][0][i] |
| match_score = max(50, int(100 - (distance * 30))) |
| meta['matchScore'] = match_score |
| matches.append(meta) |
| |
| matches.sort(key=lambda x: x['matchScore'], reverse=True) |
| return {"properties": matches} |
| except Exception as e: |
| print(f"[Search Error] {e}") |
| return {"properties": []} |
|
|
|
|
| @app.get("/api/properties") |
| async def get_all_properties(limit: int = 50, offset: int = 0): |
| count = collection.count() |
| if count == 0: |
| ingest_dummy_data() |
| results = collection.get(limit=offset + limit, include=["metadatas"]) |
| properties = [] |
| for meta in results["metadatas"][offset:]: |
| if not meta.get("matchScore"): |
| meta["matchScore"] = 75 |
| properties.append(meta) |
| return {"properties": properties, "total": len(properties)} |
|
|
|
|
| |
|
|
| import os |
| |
| frontend_path = os.path.join(os.path.dirname(__file__), "../front") |
| app.mount("/", StaticFiles(directory=frontend_path, html=True), name="front") |
|
|