Spaces:
Runtime error
Runtime error
Hanna Abi Akl
commited on
Commit
•
79f1e27
1
Parent(s):
a13f335
Update app.py
Browse files
app.py
CHANGED
@@ -12,7 +12,7 @@ import matplotlib.pyplot as plt
|
|
12 |
import io
|
13 |
from PIL import Image
|
14 |
import matplotlib.font_manager as fm
|
15 |
-
|
16 |
# global var
|
17 |
MODEL_NAME = 'yseop/distilbert-base-financial-relation-extraction'
|
18 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
@@ -29,23 +29,7 @@ for font in fm.findSystemFonts(font_dir):
|
|
29 |
print(font)
|
30 |
fm.fontManager.addfont(font)
|
31 |
plt.rcParams["font.family"] = 'NanumGothicCoding'
|
32 |
-
|
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)
|
@@ -59,8 +43,6 @@ def predict(model_name, text):
|
|
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)
|
@@ -68,8 +50,7 @@ def predict(model_name, text):
|
|
68 |
|
69 |
for idx, label in enumerate(output[0].detach().numpy()):
|
70 |
result[config.id2label[idx]] = float(label)
|
71 |
-
|
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 = [
|
@@ -78,7 +59,7 @@ if __name__ == '__main__':
|
|
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'
|
82 |
examples = [[MODEL_BUF["name"], text]],
|
83 |
title="FReE",
|
84 |
description="Financial relations classifier"
|
|
|
12 |
import io
|
13 |
from PIL import Image
|
14 |
import matplotlib.font_manager as fm
|
15 |
+
|
16 |
# global var
|
17 |
MODEL_NAME = 'yseop/distilbert-base-financial-relation-extraction'
|
18 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
|
|
29 |
print(font)
|
30 |
fm.fontManager.addfont(font)
|
31 |
plt.rcParams["font.family"] = 'NanumGothicCoding'
|
32 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
def change_model_name(name):
|
34 |
MODEL_BUF["name"] = name
|
35 |
MODEL_BUF["tokenizer"] = AutoTokenizer.from_pretrained(name)
|
|
|
43 |
model = MODEL_BUF["model"]
|
44 |
config = MODEL_BUF["config"]
|
45 |
tokenized_text = tokenizer([text], return_tensors='pt')
|
|
|
|
|
46 |
model.eval()
|
47 |
output, attention = model(**tokenized_text, output_attentions=True, return_dict=False)
|
48 |
output = F.softmax(output, dim=-1)
|
|
|
50 |
|
51 |
for idx, label in enumerate(output[0].detach().numpy()):
|
52 |
result[config.id2label[idx]] = float(label)
|
53 |
+
return result
|
|
|
54 |
if __name__ == '__main__':
|
55 |
text = 'An A-B trust is a joint trust created by a married couple for the purpose of minimizing estate taxes.'
|
56 |
model_name_list = [
|
|
|
59 |
#Create a gradio app with a button that calls predict()
|
60 |
app = gr.Interface(
|
61 |
fn=predict,
|
62 |
+
inputs=[gr.inputs.Dropdown(model_name_list, label="Model Name"), 'text'], outputs=['label'],
|
63 |
examples = [[MODEL_BUF["name"], text]],
|
64 |
title="FReE",
|
65 |
description="Financial relations classifier"
|