Views
No views yet
1pip install sgnlp
21from sgnlp.models.sentic_gcn import(
2 SenticGCNBertConfig,
3 SenticGCNBertModel,
4 SenticGCNBertEmbeddingConfig,
5 SenticGCNBertEmbeddingModel,
6 SenticGCNBertTokenizer,
7 SenticGCNBertPreprocessor,
8 SenticGCNBertPostprocessor
9)
10
11tokenizer = SenticGCNBertTokenizer.from_pretrained("bert-base-uncased")
12
13# Load Model
14config = SenticGCNBertConfig.from_pretrained("./senticgcn_bert/config.json")
15model = SenticGCNBertModel.from_pretrained("./senticgcn_bert/pytorch_model.bin",config=config)
16
17# Load Embedding Model
18embed_config = SenticGCNBertEmbeddingConfig.from_pretrained("bert-base-uncased")
19embed_model = SenticGCNBertEmbeddingModel.from_pretrained("bert-base-uncased", config=embed_config)
20
21preprocessor = SenticGCNBertPreprocessor(
22 tokenizer=tokenizer, embedding_model=embed_model,
23 senticnet="./senticgcn_bert/senticnet.pickle",
24 device="cpu")
25
26postprocessor = SenticGCNBertPostprocessor()
27
28inputs = [
29 { # Single word aspect
30 "aspects": ["service"],
31 "sentence": "To sum it up : service varies from good to mediorce , \
32 depending on which waiter you get ; generally it is just average ok .",
33 },
34 { # Single-word, multiple aspects
35 "aspects": ["service", "decor"],
36 "sentence": "Everything is always cooked to perfection , the service \
37 is excellent, the decor cool and understated.",
38 },
39 { # Multi-word aspect
40 "aspects": ["grilled chicken", "chicken"],
41 "sentence": "the only chicken i moderately enjoyed was their grilled chicken \
42 special with edamame puree .",
43 },
44]
45
46processed_inputs, processed_indices = preprocessor(inputs)
47raw_outputs = model(processed_indices)
48
49post_outputs = postprocessor(processed_inputs=processed_inputs, model_outputs=raw_outputs)
50
51print(post_outputs[0])
52# {'sentence': ['To', 'sum', 'it', 'up', ':', 'service', 'varies', 'from', 'good', 'to', 'mediorce', ',', 'depending', 'on', 'which'
53# 'waiter', 'you', 'get', ';', 'generally', 'it', 'is', 'just', 'average', 'ok', '.'],
54# 'aspects': [[5]],
55# 'labels': [0]}
56
57print(post_outputs[1])
58# {'sentence': ['Everything', 'is', 'always', 'cooked', 'to', 'perfection', ',', 'the', 'service',
59# 'is', 'excellent,', 'the', 'decor', 'cool', 'and', 'understated.'],
60# 'aspects': [[8], [12]],
61# 'labels': [1, 1]}
62
63print(post_outputs[2])
64# {'sentence': ['the', 'only', 'chicken', 'i', 'moderately', 'enjoyed', 'was', 'their', 'grilled',
65# 'chicken', 'special', 'with', 'edamame', 'puree', '.'],
66# 'aspects': [[8, 9], [2], [9]],
67# 'labels': [1, 1, 1]}
68
69