File size: 1,420 Bytes
2cfe868 |
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 pydantic import BaseModel, validator
from peft import PeftModel, PeftConfig
from transformers import T5ForConditionalGeneration, AutoTokenizer
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI()
origins = ["*"]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"]
)
peft_model_id = "deutsche-welle/t5_large_peft_wnc_debiaser"
config = PeftConfig.from_pretrained(peft_model_id)
model = T5ForConditionalGeneration.from_pretrained(config.base_model_name_or_path)
tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path)
model = PeftModel.from_pretrained(model, peft_model_id)
model.eval()
def prepare_input(sentence: str):
input_ids = tokenizer(sentence, max_length=256, return_tensors="pt").input_ids
return input_ids
def inference(sentence: str) -> str:
input_data = prepare_input(sentence=sentence)
input_data = input_data.to(model.device)
outputs = model.generate(inputs=input_data, max_length=256)
result = tokenizer.decode(token_ids=outputs[0], skip_special_tokens=True)
return result
class Response(BaseModel):
generated_text: str
@app.get("/debias", response_model=Response)
def predict_subjectivity(sentence: str):
result = inference(f"debias: {sentence} </s>")
return {"generated_text": result} |