forked from dice-group/dice-embeddings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.py
67 lines (58 loc) · 3.2 KB
/
deploy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from argparse import ArgumentParser
from core.static_funcs import load_model, intialize_model, random_prediction, deploy_relation_prediction, \
deploy_triple_prediction, deploy_tail_entity_prediction, deploy_head_entity_prediction
import gradio as gr
from core import KGE
def launch_kge(args, pre_trained_kge: KGE):
"""
Launch server
:param args:
:param pre_trained_kge:
:return:
"""
def predict(str_subject: str, str_predicate: str, str_object: str, random_examples: bool):
if random_examples:
return random_prediction(pre_trained_kge)
else:
if pre_trained_kge.is_seen(entity=str_subject) and pre_trained_kge.is_seen(
relation=str_predicate) and pre_trained_kge.is_seen(entity=str_object):
""" Triple Prediction """
return deploy_triple_prediction(pre_trained_kge, str_subject, str_predicate, str_object)
elif pre_trained_kge.is_seen(entity=str_subject) and pre_trained_kge.is_seen(
relation=str_predicate):
""" Tail Entity Prediction """
return deploy_tail_entity_prediction(pre_trained_kge, str_subject, str_predicate, args['top_k'])
elif pre_trained_kge.is_seen(entity=str_object) and pre_trained_kge.is_seen(
relation=str_predicate):
""" Head Entity Prediction """
return deploy_head_entity_prediction(pre_trained_kge, str_object, str_predicate, args['top_k'])
elif pre_trained_kge.is_seen(entity=str_subject) and pre_trained_kge.is_seen(entity=str_object):
""" Relation Prediction """
return deploy_relation_prediction(pre_trained_kge, str_subject, str_object, args['top_k'])
else:
KeyError('Uncovered scenario')
# If user simply select submit
return random_prediction(pre_trained_kge)
gr.Interface(
fn=predict,
inputs=[gr.inputs.Textbox(lines=1, placeholder=None, label='Subject'),
gr.inputs.Textbox(lines=1, placeholder=None, label='Predicate'),
gr.inputs.Textbox(lines=1, placeholder=None, label='Object'), "checkbox"],
outputs=[gr.outputs.Textbox(label='Input Triple'),
gr.outputs.Dataframe(label='Outputs')],
title=f'{pre_trained_kge.name} Deployment',
description='1. Enter a triple to compute its score,\n'
'2. Enter a subject and predicate pair to obtain most likely top ten entities or\n'
'3. Checked the random examples box and click submit').launch(share=args['share'])
def run(args: dict):
# (1) Train a knowledge graph embedding model
# (2) Give the path of serialized (1)
pre_trained_kge = KGE(path_of_pretrained_model_dir=args['path_of_experiment_folder'])
print(f'Done!\n')
launch_kge(args, pre_trained_kge)
if __name__ == '__main__':
parser = ArgumentParser()
parser.add_argument("--path_of_experiment_folder", type=str, default='Experiments/2022-06-03 08:58:22.552103')
parser.add_argument('--share', default=True, type=eval, choices=[True, False])
parser.add_argument('--top_k', default=10, type=int)
run(vars(parser.parse_args()))