File size: 2,500 Bytes
c0de02a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a0be2a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
68
69
70
71
72
73
74
75
76
77
import gradio as gr
import model_wrapper



model = model_wrapper.PredictionModel()

def pretty_print_opinion(opinion_dict):
    res = []
    maxlen = max([len(key) for key in opinion_dict.keys()]) + 2
    maxlen = 0
    for key, value in opinion_dict.items():
        if key == 'Polarity':
            res.append(f'{(key + ":").ljust(maxlen)} {value}')
        else:
            res.append(f'{(key + ":").ljust(maxlen)} \'{" ".join(value[0])}\'')
    return '\n'.join(res) + '\n'


def predict(text):
    predictions = model.predict([text])
    prediction = predictions[0]
    results = []
    if not prediction['opinions']:
        return 'No opinions detected'
    for opinion in prediction['opinions']:
        results.append(pretty_print_opinion(opinion))

    return '\n'.join(results)



markdown_text = '''
<br>
<br>
This space provides a gradio demo and an easy-to-run wrapper of the pre-trained model for structured sentiment analysis in Norwegian language, pre-trained on the [NoReC dataset](https://huggingface.co/datasets/norec).
This model is an implementation of the paper "Direct parsing to sentiment graphs" (Samuel _et al._, ACL 2022). The main repository that also contains the scripts for training the model, can be found on the project [github](https://github.com/jerbarnes/direct_parsing_to_sent_graph). 

The current model uses the 'labeled-edge' graph encoding, and achieves the following results on the NoReC dataset:

| Unlabeled sentiment tuple F1 | Target F1  | Relative polarity precision |
|:----------------------------:|:----------:|:---------------------------:|
|     0.393                    |  0.468     |        0.939                |


The model can be easily used for predicting sentiment tuples as follows: 

```python
>>> import model_wrapper
>>> model = model_wrapper.PredictionModel()
>>> model.predict(['vi liker svart kaffe'])
[{'sent_id': '0',
  'text': 'vi liker svart kaffe',
  'opinions': [{'Source': [['vi'], ['0:2']],
    'Target': [['svart', 'kaffe'], ['9:14', '15:20']],
    'Polar_expression': [['liker'], ['3:8']],
    'Polarity': 'Positive'}]}]
```
'''



with gr.Blocks() as demo:
    with gr.Row(equal_height=False) as row:
        text_input = gr.Textbox(label="input")
        text_output = gr.Textbox(label="output")
    with gr.Row(scale=4) as row:
        text_button = gr.Button("submit").style(full_width=True)

    text_button.click(fn=predict, inputs=text_input, outputs=text_output)

    gr.Markdown(markdown_text)



demo.launch()