Spaces:
Runtime error
Runtime error
File size: 1,550 Bytes
6b3769c |
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 47 48 49 50 51 52 53 54 55 56 |
from fastapi import FastAPI
from pydantic import BaseModel
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles
import os
import traceback
from dotenv import load_dotenv
load_dotenv()
from Chatbot.bot import answer_query
app = FastAPI(title="Railway Assistant")
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
FRONTEND_DIR = os.path.join(BASE_DIR, "frontend")
class ChatRequest(BaseModel):
query: str
class ChatResponse(BaseModel):
answer: str
confidence: str | None = None
source: str | None = None
@app.post("/chat", response_model=ChatResponse)
async def chat(request: ChatRequest):
try:
answer = answer_query(request.query)
# Defensive fallback (never return None)
if not answer:
answer = "I couldn’t process that. Please try rephrasing your question."
except Exception:
traceback.print_exc()
answer = "Something went wrong while processing your request."
return {
"answer": answer,
"confidence": None,
"source": None
}
@app.get("/")
def serve_ui():
return FileResponse(os.path.join(FRONTEND_DIR, "index.html"))
# Serve static assets (CSS, JS, images)
app.mount(
"/static",
StaticFiles(directory=FRONTEND_DIR),
name="static"
)
|