|
from fastapi import FastAPI, HTTPException |
|
from pydantic import BaseModel |
|
from sentence_transformers import SentenceTransformer |
|
from transformers import pipeline |
|
|
|
|
|
app = FastAPI() |
|
|
|
|
|
model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2") |
|
question_model = "deepset/tinyroberta-squad2" |
|
nlp = pipeline('question-answering', model=question_model, tokenizer=question_model) |
|
|
|
|
|
class ModifyQueryRequest(BaseModel): |
|
query_string: str |
|
|
|
class AnswerQuestionRequest(BaseModel): |
|
question: str |
|
context: str |
|
|
|
|
|
class ModifyQueryResponse(BaseModel): |
|
embeddings: list |
|
|
|
class AnswerQuestionResponse(BaseModel): |
|
answer: str |
|
|
|
|
|
@app.post("/modify_query", response_model=ModifyQueryResponse) |
|
async def modify_query(request: ModifyQueryRequest): |
|
try: |
|
binary_embeddings = model.encode([request.query_string], precision="binary") |
|
return ModifyQueryResponse(embeddings=binary_embeddings[0].tolist()) |
|
except Exception as e: |
|
raise HTTPException(status_code=500, detail=str(e)) |
|
|
|
@app.post("/answer_question", response_model=AnswerQuestionResponse) |
|
async def answer_question(request: AnswerQuestionRequest): |
|
try: |
|
QA_input = { |
|
'question': request.question, |
|
'context': request.context |
|
} |
|
result = nlp(QA_input) |
|
return AnswerQuestionResponse(answer=result['answer']) |
|
except Exception as e: |
|
raise HTTPException(status_code=500, detail=str(e)) |
|
|
|
if __name__ == "__main__": |
|
import uvicorn |
|
uvicorn.run(app, host="0.0.0.0", port=8000) |
|
|