ayberkimir commited on
Commit
8d175cc
1 Parent(s): 4dadc70

created tabbed interfaces

Browse files
app.py CHANGED
@@ -1,57 +1,18 @@
1
- from transformers import TextClassificationPipeline, AutoTokenizer, AutoModelForSequenceClassification
2
  import gradio as gr
3
- import os
4
 
5
- models = [
6
- "Overfit-GM/bert-base-turkish-cased-offensive",
7
- "Overfit-GM/bert-base-turkish-uncased-offensive",
8
- "Overfit-GM/bert-base-turkish-128k-cased-offensive",
9
- "Overfit-GM/bert-base-turkish-128k-uncased-offensive",
10
- "Overfit-GM/convbert-base-turkish-mc4-cased-offensive",
11
- "Overfit-GM/convbert-base-turkish-mc4-uncased-offensive",
12
- "Overfit-GM/convbert-base-turkish-cased-offensive",
13
- "Overfit-GM/distilbert-base-turkish-cased-offensive",
14
- "Overfit-GM/electra-base-turkish-cased-discriminator-offensive",
15
- "Overfit-GM/electra-base-turkish-mc4-cased-discriminator-offensive",
16
- "Overfit-GM/electra-base-turkish-mc4-uncased-discriminator-offensive",
17
- "Overfit-GM/xlm-roberta-large-turkish-offensive",
18
- "Overfit-GM/mdeberta-v3-base-offensive"
19
- ]
20
 
21
- model_box=[
22
- gr.load(models[0], src='models', hf_token=os.environ['API_KEY']),
23
- gr.load(models[1], src='models', hf_token=os.environ['API_KEY']),
24
- gr.load(models[2], src='models', hf_token=os.environ['API_KEY']),
25
- gr.load(models[3], src='models', hf_token=os.environ['API_KEY']),
26
- gr.load(models[4], src='models', hf_token=os.environ['API_KEY']),
27
- gr.load(models[5], src='models', hf_token=os.environ['API_KEY']),
28
- gr.load(models[6], src='models', hf_token=os.environ['API_KEY']),
29
- gr.load(models[7], src='models', hf_token=os.environ['API_KEY']),
30
- gr.load(models[8], src='models', hf_token=os.environ['API_KEY']),
31
- gr.load(models[9], src='models', hf_token=os.environ['API_KEY']),
32
- gr.load(models[10], src='models', hf_token=os.environ['API_KEY']),
33
- gr.load(models[11], src='models', hf_token=os.environ['API_KEY']),
34
- gr.load(models[12], src='models', hf_token=os.environ['API_KEY'])
35
- ]
36
-
37
- def sentiment_analysis(text, model_choice):
38
-
39
- model = model_box[model_choice]
40
- output = model(text)
41
- return output
42
-
43
- with gr.Blocks() as demo:
44
- gr.HTML("""<h1 style="font-weight:600;font-size:50;margin-top:4px;margin-bottom:4px;text-align:center;">No Offense Classifier</h1></div>""")
45
- with gr.Row():
46
- with gr.Column():
47
- model_choice = gr.Dropdown(label="Select Model", choices=[m for m in models], type="index", interactive=True)
48
- input_text = gr.Textbox(label="Input", placeholder="senin ben amk")
49
- the_button = gr.Button(label="Run")
50
- with gr.Column():
51
- output_window = gr.Label(num_top_classes=5)
52
-
53
- the_button.click(sentiment_analysis, inputs=[input_text, model_choice], outputs=[output_window])
54
- examples = gr.Examples(examples=["bu adamların ülkesine dönmesi lazım", "adam olsan oraya gitmezdin"],
55
- inputs=[input_text])
56
 
57
  demo.launch()
 
 
1
  import gradio as gr
 
2
 
