Spaces:
Runtime error
Runtime error
Prathamesh Khade
commited on
Commit
·
26c4f72
1
Parent(s):
260b995
Deploy new version
Browse files- app/app.py +32 -0
app/app.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#import libraries
|
2 |
+
from fastapi import FastAPI, HTTPException
|
3 |
+
from pydantic import BaseModel
|
4 |
+
from transformers import pipeline
|
5 |
+
import uvicorn
|
6 |
+
|
7 |
+
# Initialize FastAPI app
|
8 |
+
app = FastAPI()
|
9 |
+
|
10 |
+
# Load the Hugging Face question-answering model
|
11 |
+
qa_pipeline = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad")
|
12 |
+
|
13 |
+
# Define request and response models
|
14 |
+
class ChatRequest(BaseModel):
|
15 |
+
question: str
|
16 |
+
context: str
|
17 |
+
|
18 |
+
class ChatResponse(BaseModel):
|
19 |
+
answer: str
|
20 |
+
|
21 |
+
# Define the /chat endpoint
|
22 |
+
@app.post("/chat", response_model=ChatResponse)
|
23 |
+
async def chat(request: ChatRequest):
|
24 |
+
try:
|
25 |
+
result = qa_pipeline(question=request.question, context=request.context)
|
26 |
+
return ChatResponse(answer=result['answer'])
|
27 |
+
except Exception as e:
|
28 |
+
raise HTTPException(status_code=500, detail=str(e))
|
29 |
+
|
30 |
+
# Run the FastAPI server
|
31 |
+
if __name__ == "__main__":
|
32 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|