Finance / app.py
dmariko's picture
Update app.py
cec4eb1
raw
history blame
2.22 kB
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, AutoConfig
#from transformers import T5Tokenizer, T5ForConditionalGeneration, AutoConfig
import gradio as gr
from torch.nn import functional as F
import seaborn
import matplotlib
import platform
from transformers.file_utils import ModelOutput
if platform.system() == "Darwin":
print("MacOS")
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import io
from PIL import Image
import matplotlib.font_manager as fm
# global var
MODEL_NAME = 'https://huggingface.co/yseop/FNP_T5_D2T_complete'
#tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
#model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME)
tokenizer = T5Tokenizer.from_pretrained(MODEL_NAME)
model = T5ForConditionalGeneration.from_pretrained(MODEL_NAME)
config = AutoConfig.from_pretrained(MODEL_NAME)
MODEL_BUF = {
"name": MODEL_NAME,
"tokenizer": tokenizer,
"model": model,
"config": config
}
font_dir = ['./']
for font in fm.findSystemFonts(font_dir):
print(font)
fm.fontManager.addfont(font)
plt.rcParams["font.family"] = 'NanumGothicCoding'
def change_model_name(name):
MODEL_BUF["name"] = name
MODEL_BUF["tokenizer"] = AutoTokenizer.from_pretrained(name)
MODEL_BUF["model"] = AutoModelForSequenceClassification.from_pretrained(name)
MODEL_BUF["config"] = AutoConfig.from_pretrained(name)
def generate(text, model, tokenizer):
model.eval()
input_ids = tokenizer.encode("webNLG:{}".format(text), return_tensors="pt")
outputs = model.generate(input_ids, max_length=200, num_beams=2, repetition_penalty=2.5, top_k=50, top_p=0.98, length_penalty=1.0, early_stopping=True)
return tokenizer.decode(outputs[0])
if __name__ == '__main__':
text = 'Group profit | valIs | € 115.7 million && € 115.7 million | dTime | in 2019'
model_name_list = [
'yseop/distilbert-base-financial-relation-extraction'
]
app = gr.Interface(
fn=predict,
inputs=[gr.inputs.Dropdown(model_name_list, label="Model Name"), 'triples'], outputs=['text'],
examples = [[MODEL_BUF["name"], text]],
title="FReE",
description="Financial relations classifier"
)
app.launch(inline=False)