Spaces:
Runtime error
Runtime error
Hanna Abi Akl
commited on
Commit
•
e0b928c
1
Parent(s):
9ffee63
Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,86 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
rom transformers import AutoTokenizer, AutoModelForSequenceClassification, AutoConfig
|
2 |
import gradio as gr
|
3 |
+
from torch.nn import functional as F
|
4 |
+
import seaborn
|
5 |
+
import matplotlib
|
6 |
+
import platform
|
7 |
+
from transformers.file_utils import ModelOutput
|
8 |
+
if platform.system() == "Darwin":
|
9 |
+
print("MacOS")
|
10 |
+
matplotlib.use('Agg')
|
11 |
+
import matplotlib.pyplot as plt
|
12 |
+
import io
|
13 |
+
from PIL import Image
|
14 |
+
import matplotlib.font_manager as fm
|
15 |
+
import util
|
16 |
+
# global var
|
17 |
+
MODEL_NAME = 'yseop/distilbert-base-financial-relation-extraction'
|
18 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
19 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
|
20 |
+
config = AutoConfig.from_pretrained(MODEL_NAME)
|
21 |
+
MODEL_BUF = {
|
22 |
+
"name": MODEL_NAME,
|
23 |
+
"tokenizer": tokenizer,
|
24 |
+
"model": model,
|
25 |
+
"config": config
|
26 |
+
}
|
27 |
+
font_dir = ['./']
|
28 |
+
for font in fm.findSystemFonts(font_dir):
|
29 |
+
print(font)
|
30 |
+
fm.fontManager.addfont(font)
|
31 |
+
plt.rcParams["font.family"] = 'NanumGothicCoding'
|
32 |
+
def visualize_attention(sent, attention_matrix, n_words=10):
|
33 |
+
def draw(data, x, y, ax):
|
34 |
+
seaborn.heatmap(data,
|
35 |
+
xticklabels=x, square=True, yticklabels=y, vmin=0.0, vmax=1.0,
|
36 |
+
cbar=False, ax=ax)
|
37 |
+
|
38 |
+
# make plt figure with 1x6 subplots
|
39 |
+
fig = plt.figure(figsize=(16, 8))
|
40 |
+
# fig.subplots_adjust(hspace=0.7, wspace=0.2)
|
41 |
+
for i, layer in enumerate(range(1, 12, 2)):
|
42 |
+
ax = fig.add_subplot(2, 3, i+1)
|
43 |
+
ax.set_title("Layer {}".format(layer))
|
44 |
+
draw(attention_matrix[layer], sent if layer > 6 else [], sent if layer in [1,7] else [], ax=ax)
|
45 |
+
|
46 |
+
fig.tight_layout()
|
47 |
+
plt.close()
|
48 |
+
return fig
|
49 |
+
def change_model_name(name):
|
50 |
+
MODEL_BUF["name"] = name
|
51 |
+
MODEL_BUF["tokenizer"] = AutoTokenizer.from_pretrained(name)
|
52 |
+
MODEL_BUF["model"] = AutoModelForSequenceClassification.from_pretrained(name)
|
53 |
+
MODEL_BUF["config"] = AutoConfig.from_pretrained(name)
|
54 |
+
def predict(model_name, text):
|
55 |
+
if model_name != MODEL_NAME:
|
56 |
+
change_model_name(model_name)
|
57 |
+
|
58 |
+
tokenizer = MODEL_BUF["tokenizer"]
|
59 |
+
model = MODEL_BUF["model"]
|
60 |
+
config = MODEL_BUF["config"]
|
61 |
+
tokenized_text = tokenizer([text], return_tensors='pt')
|
62 |
+
input_tokens = tokenizer.convert_ids_to_tokens(tokenized_text.input_ids[0])
|
63 |
+
input_tokens = util.bytetokens_to_unicdode(input_tokens) if config.model_type in ['roberta', 'gpt', 'gpt2'] else input_tokens
|
64 |
+
model.eval()
|
65 |
+
output, attention = model(**tokenized_text, output_attentions=True, return_dict=False)
|
66 |
+
output = F.softmax(output, dim=-1)
|
67 |
+
result = {}
|
68 |
+
|
69 |
+
for idx, label in enumerate(output[0].detach().numpy()):
|
70 |
+
result[config.id2label[idx]] = float(label)
|
71 |
+
fig = visualize_attention(input_tokens, attention[0][0].detach().numpy())
|
72 |
+
return result, fig#.logits.detach()#.numpy()#, output.attentions.detach().numpy()
|
73 |
+
if __name__ == '__main__':
|
74 |
+
text = 'An A-B trust is a joint trust created by a married couple for the purpose of minimizing estate taxes.'
|
75 |
+
model_name_list = [
|
76 |
+
'yseop/distilbert-base-financial-relation-extraction'
|
77 |
+
]
|
78 |
+
#Create a gradio app with a button that calls predict()
|
79 |
+
app = gr.Interface(
|
80 |
+
fn=predict,
|
81 |
+
inputs=[gr.inputs.Dropdown(model_name_list, label="Model Name"), 'text'], outputs=['label', 'plot'],
|
82 |
+
examples = [[MODEL_BUF["name"], text]],
|
83 |
+
title="FReE",
|
84 |
+
description="Financial relations classifier"
|
85 |
+
)
|
86 |
+
app.launch(inline=False)
|