|
from fastapi import FastAPI, HTTPException |
|
from pydantic import BaseModel |
|
from sentence_transformers import SentenceTransformer, util |
|
from transformers import pipeline |
|
from typing import List |
|
import numpy as np |
|
|
|
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) |
|
|
|
summarizer = pipeline("summarization", model="facebook/bart-large-cnn") |
|
|
|
|
|
class ModifyQueryRequest(BaseModel): |
|
query_string: str |
|
|
|
class ModifyQueryRequest_v3(BaseModel): |
|
query_string_list: List[str] |
|
|
|
class AnswerQuestionRequest(BaseModel): |
|
question: str |
|
context: List[str] |
|
locations: List[str] |
|
|
|
class T5QuestionRequest(BaseModel): |
|
context: str |
|
|
|
|
|
class ModifyQueryResponse(BaseModel): |
|
embeddings: List[List[float]] |
|
|
|
class AnswerQuestionResponse(BaseModel): |
|
answer: str |
|
locations: List[str] |
|
|
|
class T5Response(BaseModel): |
|
answer: str |
|
|
|
|
|
@app.post("/modify_query", response_model=ModifyQueryResponse) |
|
async def modify_query(request: ModifyQueryRequest): |
|
try: |
|
|
|
embeddings = model.encode([request.query_string]) |
|
return ModifyQueryResponse(embeddings=[emb.tolist() for emb in embeddings]) |
|
except Exception as e: |
|
raise HTTPException(status_code=500, detail=f"Error in modifying query: {str(e)}") |
|
|
|
@app.post("/modify_query_v3", response_model=ModifyQueryResponse) |
|
async def modify_query_v3(request: ModifyQueryRequest_v3): |
|
try: |
|
|
|
embeddings = model.encode(request.query_string_list) |
|
return ModifyQueryResponse(embeddings=[emb.tolist() for emb in embeddings]) |
|
except Exception as e: |
|
raise HTTPException(status_code=500, detail=f"Error in modifying query v3: {str(e)}") |
|
|
|
@app.post("/answer_question", response_model=AnswerQuestionResponse) |
|
async def answer_question(request: AnswerQuestionRequest): |
|
try: |
|
res_locs = [] |
|
context_string = '' |
|
corpus_embeddings = model.encode(request.context, convert_to_tensor=True) |
|
query_embeddings = model.encode(request.question, convert_to_tensor=True) |
|
hits = util.semantic_search(query_embeddings, corpus_embeddings) |
|
|
|
|
|
for hit in hits[0]: |
|
if hit['score'] > 0.4: |
|
loc = hit['corpus_id'] |
|
res_locs.append(request.locations[loc]) |
|
context_string += request.context[loc] + ' ' |
|
|
|
|
|
if not res_locs: |
|
answer = "Sorry, I couldn't find any results for your query. Please try again!" |
|
else: |
|
|
|
QA_input = { |
|
'question': request.question, |
|
'context': context_string.replace('\n', ' ') |
|
} |
|
result = nlp(QA_input) |
|
answer = result['answer'] |
|
|
|
return AnswerQuestionResponse(answer=answer, locations=res_locs) |
|
except Exception as e: |
|
raise HTTPException(status_code=500, detail=f"Error in answering question: {str(e)}") |
|
|
|
@app.post("/t5answer", response_model=T5Response) |
|
async def t5answer(request: T5QuestionRequest): |
|
try: |
|
|
|
response = summarizer(request.context, max_length=130, min_length=30, do_sample=False) |
|
return T5Response(answer=response[0]["summary_text"]) |
|
except Exception as e: |
|
raise HTTPException(status_code=500, detail=f"Error in T5 summarization: {str(e)}") |
|
|
|
if __name__ == "__main__": |
|
import uvicorn |
|
uvicorn.run(app, host="0.0.0.0", port=8000) |
|
|
|
|
|
|