Spaces:
Sleeping
Sleeping
| import os | |
| from fastapi import FastAPI, Request | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from fastapi.staticfiles import StaticFiles | |
| from fastapi.responses import FileResponse | |
| from pydantic import BaseModel | |
| from src.chatbot import handle_query | |
| class ChatRequest(BaseModel): | |
| query: str | |
| class ChatResponse(BaseModel): | |
| answer: str | |
| cards: list = [] | |
| app = FastAPI(title="NoBrokerage Chatbot") | |
| # CORS middleware | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| # Mount static files (frontend) | |
| app.mount("/static", StaticFiles(directory="frontend"), name="static") | |
| def root(): | |
| """Serve the frontend HTML""" | |
| return FileResponse("frontend/index.html") | |
| def chat(request: ChatRequest): | |
| """Handle chat requests""" | |
| result = handle_query(request.query) | |
| return ChatResponse(answer=result.get("summary"), cards=result.get("cards", [])) | |
| def health(): | |
| """Health check endpoint""" | |
| return {"status": "ok", "message": "✅ NoBrokerage running"} | |
| # For Hugging Face Spaces | |
| if __name__ == "__main__": | |
| import uvicorn | |
| port = int(os.environ.get("PORT", 7860)) | |
| uvicorn.run(app, host="0.0.0.0", port=port) |