File size: 1,382 Bytes
2168cf5
 
 
6ce2f8e
2168cf5
7f68476
 
2168cf5
7f68476
ef26fd6
7f68476
 
a60235f
2168cf5
 
7f68476
 
 
 
 
 
2168cf5
d961c51
7f68476
2168cf5
 
ef26fd6
7f68476
d961c51
7f68476
 
 
 
2168cf5
c0d80b6
53c2635
92bbd4b
c0d80b6
0fc2865
92bbd4b
2168cf5
0fc2865
 
92bbd4b
0fc2865
92bbd4b
 
c0d80b6
2168cf5
6ce2f8e
92bbd4b
 
dc02166
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
from transformers import pipeline

import gradio as gr
from gradio.mix import Parallel

pretrained_sentiment = "w11wo/indonesian-roberta-base-sentiment-classifier"
pretrained_ner = "cahya/bert-base-indonesian-NER"

sentiment_pipeline = pipeline(
    "sentiment-analysis",
    model=pretrained_sentiment,
    tokenizer=pretrained_sentiment,
    return_all_scores=True
)

ner_pipeline = pipeline(
    "ner",
    model=pretrained_ner,
    tokenizer=pretrained_ner
)

examples = [
    "Masyarakat sangat kecewa dengan tragedi Kanjuruhan",
    "Jokowi mengutuk kepolisian atas kerusuhan yang terjadi di Malang"
]

def sentiment_analysis(text):
    output = sentiment_pipeline(text)
    return {elm["label"]: elm["score"] for elm in output[0]}
    
def ner(text):
    output = ner_pipeline(text)
    return {"text": text, "entities": output}

sentiment_demo = gr.Interface(
    fn=sentiment_analysis,
    inputs="text",
    outputs="label",
    interpretation="default",
    title="Sentiment Classification")

ner_demo = gr.Interface(
    ner,
    "text",
    gr.HighlightedText(),
    examples=examples,
    title="Named Entity Recognition")

if __name__ == "__main__":
    Parallel(sentiment_demo, ner_demo,
    inputs=gr.Textbox(lines=20, label="Input Text", placeholder="Enter sentences here..."),
    title="Entity Based Sentiment Analysis Indonesia",
    examples=examples).launch()