from fastapi import FastAPI from fastapi.responses import HTMLResponse from fastapi.staticfiles import StaticFiles from fastapi.middleware.cors import CORSMiddleware from pydantic import BaseModel from pathlib import Path app = FastAPI() # CORS – allow embedding and external calls if needed app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"], ) # FIX: Use absolute path for Hugging Face Spaces STATIC_DIR = Path(__file__).parent / "static" app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static") class ChatRequest(BaseModel): message: str @app.get("/", response_class=HTMLResponse) def serve_html(): index_path = STATIC_DIR / "index.html" return index_path.read_text(encoding="utf-8") @app.post("/chat") def chat_endpoint(req: ChatRequest): user_message = req.message.strip() if not user_message: reply = "من فضلك اكتب رسالة للبدء." else: reply = ( "هذا رد مؤقت — سيتم دمج نموذج الذكاء الاصطناعي لاحقًا.\n\n" f"لقد كتبت: \"{user_message}\"" ) return {"response": reply}