ltk / app.py
ltk242111's picture
Update app.py
4d62107 verified
raw
history blame contribute delete
729 Bytes
from fastapi import FastAPI
from pydantic import BaseModel
from transformers import pipeline
import uvicorn
# Load model (small QA model to fit free tier)
qa_pipeline = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad")
# Define request format
class QARequest(BaseModel):
question: str
context: str
# Initialize FastAPI
app = FastAPI()
@app.get("/")
def home():
return {"message": "Chatbot API is running πŸš€"}
@app.post("/chat")
def chat(request: QARequest):
result = qa_pipeline(question=request.question, context=request.context)
return {"answer": result["answer"], "score": result["score"]}
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=7860)