Spaces:
Runtime error
Runtime error
Commit
·
5722b62
1
Parent(s):
8d175cc
code refactoring
Browse files- app.py +11 -6
- interfaces/classifier_interface.py +56 -54
- interfaces/embed_interface.py +53 -53
- interfaces/masked_interface.py +59 -51
app.py
CHANGED
@@ -1,13 +1,18 @@
|
|
1 |
import gradio as gr
|
2 |
|
3 |
-
from interfaces.classifier_interface import
|
4 |
-
from interfaces.masked_interface import
|
5 |
-
from interfaces.embed_interface import
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
demo = gr.TabbedInterface(
|
8 |
-
interface_list=[
|
9 |
-
masked_interface,
|
10 |
-
embed_interface],
|
11 |
tab_names=[
|
12 |
'Multiclass Classification',
|
13 |
'Masked Language Modeling',
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
+
from interfaces.classifier_interface import ClassifierInterface
|
4 |
+
from interfaces.masked_interface import MaskedInterface
|
5 |
+
from interfaces.embed_interface import EmbedInterface
|
6 |
+
|
7 |
+
|
8 |
+
class_interface = ClassifierInterface()
|
9 |
+
masked_interface = MaskedInterface()
|
10 |
+
embed_interface = EmbedInterface()
|
11 |
|
12 |
demo = gr.TabbedInterface(
|
13 |
+
interface_list=[class_interface(),
|
14 |
+
masked_interface(),
|
15 |
+
embed_interface()],
|
16 |
tab_names=[
|
17 |
'Multiclass Classification',
|
18 |
'Masked Language Modeling',
|
interfaces/classifier_interface.py
CHANGED
@@ -3,57 +3,59 @@ import gradio as gr
|
|
3 |
import os
|
4 |
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
model_box=[
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
]
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
def sentiment_analysis(text, model_choice):
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
|
|
|
3 |
import os
|
4 |
|
5 |
|
6 |
+
class ClassifierInterface:
|
7 |
+
def __init__(self):
|
8 |
+
self.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 |
+
self.model_box=[
|
24 |
+
gr.load(self.models[0], src='models', hf_token=os.environ['API_KEY']),
|
25 |
+
gr.load(self.models[1], src='models', hf_token=os.environ['API_KEY']),
|
26 |
+
gr.load(self.models[2], src='models', hf_token=os.environ['API_KEY']),
|
27 |
+
gr.load(self.models[3], src='models', hf_token=os.environ['API_KEY']),
|
28 |
+
gr.load(self.models[4], src='models', hf_token=os.environ['API_KEY']),
|
29 |
+
gr.load(self.models[5], src='models', hf_token=os.environ['API_KEY']),
|
30 |
+
gr.load(self.models[6], src='models', hf_token=os.environ['API_KEY']),
|
31 |
+
gr.load(self.models[7], src='models', hf_token=os.environ['API_KEY']),
|
32 |
+
gr.load(self.models[8], src='models', hf_token=os.environ['API_KEY']),
|
33 |
+
gr.load(self.models[9], src='models', hf_token=os.environ['API_KEY']),
|
34 |
+
gr.load(self.models[10], src='models', hf_token=os.environ['API_KEY']),
|
35 |
+
gr.load(self.models[11], src='models', hf_token=os.environ['API_KEY']),
|
36 |
+
gr.load(self.models[12], src='models', hf_token=os.environ['API_KEY'])
|
37 |
+
]
|
38 |
+
|
39 |
+
|
40 |
+
|
41 |
+
def sentiment_analysis(self, text, model_choice):
|
42 |
+
model = self.model_box[model_choice]
|
43 |
+
output = model(text)
|
44 |
+
return output
|
45 |
+
|
46 |
+
|
47 |
+
def __call__(self):
|
48 |
+
with gr.Blocks() as classifier_interface:
|
49 |
+
gr.HTML("""<h1 style="font-weight:600;font-size:50;margin-top:4px;margin-bottom:4px;text-align:center;">No Offense Classifier</h1></div>""")
|
50 |
+
with gr.Row():
|
51 |
+
with gr.Column():
|
52 |
+
model_choice = gr.Dropdown(label="Select Model", choices=[m for m in self.models], type="index", interactive=True)
|
53 |
+
input_text = gr.Textbox(label="Input", placeholder="senin ben amk")
|
54 |
+
the_button = gr.Button(label="Run")
|
55 |
+
with gr.Column():
|
56 |
+
output_window = gr.Label(num_top_classes=5)
|
57 |
+
|
58 |
+
the_button.click(self.sentiment_analysis, inputs=[input_text, model_choice], outputs=[output_window])
|
59 |
+
examples = gr.Examples(examples=["bu adamların ülkesine dönmesi lazım", "adam olsan oraya gitmezdin"],
|
60 |
+
inputs=[input_text])
|
61 |
+
return classifier_interface
|
interfaces/embed_interface.py
CHANGED
@@ -5,62 +5,62 @@ import gradio as gr
|
|
5 |
import os
|
6 |
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
-
def
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
def
|
33 |
-
|
34 |
-
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
with gr.Column():
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
the_button =
|
61 |
-
with gr.Column():
|
62 |
-
output_window = gr.Label(num_top_classes=5, show_label=False)
|
63 |
|
64 |
-
|
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])
|
|
|
5 |
import os
|
6 |
|
7 |
|
8 |
+
class EmbedInterface:
|
9 |
+
def __init__(self):
|
10 |
+
self.models = [
|
11 |
+
"Overfit-GM/bert-base-turkish-cased-offensive",
|
12 |
+
"Overfit-GM/bert-base-turkish-uncased-offensive",
|
13 |
+
"Overfit-GM/bert-base-turkish-128k-cased-offensive",
|
14 |
+
"Overfit-GM/bert-base-turkish-128k-uncased-offensive",
|
15 |
+
"Overfit-GM/convbert-base-turkish-mc4-cased-offensive",
|
16 |
+
"Overfit-GM/convbert-base-turkish-mc4-uncased-offensive",
|
17 |
+
"Overfit-GM/convbert-base-turkish-cased-offensive",
|
18 |
+
"Overfit-GM/distilbert-base-turkish-cased-offensive",
|
19 |
+
"Overfit-GM/electra-base-turkish-cased-discriminator-offensive",
|
20 |
+
"Overfit-GM/electra-base-turkish-mc4-cased-discriminator-offensive",
|
21 |
+
"Overfit-GM/electra-base-turkish-mc4-uncased-discriminator-offensive",
|
22 |
+
"Overfit-GM/xlm-roberta-large-turkish-offensive",
|
23 |
+
"Overfit-GM/mdeberta-v3-base-offensive"
|
24 |
+
]
|
25 |
+
|
26 |
+
|
27 |
+
def clear_sentences(self):
|
28 |
+
return ""
|
29 |
|
30 |
+
def display_list(self, written_text, text_to_add):
|
31 |
+
if written_text == "":
|
32 |
+
new_text = text_to_add
|
33 |
+
else:
|
34 |
+
new_text = written_text + "\n" + text_to_add
|
35 |
+
return new_text
|
36 |
|
37 |
+
def sentiment_analysis(self, text, model_choice, text_to_compare):
|
38 |
+
sentence_list = text_to_compare.split('\n')
|
39 |
+
model = SentenceEncoder(self.models[model_choice])
|
40 |
+
pred = model.find_most_similar(text, sentence_list)
|
41 |
+
return {p[0]:(float(p[1]) if p[1]>0 else 0) for p in pred}
|
42 |
+
|
43 |
+
def __call__(self):
|
44 |
+
with gr.Blocks() as embed_interface:
|
45 |
+
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>""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
with gr.Row():
|
47 |
with gr.Column():
|
48 |
+
model_choice = gr.Dropdown(label="Select Model", choices=[m for m in self.models], type="index", interactive=True)
|
49 |
+
input_text = gr.Textbox(label="Input", placeholder="senin ben amk")
|
50 |
+
with gr.Row():
|
51 |
+
with gr.Column():
|
52 |
+
input_text2 = gr.Textbox(label ='Add Sentence', placeholder='aptal aptal konuşma')
|
53 |
+
with gr.Column():
|
54 |
+
input_text3 = gr.Textbox(label ='Sentences List')
|
55 |
+
with gr.Row():
|
56 |
+
add_button = gr.Button('Add')
|
57 |
+
clear_button = gr.Button('Clear')
|
58 |
+
the_button = gr.Button("Run")
|
59 |
with gr.Column():
|
60 |
+
output_window = gr.Label(num_top_classes=5, show_label=False)
|
61 |
+
|
62 |
+
clear_button.click(self.clear_sentences, outputs=[input_text3])
|
63 |
+
add_button.click(self.display_list, inputs=[input_text3,input_text2], outputs=[input_text3])
|
64 |
+
the_button.click(self.sentiment_analysis, inputs=[input_text, model_choice, input_text3], outputs=[output_window])
|
|
|
|
|
65 |
|
66 |
+
return embed_interface
|
|
|
|
interfaces/masked_interface.py
CHANGED
@@ -2,55 +2,63 @@ from transformers import TextClassificationPipeline, AutoTokenizer, AutoModelFor
|
|
2 |
import gradio as gr
|
3 |
import os
|
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 |
-
with gr.
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
|
|
|
|
|
|
|
2 |
import gradio as gr
|
3 |
import os
|
4 |
|
5 |
+
|
6 |
+
class MaskedInterface:
|
7 |
+
def __init__(self):
|
8 |
+
self.models = [
|
9 |
+
"Overfit-GM/bert-base-turkish-cased-offensive-mlm",
|
10 |
+
"Overfit-GM/bert-base-turkish-uncased-offensive-mlm",
|
11 |
+
"Overfit-GM/bert-base-turkish-128k-cased-offensive-mlm",
|
12 |
+
"Overfit-GM/bert-base-turkish-128k-uncased-offensive-mlm",
|
13 |
+
"Overfit-GM/convbert-base-turkish-mc4-cased-offensive-mlm",
|
14 |
+
"Overfit-GM/convbert-base-turkish-mc4-uncased-offensive-mlm",
|
15 |
+
"Overfit-GM/convbert-base-turkish-cased-offensive-mlm",
|
16 |
+
"Overfit-GM/distilbert-base-turkish-cased-offensive-mlm",
|
17 |
+
"Overfit-GM/electra-base-turkish-cased-discriminator-offensive-mlm",
|
18 |
+
"Overfit-GM/electra-base-turkish-mc4-cased-discriminator-offensive-mlm",
|
19 |
+
"Overfit-GM/electra-base-turkish-mc4-uncased-discriminator-offensive-mlm",
|
20 |
+
"Overfit-GM/xlm-roberta-large-offensive-mlm",
|
21 |
+
"Overfit-GM/mdeberta-v3-base-offensive-mlm"
|
22 |
+
]
|
23 |
+
self.model_box = [
|
24 |
+
gr.load(self.models[0], src='models', hf_token=os.environ['API_KEY']),
|
25 |
+
gr.load(self.models[1], src='models', hf_token=os.environ['API_KEY']),
|
26 |
+
gr.load(self.models[2], src='models', hf_token=os.environ['API_KEY']),
|
27 |
+
gr.load(self.models[3], src='models', hf_token=os.environ['API_KEY']),
|
28 |
+
gr.load(self.models[4], src='models', hf_token=os.environ['API_KEY']),
|
29 |
+
gr.load(self.models[5], src='models', hf_token=os.environ['API_KEY']),
|
30 |
+
gr.load(self.models[6], src='models', hf_token=os.environ['API_KEY']),
|
31 |
+
gr.load(self.models[7], src='models', hf_token=os.environ['API_KEY']),
|
32 |
+
gr.load(self.models[8], src='models', hf_token=os.environ['API_KEY']),
|
33 |
+
gr.load(self.models[9], src='models', hf_token=os.environ['API_KEY']),
|
34 |
+
gr.load(self.models[10], src='models', hf_token=os.environ['API_KEY']),
|
35 |
+
gr.load(self.models[11], src='models', hf_token=os.environ['API_KEY']),
|
36 |
+
gr.load(self.models[12], src='models', hf_token=os.environ['API_KEY'])
|
37 |
+
]
|
38 |
+
|
39 |
+
def sentiment_analysis(self, text, model_choice):
|
40 |
+
model = self.model_box[model_choice]
|
41 |
+
output = model(text)
|
42 |
+
return output
|
43 |
|
44 |
+
def __call__(self):
|
45 |
+
with gr.Blocks() as masked_interface:
|
46 |
+
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>""")
|
47 |
+
with gr.Row():
|
48 |
+
with gr.Column():
|
49 |
+
model_choice = gr.Dropdown(label="Select Model", choices=[m for m in self.models], type="index", interactive=True)
|
50 |
+
input_text = gr.Textbox(label="Input", placeholder="senin ben [MASK]")
|
51 |
+
the_button = gr.Button(label="Run")
|
52 |
+
with gr.Column():
|
53 |
+
output_window = gr.Label(num_top_classes=5)
|
54 |
+
|
55 |
+
the_button.click(self.sentiment_analysis, inputs=[input_text, model_choice], outputs=[output_window])
|
56 |
+
examples = gr.Examples(examples=["sen tam bir [MASK]", "erkekler [MASK] üstündür"],
|
57 |
+
inputs=[input_text])
|
58 |
+
|
59 |
+
return masked_interface
|
60 |
+
|
61 |
+
|
62 |
+
|
63 |
+
|
64 |
+
|