chen666-666 commited on
Commit
b99a17e
·
verified ·
1 Parent(s): 51333c9

Upload 2 files

Browse files
Files changed (1) hide show
  1. mcp_use.py +105 -0
mcp_use.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sys
2
+ import os
3
+ sys.path.append(os.path.join(os.path.dirname(__file__), 'relation-extraction-master'))
4
+ import re
5
+ import torch
6
+ from gqlalchemy import Memgraph
7
+ from relation_extraction.hparams import hparams
8
+ from relation_extraction.model import SentenceRE
9
+ from relation_extraction.data_utils import MyTokenizer, get_idx2tag, convert_pos_to_mask
10
+
11
+ # 云端Memgraph连接参数
12
+ MEMGRAPH_HOST = '18.159.132.161'
13
+ MEMGRAPH_PORT = 7687
14
+ MEMGRAPH_USERNAME = '[email protected]'
15
+ MEMGRAPH_PASSWORD = '159951Tjk.' # 请替换为你的真实密码
16
+ MEMGRAPH_ENCRYPTED = True
17
+
18
+ # 连接memgraph云数据库
19
+ def get_memgraph_conn():
20
+ return Memgraph(
21
+ MEMGRAPH_HOST,
22
+ MEMGRAPH_PORT,
23
+ MEMGRAPH_USERNAME,
24
+ MEMGRAPH_PASSWORD,
25
+ encrypted=MEMGRAPH_ENCRYPTED
26
+ )
27
+
28
+ # 单句预测,返回三元组
29
+ class RelationPredictor:
30
+ def __init__(self, hparams):
31
+ self.device = hparams.device
32
+ torch.manual_seed(hparams.seed)
33
+ self.idx2tag = get_idx2tag(hparams.tagset_file)
34
+ hparams.tagset_size = len(self.idx2tag)
35
+ self.model = SentenceRE(hparams).to(self.device)
36
+ self.model.load_state_dict(torch.load(hparams.model_file))
37
+ self.model.eval()
38
+ self.tokenizer = MyTokenizer(hparams.pretrained_model_path)
39
+
40
+ def predict_one(self, text, entity1, entity2):
41
+ match_obj1 = re.search(entity1, text)
42
+ match_obj2 = re.search(entity2, text)
43
+ if not (match_obj1 and match_obj2):
44
+ return None
45
+ e1_pos = match_obj1.span()
46
+ e2_pos = match_obj2.span()
47
+ item = {
48
+ 'h': {'name': entity1, 'pos': e1_pos},
49
+ 't': {'name': entity2, 'pos': e2_pos},
50
+ 'text': text
51
+ }
52
+ tokens, pos_e1, pos_e2 = self.tokenizer.tokenize(item)
53
+ encoded = self.tokenizer.bert_tokenizer.batch_encode_plus([(tokens, None)], return_tensors='pt')
54
+ input_ids = encoded['input_ids'].to(self.device)
55
+ token_type_ids = encoded['token_type_ids'].to(self.device)
56
+ attention_mask = encoded['attention_mask'].to(self.device)
57
+ e1_mask = torch.tensor([convert_pos_to_mask(pos_e1, max_len=attention_mask.shape[1])]).to(self.device)
58
+ e2_mask = torch.tensor([convert_pos_to_mask(pos_e2, max_len=attention_mask.shape[1])]).to(self.device)
59
+ with torch.no_grad():
60
+ logits = self.model(input_ids, token_type_ids, attention_mask, e1_mask, e2_mask)[0]
61
+ logits = logits.to(torch.device('cpu'))
62
+ relation = self.idx2tag[logits.argmax(0).item()]
63
+ return entity1, relation, entity2
64
+
65
+ # 写入memgraph
66
+ def insert_to_memgraph(memgraph, entity1, relation, entity2):
67
+ memgraph.execute(
68
+ "MERGE (a:Entity {name: $name1})",
69
+ {"name1": entity1}
70
+ )
71
+ memgraph.execute(
72
+ "MERGE (b:Entity {name: $name2})",
73
+ {"name2": entity2}
74
+ )
75
+ memgraph.execute(
76
+ f"MATCH (a:Entity {{name: $name1}}), (b:Entity {{name: $name2}}) MERGE (a)-[:{relation}]->(b)",
77
+ {"name1": entity1, "name2": entity2}
78
+ )
79
+
80
+ # 主流程
81
+
82
+ def main():
83
+ memgraph = get_memgraph_conn()
84
+ predictor = RelationPredictor(hparams)
85
+ print("请输入句子和两个实体,识别关系并写入Memgraph。输入exit退出。")
86
+ while True:
87
+ text = input("输入中文句子:")
88
+ if text.strip().lower() == 'exit':
89
+ break
90
+ entity1 = input("句子中的实体1:")
91
+ if entity1.strip().lower() == 'exit':
92
+ break
93
+ entity2 = input("句子中的实体2:")
94
+ if entity2.strip().lower() == 'exit':
95
+ break
96
+ result = predictor.predict_one(text, entity1, entity2)
97
+ if result is None:
98
+ print("实体未在句子中找到,请重试。")
99
+ continue
100
+ entity1, relation, entity2 = result
101
+ insert_to_memgraph(memgraph, entity1, relation, entity2)
102
+ print(f"已写入Memgraph:({entity1})-[:{relation}]->({entity2})")
103
+
104
+ if __name__ == '__main__':
105
+ main()