Spaces:
Sleeping
Sleeping
File size: 1,209 Bytes
02e4173 3e3298f 02e4173 3e3298f 02e4173 3e3298f 02e4173 3e3298f 02e4173 3e3298f 02e4173 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
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}
|