""" Real Estate Description Formatter API Uses lightweight AI models from HuggingFace to format real estate descriptions """ from fastapi import FastAPI, HTTPException from fastapi.middleware.cors import CORSMiddleware from pydantic import BaseModel from typing import Optional import os from huggingface_hub import InferenceClient # FastAPI app app = FastAPI( title="Real Estate Description Formatter", description="API to format real estate descriptions with AI", version="1.0.0" ) # CORS app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # Initialize HuggingFace Inference Client # Sử dụng model mạnh: Qwen2.5-7B-Instruct (7B params, hỗ trợ tiếng Việt) # Alternatives: mistralai/Mistral-7B-Instruct-v0.3, google/gemma-2-7b-it MODEL_NAME = os.getenv("MODEL_NAME", "Qwen/Qwen2.5-7B-Instruct") HF_TOKEN = os.getenv("HF_TOKEN", None) # Optional: for private models or faster inference client = InferenceClient(model=MODEL_NAME, token=HF_TOKEN) # Request/Response Models class RealEstateInput(BaseModel): description: str class RealEstateOutput(BaseModel): original: str formatted_html: str success: bool error: Optional[str] = None # Prompt phức tạp để xử lý form description bất động sản SYSTEM_PROMPT = """Bạn là một chuyên gia định dạng nội dung bất động sản chuyên nghiệp. NHIỆM VỤ: Chuyển đổi mô tả bất động sản "xấu" (không có cấu trúc, viết tắt, thiếu dấu câu) thành HTML được format đẹp mắt với CSS inline. QUY TẮC QUAN TRỌNG: 1. KHÔNG ĐƯỢC thay đổi nội dung text gốc - CHỈ thêm HTML tags và CSS styling 2. PHẢI giữ nguyên tất cả thông tin: giá, diện tích, số phòng, địa chỉ, số điện thoại 3. PHẢI phân tích và nhận diện các thành phần: - Tiêu đề (tên BDS, loại nhà) - Thông tin kỹ thuật (diện tích, hướng, pháp lý) - Mô tả chi tiết (kết cấu, nội thất) - Vị trí (địa chỉ, khu vực, tiện ích xung quanh) - Giá bán - Liên hệ (SĐT, tên người bán) - ... các thành phần khác 4. SỬ DỤNG CẤU TRÚC HTML: -