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("""

No Offense Sentence Similarity

""") 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