File size: 1,356 Bytes
4000467
ae9ab04
e5e27ac
4000467
 
 
 
 
 
 
 
ce686de
4000467
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fc24d64
4000467
ae9ab04
fc24d64
 
 
4000467
 
 
 
 
 
 
 
 
 
 
 
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from fastapi.middleware.cors import CORSMiddleware
from fastapi import FastAPI,Request

from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from transformer_qa_decode import TransformerQADecode
from transformers import AutoTokenizer, AutoModelForQuestionAnswering
from pydantic import BaseModel

tokenizer = AutoTokenizer.from_pretrained("deepset/roberta-base-squad2")
model = AutoModelForQuestionAnswering.from_pretrained("deepset/roberta-base-squad2")
qahl = TransformerQADecode(model=model, tokenizer=tokenizer, is_squad_v2=True)

app = FastAPI()
app.mount("/static", StaticFiles(directory="react-qa/build/static"), name="static")

origins = ["*"]

app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

class QAItem(BaseModel):
    question:str
    context:str
   
# https://hf.space/embed/{user}/{space}

@app.get("/")
def read_root():
    html_content = open('react-qa/build/index.html','r').read()
    return HTMLResponse(content=html_content,status_code=200)

@app.post("/question-answer")
def read_item(item:QAItem):
    result = qahl(item.question, item.context)

    # convert to dict
    for r in result:
        for i,x in enumerate(r):
            x_dict = x._asdict()
            r[i] = x_dict
    return result