sonIA / utils.py
Davide Fiocco
Refactor
46152dd
raw
history blame
1.2 kB
import datetime
import json
import streamlit as st
import tokenizers
import torch
from transformers import Pipeline, pipeline
def get_answer(input, context, engine):
answer = engine({"question": input, "context": context})
return answer["answer"]
@st.cache
def get_context():
BIRTHYEAR = 1952
OTHERBIRTHYEAR = 1984
now = datetime.datetime.now()
with open("context.json") as f:
context = (
json.load(f)["info"]
.replace("[YEAR]", str(now.year))
.replace("[BIRTHYEAR]", str(BIRTHYEAR))
.replace("[AGE]", str(now.year - BIRTHYEAR))
.replace("[OTHERAGE]", str(now.year - OTHERBIRTHYEAR))
)
return context
@st.cache(
hash_funcs={
torch.nn.parameter.Parameter: lambda _: None,
tokenizers.Tokenizer: lambda _: None,
tokenizers.AddedToken: lambda _: None,
},
allow_output_mutation=True,
show_spinner=False,
)
def load_engine() -> Pipeline:
nlp_qa = pipeline(
"question-answering",
model="mrm8488/bert-italian-finedtuned-squadv1-it-alfa",
tokenizer="mrm8488/bert-italian-finedtuned-squadv1-it-alfa",
)
return nlp_qa