File size: 2,647 Bytes
8044721
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e2897e4
 
37d1022
 
 
fe77cff
37d1022
 
 
 
fe77cff
37d1022
e2897e4
 
90beb6a
8044721
 
e2897e4
8044721
 
 
 
 
 
 
 
 
 
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
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):
    print(f'Input message "{text}"')
    try:
        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))
        print(f'Successfully predicted SA for input message "{text}": {results}')
        return '\n'.join(results)
    except Exception as e:
        print(f'Error for input message "{text}": {e}')
        raise e



markdown_text = '''
<h1>Structured Sentiment Analysis for Norwegian</h1>
<p align="left">
This space provides a gradio demo of a <a href="https://huggingface.co/ltg/ssa-perin">pretrained model</a> for structured sentiment analysis (SSA) of Norwegian text, trained on the <a href="https://github.com/ltgoslo/norec_fine">NoReC_fine</a> dataset by the <a href"https://www.mn.uio.no/ifi/english/research/groups/ltg/">Language Technology Group</a> at the University of Oslo. It implements a method described in the paper <a href="https://aclanthology.org/2022.acl-short.51/">Direct parsing to sentiment graphs</a> by Samuel et al. 2022.
<br>
For a given sentence, the model will attempt to identify the following components if it is found to be sentiment-bearing: 
<ul>
  <li> <i>source expressions</i> (the opinion holder), </li>
  <li> <i>target expressions</i> (what the opinion is directed towards), </li>
  <li> <i>polar expressions</i> (the part of the text indicating that an opinion is expressed), </li>
  <li> and finally the <i>polarity</i> (positive or negative). </li>
</ul>
<br>
To download the model and find more in-depth documentation, please see <a href="https://huggingface.co/ltg/ssa-perin">https://huggingface.co/ltg/ssa-perin</a>
</p>
'''

with gr.Blocks() as demo:
    gr.Markdown(markdown_text)
    with gr.Row() as row:
        text_input = gr.Textbox(label="input")
        text_output = gr.Textbox(label="output")
    with gr.Row() as row:
        text_button = gr.Button("submit")

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


demo.launch()