Spaces:
Sleeping
Sleeping
Subhakanta
commited on
Commit
·
8a65976
1
Parent(s):
c363d0e
Add Hugging Face entrypoint app file
Browse files
app.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from fastapi import FastAPI, Request
|
| 3 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
+
from fastapi.staticfiles import StaticFiles
|
| 5 |
+
from fastapi.responses import FileResponse
|
| 6 |
+
from pydantic import BaseModel
|
| 7 |
+
from src.chatbot import handle_query
|
| 8 |
+
|
| 9 |
+
class ChatRequest(BaseModel):
|
| 10 |
+
query: str
|
| 11 |
+
|
| 12 |
+
class ChatResponse(BaseModel):
|
| 13 |
+
answer: str
|
| 14 |
+
cards: list = []
|
| 15 |
+
|
| 16 |
+
app = FastAPI(title="NoBrokerage Chatbot")
|
| 17 |
+
|
| 18 |
+
# CORS middleware
|
| 19 |
+
app.add_middleware(
|
| 20 |
+
CORSMiddleware,
|
| 21 |
+
allow_origins=["*"],
|
| 22 |
+
allow_credentials=True,
|
| 23 |
+
allow_methods=["*"],
|
| 24 |
+
allow_headers=["*"],
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
# Mount static files (frontend)
|
| 28 |
+
app.mount("/static", StaticFiles(directory="frontend"), name="static")
|
| 29 |
+
|
| 30 |
+
@app.get("/")
|
| 31 |
+
def root():
|
| 32 |
+
"""Serve the frontend HTML"""
|
| 33 |
+
return FileResponse("frontend/index.html")
|
| 34 |
+
|
| 35 |
+
@app.post("/chat", response_model=ChatResponse)
|
| 36 |
+
def chat(request: ChatRequest):
|
| 37 |
+
"""Handle chat requests"""
|
| 38 |
+
result = handle_query(request.query)
|
| 39 |
+
return ChatResponse(answer=result.get("summary"), cards=result.get("cards", []))
|
| 40 |
+
|
| 41 |
+
@app.get("/health")
|
| 42 |
+
def health():
|
| 43 |
+
"""Health check endpoint"""
|
| 44 |
+
return {"status": "ok", "message": "✅ NoBrokerage running"}
|
| 45 |
+
|
| 46 |
+
# For Hugging Face Spaces
|
| 47 |
+
if __name__ == "__main__":
|
| 48 |
+
import uvicorn
|
| 49 |
+
port = int(os.environ.get("PORT", 7860))
|
| 50 |
+
uvicorn.run(app, host="0.0.0.0", port=port)
|