Spaces:
Configuration error
Configuration error
Update app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869from fastapi.middleware.cors import CORSMiddlewarefrom fastapi import FastAPI, HTTPExceptionfrom pydantic import BaseModel, Fieldfrom fastapi.concurrency import run_in_threadpoolfrom contextlib import asynccontextmanagerfrom main import main# ========= Global RAG Chain =========rag_chain = None# ========= Lifespan (Startup Loader) =========@asynccontextmanagerasync def lifespan(app: FastAPI):global rag_chainprint("π Loading RAG model...")rag_chain = main()print("β RAG model loaded successfully!")yieldprint("π Shutting down...")# ========== Initialize FastAPI ==========app = FastAPI(title="Yashodeep's Personal Chatbot API",description="A RAG-based chatbot that answers questions about Yashodeep Hundiwale's skills, education, experience, etc.",version="1.0.0",lifespan=lifespan)# ========== Configure CORS ==========app.add_middleware(CORSMiddleware,allow_origins=["*"], # OK for now (later restrict)allow_credentials=True,allow_methods=["*"],allow_headers=["*"],)# ========== Request Schema ==========class QueryRequest(BaseModel):query: str = Field(...,description="The user's query to the chatbot.",examples=["What is Yashodeep's educational background?"])# ========== Routes ==========@app.get("/")def home():return {"message": "API is running π"}@app.get("/health")def health():return {"status": "ok"}@app.post("/chat")async def chat(query_request: QueryRequest):try:# Run blocking RAG in threadpool (IMPORTANT)response = await run_in_threadpool(rag_chain.invoke,{"input": query_request.query})return {"answer": response["answer"]}except Exception as e:raise HTTPException(status_code=500, detail=str(e))123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869from fastapi.middleware.cors import CORSMiddlewarefrom fastapi import FastAPI, HTTPExceptionfrom pydantic import BaseModel, Fieldfrom fastapi.concurrency import run_in_threadpoolfrom contextlib import asynccontextmanagerfrom main import main# ========= Global RAG Chain =========rag_chain = None# ========= Lifespan (Startup Loader) =========@asynccontextmanagerasync def lifespan(app: FastAPI):global rag_chainprint("π Loading RAG model...")rag_chain = main()print("β RAG model loaded successfully!")yieldprint("π Shutting down...")# ========== Initialize FastAPI ==========app = FastAPI(title="Yashodeep's Personal Chatbot API",description="A RAG-based chatbot that answers questions about Yashodeep Hundiwale's skills, education, experience, etc.",version="1.0.0",lifespan=lifespan)# ========== Configure CORS ==========app.add_middleware(CORSMiddleware,allow_origins=["*"], # OK for now (later restrict)allow_credentials=True,allow_methods=["*"],allow_headers=["*"],)# ========== Request Schema ==========class QueryRequest(BaseModel):query: str = Field(...,description="The user's query to the chatbot.",examples=["What is Yashodeep's educational background?"])# ========== Routes ==========@app.get("/")def home():return {"message": "API is running π, Use the /chat endpoint to ask questions about me"}@app.get("/health")def health():return {"status": "ok"}@app.post("/chat")async def chat(query_request: QueryRequest):try:# Run blocking RAG in threadpool (IMPORTANT)response = await run_in_threadpool(rag_chain.invoke,{"input": query_request.query})return {"answer": response["answer"]}except Exception as e:raise HTTPException(status_code=500, detail=str(e))
app.py+68-68