3
+ from interfaces.classifier_interface import classifier_interface
4
+ from interfaces.masked_interface import masked_interface
5
+ from interfaces.embed_interface import embed_interface
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
+ demo = gr.TabbedInterface(
8
+ interface_list=[classifier_interface,
9
+ masked_interface,
10
+ embed_interface],
11
+ tab_names=[
12
+ 'Multiclass Classification',
13
+ 'Masked Language Modeling',
14
+ 'Sentence Similarity'
15
+ ]
16
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
  demo.launch()
interfaces/classifier_interface.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import TextClassificationPipeline, AutoTokenizer, AutoModelForSequenceClassification
2
+ import gradio as gr
3
+ import os
4
+
5
+
6
+ models = [
7
+ "Overfit-GM/bert-base-turkish-cased-offensive",
8
+ "Overfit-GM/bert-base-turkish-uncased-offensive",
9
+ "Overfit-GM/bert-base-turkish-128k-cased-offensive",
10
+ "Overfit-GM/bert-base-turkish-128k-uncased-offensive",
11
+ "Overfit-GM/convbert-base-turkish-mc4-cased-offensive",
12
+ "Overfit-GM/convbert-base-turkish-mc4-uncased-offensive",
13
+ "Overfit-GM/convbert-base-turkish-cased-offensive",
14
+ "Overfit-GM/distilbert-base-turkish-cased-offensive",
15
+ "Overfit-GM/electra-base-turkish-cased-discriminator-offensive",
16
+ "Overfit-GM/electra-base-turkish-mc4-cased-discriminator-offensive",
17
+ "Overfit-GM/electra-base-turkish-mc4-uncased-discriminator-offensive",
18
+ "Overfit-GM/xlm-roberta-large-turkish-offensive",
19
+ "Overfit-GM/mdeberta-v3-base-offensive"
20
+ ]
21
+
22
+
23
+ model_box=[
24
+ gr.load(models[0], src='models', hf_token=os.environ['API_KEY']),
25
+ gr.load(models[1], src='models', hf_token=os.environ['API_KEY']),
26
+ gr.load(models[2], src='models', hf_token=os.environ['API_KEY']),
27
+ gr.load(models[3], src='models', hf_token=os.environ['API_KEY']),
28
+ gr.load(models[4], src='models', hf_token=os.environ['API_KEY']),
29
+ gr.load(models[5], src='models', hf_token=os.environ['API_KEY']),
30
+ gr.load(models[6], src='models', hf_token=os.environ['API_KEY']),
31
+ gr.load(models[7], src='models', hf_token=os.environ['API_KEY']),
32
+ gr.load(models[8], src='models', hf_token=os.environ['API_KEY']),
33
+ gr.load(models[9], src='models', hf_token=os.environ['API_KEY']),
34
+ gr.load(models[10], src='models', hf_token=os.environ['API_KEY']),
35
+ gr.load(models[11], src='models', hf_token=os.environ['API_KEY']),
36
+ gr.load(models[12], src='models', hf_token=os.environ['API_KEY'])
37
+ ]
38
+
39
+
40
+
41
+ def sentiment_analysis(text, model_choice):
42
+
43
+ model = model_box[model_choice]
44
+ output = model(text)
45
+ return output
46
+
47
+ with gr.Blocks() as classifier_interface:
48
+ gr.HTML("""<h1 style="font-weight:600;font-size:50;margin-top:4px;margin-bottom:4px;text-align:center;">No Offense Classifier</h1></div>""")
49
+ with gr.Row():
50
+ with gr.Column():
51
+ model_choice = gr.Dropdown(label="Select Model", choices=[m for m in models], type="index", interactive=True)
52
+ input_text = gr.Textbox(label="Input", placeholder="senin ben amk")
53
+ the_button = gr.Button(label="Run")
54
+ with gr.Column():
55
+ output_window = gr.Label(num_top_classes=5)
56
+
57
+ the_button.click(sentiment_analysis, inputs=[input_text, model_choice], outputs=[output_window])
58
+ examples = gr.Examples(examples=["bu adamların ülkesine dönmesi lazım", "adam olsan oraya gitmezdin"],
59
+ inputs=[input_text])
interfaces/embed_interface.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import TextClassificationPipeline, AutoTokenizer, AutoModelForSequenceClassification
2
+ from nooffense.sentence_encoder import SentenceEncoder
3
+ import numpy as np
4
+ import gradio as gr
5
+ import os
6
+
7
+
8
+ models = [
9
+ "Overfit-GM/bert-base-turkish-cased-offensive",
10
+ "Overfit-GM/bert-base-turkish-uncased-offensive",
11
+ "Overfit-GM/bert-base-turkish-128k-cased-offensive",
12
+ "Overfit-GM/bert-base-turkish-128k-uncased-offensive",
13
+ "Overfit-GM/convbert-base-turkish-mc4-cased-offensive",
14
+ "Overfit-GM/convbert-base-turkish-mc4-uncased-offensive",
15
+ "Overfit-GM/convbert-base-turkish-cased-offensive",
16
+ "Overfit-GM/distilbert-base-turkish-cased-offensive",
17
+ "Overfit-GM/electra-base-turkish-cased-discriminator-offensive",
18
+ "Overfit-GM/electra-base-turkish-mc4-cased-discriminator-offensive",
19
+ "Overfit-GM/electra-base-turkish-mc4-uncased-discriminator-offensive",
20
+ "Overfit-GM/xlm-roberta-large-turkish-offensive",
21
+ "Overfit-GM/mdeberta-v3-base-offensive"
22
+ ]
23
+
24
+ sentence_list = [] #global variable go brr
25
+
26
+ def normalize_outputs(pred):
27
+ values = np.asarray([p[1] for p in pred])
28
+ normalized = (values-min(values))/(max(values)-min(values))
29
+ new_preds = {p[0]:float(v) for p,v in zip(pred, normalized)}
30
+ return new_preds
31
+
32
+ def clear_sentences():
33
+ sentence_list.clear()
34
+ return None
35
+
36
+ def display_list(text):
37
+ sentence_list.append(text)
38
+ new_text = '\n'.join(sentence_list)
39
+ return new_text
40
+
41
+ def sentiment_analysis(text, model_choice):
42
+ model = SentenceEncoder(models[model_choice])
43
+ pred = model.find_most_similar(text, sentence_list)
44
+ return normalize_outputs(pred)
45
+
46
+ with gr.Blocks() as embed_interface:
47
+ 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>""")
48
+ with gr.Row():
49
+ with gr.Column():
50
+ model_choice = gr.Dropdown(label="Select Model", choices=[m for m in models], type="index", interactive=True)
51
+ input_text = gr.Textbox(label="Input", placeholder="senin ben amk")
52
+ with gr.Row():
53
+ with gr.Column():
54
+ input_text2 = gr.Textbox(label ='Add Sentence', placeholder='asdas')
55
+ with gr.Column():
56
+ input_text3 = gr.Textbox(label ='Sentences List', placeholder='asdasd')
57
+ with gr.Row():
58
+ add_button = gr.Button('Add')
59
+ clear_button = gr.Button('Clear')
60
+ the_button = gr.Button("Run")
61
+ with gr.Column():
62
+ output_window = gr.Label(num_top_classes=5, show_label=False)
63
+
64
+ clear_button.click(clear_sentences, outputs=[input_text3])
65
+ add_button.click(display_list, inputs=[input_text2], outputs=[input_text3])
66
+ the_button.click(sentiment_analysis, inputs=[input_text, model_choice], outputs=[output_window])
interfaces/masked_interface.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import TextClassificationPipeline, AutoTokenizer, AutoModelForSequenceClassification
2
+ import gradio as gr
3
+ import os
4
+
5
+ models = [
6
+ "Overfit-GM/bert-base-turkish-cased-offensive-mlm",
7
+ "Overfit-GM/bert-base-turkish-uncased-offensive-mlm",
8
+ "Overfit-GM/bert-base-turkish-128k-cased-offensive-mlm",
9
+ "Overfit-GM/bert-base-turkish-128k-uncased-offensive-mlm",
10
+ "Overfit-GM/convbert-base-turkish-mc4-cased-offensive-mlm",
11
+ "Overfit-GM/convbert-base-turkish-mc4-uncased-offensive-mlm",
12
+ "Overfit-GM/convbert-base-turkish-cased-offensive-mlm",
13
+ "Overfit-GM/distilbert-base-turkish-cased-offensive-mlm",
14
+ "Overfit-GM/electra-base-turkish-cased-discriminator-offensive-mlm",
15
+ "Overfit-GM/electra-base-turkish-mc4-cased-discriminator-offensive-mlm",
16
+ "Overfit-GM/electra-base-turkish-mc4-uncased-discriminator-offensive-mlm",
17
+ "Overfit-GM/xlm-roberta-large-offensive-mlm",
18
+ "Overfit-GM/mdeberta-v3-base-offensive-mlm"
19
+ ]
20
+
21
+
22
+ model_box=[
23
+ gr.load(models[0], src='models', hf_token=os.environ['API_KEY']),
24
+ gr.load(models[1], src='models', hf_token=os.environ['API_KEY']),
25
+ gr.load(models[2], src='models', hf_token=os.environ['API_KEY']),
26
+ gr.load(models[3], src='models', hf_token=os.environ['API_KEY']),
27
+ gr.load(models[4], src='models', hf_token=os.environ['API_KEY']),
28
+ gr.load(models[5], src='models', hf_token=os.environ['API_KEY']),
29
+ gr.load(models[6], src='models', hf_token=os.environ['API_KEY']),
30
+ gr.load(models[7], src='models', hf_token=os.environ['API_KEY']),
31
+ gr.load(models[8], src='models', hf_token=os.environ['API_KEY']),
32
+ gr.load(models[9], src='models', hf_token=os.environ['API_KEY']),
33
+ gr.load(models[10], src='models', hf_token=os.environ['API_KEY']),
34
+ gr.load(models[11], src='models', hf_token=os.environ['API_KEY']),
35
+ gr.load(models[12], src='models', hf_token=os.environ['API_KEY'])
36
+ ]
37
+
38
+ def sentiment_analysis(text, model_choice):
39
+
40
+ model = model_box[model_choice]
41
+ output = model(text)
42
+ return output
43
+
44
+ with gr.Blocks() as masked_interface:
45
+ gr.HTML("""<h1 style="font-weight:600;font-size:50;margin-top:4px;margin-bottom:4px;text-align:center;">No Offense Fill Masks</h1></div>""")
46
+ with gr.Row():
47
+ with gr.Column():
48
+ model_choice = gr.Dropdown(label="Select Model", choices=[m for m in models], type="index", interactive=True)
49
+ input_text = gr.Textbox(label="Input", placeholder="senin ben [MASK]")
50
+ the_button = gr.Button(label="Run")
51
+ with gr.Column():
52
+ output_window = gr.Label(num_top_classes=5)
53
+
54
+ the_button.click(sentiment_analysis, inputs=[input_text, model_choice], outputs=[output_window])
55
+ examples = gr.Examples(examples=["sen tam bir [MASK]", "erkekler [MASK] üstündür"],
56
+ inputs=[input_text])
requirements.txt CHANGED
@@ -1,4 +1,5 @@
1
  torch
2
  transformers
3
  numpy
4
- gradio
 
 
1
  torch
2
  transformers
3
  numpy
4
+ gradio
5
+ git+https://github.com/ertugrul-dmr/NoOffense.git