Spaces:
Runtime error
Runtime error
File size: 925 Bytes
5702872 |
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 |
#import libraries
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from transformers import pipeline
import uvicorn
# Initialize FastAPI app
app = FastAPI()
# Load the Hugging Face question-answering model
qa_pipeline = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad")
# Define request and response models
class ChatRequest(BaseModel):
question: str
context: str
class ChatResponse(BaseModel):
answer: str
# Define the /chat endpoint
@app.post("/chat", response_model=ChatResponse)
async def chat(request: ChatRequest):
try:
result = qa_pipeline(question=request.question, context=request.context)
return ChatResponse(answer=result['answer'])
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
# Run the FastAPI server
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)
|