Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -26,10 +26,10 @@ from modeling import (
|
|
| 26 |
from run_classifier_dataset_utils import processors, output_modes, compute_metrics
|
| 27 |
from data_loader import load_train_data, load_train_data_kf, load_test_data, load_sentence_data
|
| 28 |
|
| 29 |
-
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
|
| 34 |
CONFIG_NAME = "config.json"
|
| 35 |
WEIGHTS_NAME = "pytorch_model.bin"
|
|
@@ -112,14 +112,12 @@ def main():
|
|
| 112 |
|
| 113 |
#tokenizer.tokenize('the debate has sharpened.')
|
| 114 |
|
| 115 |
-
def run_one_sentence(sentence):
|
| 116 |
-
|
| 117 |
-
sentence = re.sub(r'([.,!?()-]+)', r' \1 ', sentence)
|
| 118 |
sentence = ' '.join(sentence.split())
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
#print(result)
|
| 123 |
|
| 124 |
model.eval()
|
| 125 |
s_batch = load_sentence_data(args, sentence, ['0','1'], tokenizer, 'classification')
|
|
@@ -152,21 +150,44 @@ def main():
|
|
| 152 |
|
| 153 |
pred = logits.detach().cpu().numpy()
|
| 154 |
pred = np.argmax(pred, axis=1)
|
| 155 |
-
|
|
|
|
| 156 |
for i,n in enumerate(idx):
|
| 157 |
pred_list[n] = 'M' if pred[i] == 1 else None
|
| 158 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
label_list = [(w, p) for w,p in zip(sentence.split(), pred_list)]
|
| 160 |
-
print(label_list)
|
| 161 |
-
return label_list,
|
| 162 |
-
#import pdb; pdb.set_trace()
|
| 163 |
|
| 164 |
demo = gr.Interface(
|
| 165 |
run_one_sentence,
|
|
|
|
| 166 |
gr.Textbox(placeholder="Enter sentence here..."),
|
| 167 |
-
|
|
|
|
|
|
|
| 168 |
examples=[
|
| 169 |
['while new departments are born and others extended .'],
|
|
|
|
|
|
|
| 170 |
['The sounds are the same as those of daylight , yet somehow the night magnifies and sharpens the creak of a yielding block , the sigh of air over a shroud , the stretching of a sail , the hiss of water sliding sleek against the hull , the curl of a quarter-wave falling away , and the thump as a wave strikes the cutwater to be sheared into two bright slices of whiteness .'],
|
| 171 |
['and finally, the debate has sharpened.']
|
| 172 |
]
|
|
|
|
| 26 |
from run_classifier_dataset_utils import processors, output_modes, compute_metrics
|
| 27 |
from data_loader import load_train_data, load_train_data_kf, load_test_data, load_sentence_data
|
| 28 |
|
| 29 |
+
from frame_semantic_transformer import FrameSemanticTransformer
|
| 30 |
|
| 31 |
+
frame_transformer = FrameSemanticTransformer()
|
| 32 |
+
frame_transformer.setup()
|
| 33 |
|
| 34 |
CONFIG_NAME = "config.json"
|
| 35 |
WEIGHTS_NAME = "pytorch_model.bin"
|
|
|
|
| 112 |
|
| 113 |
#tokenizer.tokenize('the debate has sharpened.')
|
| 114 |
|
| 115 |
+
def run_one_sentence(sentence, test_frame):
|
| 116 |
+
sentence = re.sub(r'([^\w\s])', r' \1 ', sentence)
|
|
|
|
| 117 |
sentence = ' '.join(sentence.split())
|
| 118 |
+
frame_sentence = re.sub(r'\s+([^\w\s])', r'\1', sentence)
|
| 119 |
+
print('sentence1:', sentence)
|
| 120 |
+
print('sentence2:', frame_sentence)
|
|
|
|
| 121 |
|
| 122 |
model.eval()
|
| 123 |
s_batch = load_sentence_data(args, sentence, ['0','1'], tokenizer, 'classification')
|
|
|
|
| 150 |
|
| 151 |
pred = logits.detach().cpu().numpy()
|
| 152 |
pred = np.argmax(pred, axis=1)
|
| 153 |
+
sentence_list = sentence.split()
|
| 154 |
+
pred_list = [None for _ in range(len(sentence_list))]
|
| 155 |
for i,n in enumerate(idx):
|
| 156 |
pred_list[n] = 'M' if pred[i] == 1 else None
|
| 157 |
+
frame_list = []
|
| 158 |
+
if test_frame:
|
| 159 |
+
sentence_frame = frame_transformer.detect_frames(frame_sentence)
|
| 160 |
+
for i,n in enumerate(idx):
|
| 161 |
+
if pred[i] == 1:
|
| 162 |
+
word_loc = frame_sentence.find(sentence_list[n])
|
| 163 |
+
word_frame = frame_transformer.detect_frames(sentence_list[n])
|
| 164 |
+
if word_loc in sentence_frame.trigger_locations and 0 in word_frame.trigger_locations:
|
| 165 |
+
frame_list = frame_list + [
|
| 166 |
+
('['+sentence_list[n]+']', None),
|
| 167 |
+
(sentence_frame.frames[sentence_frame.trigger_locations.index(word_loc)].name, 'Contextual'),
|
| 168 |
+
(word_frame.frames[0].name, 'Literal'),
|
| 169 |
+
(' \n', None)
|
| 170 |
+
]
|
| 171 |
+
else:
|
| 172 |
+
frame_list = frame_list + [
|
| 173 |
+
('['+sentence_list[n]+']', None),
|
| 174 |
+
]
|
| 175 |
+
|
| 176 |
label_list = [(w, p) for w,p in zip(sentence.split(), pred_list)]
|
| 177 |
+
#print(label_list)
|
| 178 |
+
return label_list, frame_list
|
|
|
|
| 179 |
|
| 180 |
demo = gr.Interface(
|
| 181 |
run_one_sentence,
|
| 182 |
+
[
|
| 183 |
gr.Textbox(placeholder="Enter sentence here..."),
|
| 184 |
+
gr.Checkbox(label="Test frame", value=False),
|
| 185 |
+
],
|
| 186 |
+
[gr.HighlightedText(label='Metaphor Detection'), gr.HighlightedText(label='Frame Extraction')],
|
| 187 |
examples=[
|
| 188 |
['while new departments are born and others extended .'],
|
| 189 |
+
['Dimples played in his cheeks .'],
|
| 190 |
+
['For a whole week they had worked closely together , sharing flasks of coffee and packets of cigarettes and Paula had grown to like the pixieish little man who by his very nature offered her no challenge — and no threat .'],
|
| 191 |
['The sounds are the same as those of daylight , yet somehow the night magnifies and sharpens the creak of a yielding block , the sigh of air over a shroud , the stretching of a sail , the hiss of water sliding sleek against the hull , the curl of a quarter-wave falling away , and the thump as a wave strikes the cutwater to be sheared into two bright slices of whiteness .'],
|
| 192 |
['and finally, the debate has sharpened.']
|
| 193 |
]
|