File size: 729 Bytes
ecd2888
 
 
4d62107
 
 
 
 
 
87c5b9b
b2f7101
87c5b9b
4d62107
ecd2888
 
 
 
4d62107
 
 
f23af25
 
4d62107
 
 
 
 
 
 
 
 
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
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)