File size: 3,185 Bytes
8d175cc
 
 
 
 
 
 
5722b62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8d175cc
5722b62
 
 
 
 
 
8d175cc
5722b62
 
 
 
 
 
 
 
 
8d175cc
 
5722b62
 
 
 
 
 
 
 
 
 
 
8d175cc
5722b62
 
 
 
 
8d175cc
5722b62
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


class EmbedInterface:
    def __init__(self):
        self.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"
    ]
    
    
    def clear_sentences(self):
        return ""

    def display_list(self, written_text, text_to_add):
        if written_text == "":
            new_text = text_to_add
        else:
            new_text = written_text + "\n" + text_to_add
        return new_text

    def sentiment_analysis(self, text, model_choice, text_to_compare):
        sentence_list = text_to_compare.split('\n')
        model = SentenceEncoder(self.models[model_choice])
        pred = model.find_most_similar(text, sentence_list)
        return {p[0]:(float(p[1]) if p[1]>0 else 0) for p in pred}
    
    def __call__(self):
        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 self.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='aptal aptal konuşma')
                        with gr.Column():
                            input_text3 = gr.Textbox(label ='Sentences List')
                    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(self.clear_sentences, outputs=[input_text3])
            add_button.click(self.display_list, inputs=[input_text3,input_text2], outputs=[input_text3])
            the_button.click(self.sentiment_analysis, inputs=[input_text, model_choice, input_text3], outputs=[output_window])

        return embed_interface