File size: 2,864 Bytes
8d175cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from transformers import TextClassificationPipeline, AutoTokenizer, AutoModelForSequenceClassification
from nooffense.sentence_encoder import SentenceEncoder
import numpy as np
import gradio as gr
import os


models = [
    "Overfit-GM/bert-base-turkish-cased-offensive",
    "Overfit-GM/bert-base-turkish-uncased-offensive",
    "Overfit-GM/bert-base-turkish-128k-cased-offensive",
    "Overfit-GM/bert-base-turkish-128k-uncased-offensive",
    "Overfit-GM/convbert-base-turkish-mc4-cased-offensive",
    "Overfit-GM/convbert-base-turkish-mc4-uncased-offensive",
    "Overfit-GM/convbert-base-turkish-cased-offensive",
    "Overfit-GM/distilbert-base-turkish-cased-offensive",
    "Overfit-GM/electra-base-turkish-cased-discriminator-offensive",
    "Overfit-GM/electra-base-turkish-mc4-cased-discriminator-offensive",
    "Overfit-GM/electra-base-turkish-mc4-uncased-discriminator-offensive",
    "Overfit-GM/xlm-roberta-large-turkish-offensive",
    "Overfit-GM/mdeberta-v3-base-offensive"
]

sentence_list = [] #global variable go brr

def normalize_outputs(pred):
    values = np.asarray([p[1] for p in pred])
    normalized = (values-min(values))/(max(values)-min(values))
    new_preds = {p[0]:float(v) for p,v in zip(pred, normalized)}
    return new_preds

def clear_sentences():
    sentence_list.clear()
    return None

def display_list(text):
    sentence_list.append(text)
    new_text = '\n'.join(sentence_list)
    return new_text

def sentiment_analysis(text, model_choice):
    model = SentenceEncoder(models[model_choice])
    pred = model.find_most_similar(text, sentence_list)
    return normalize_outputs(pred)

with gr.Blocks() as embed_interface:
    gr.HTML("""<h1 style="font-weight:600;font-size:50;margin-top:4px;margin-bottom:4px;text-align:center;">No Offense Sentence Similarity</h1></div>""")
    with gr.Row():
        with gr.Column():
            model_choice = gr.Dropdown(label="Select Model", choices=[m for m in models], type="index", interactive=True)
            input_text = gr.Textbox(label="Input", placeholder="senin ben amk")
            with gr.Row():
                with gr.Column():
                    input_text2 = gr.Textbox(label ='Add Sentence', placeholder='asdas')
                with gr.Column():
                    input_text3 = gr.Textbox(label ='Sentences List', placeholder='asdasd')
            with gr.Row():
                add_button = gr.Button('Add')       
                clear_button = gr.Button('Clear')
            the_button = gr.Button("Run")
        with gr.Column():
            output_window = gr.Label(num_top_classes=5, show_label=False)

    clear_button.click(clear_sentences, outputs=[input_text3])
    add_button.click(display_list, inputs=[input_text2], outputs=[input_text3])
    the_button.click(sentiment_analysis, inputs=[input_text, model_choice], outputs=[output_window])