Spaces:
Sleeping
Sleeping
""" | |
python interactive.py | |
""" | |
import torch | |
from transformers import AutoTokenizer, BertForSequenceClassification | |
from transformers import TextClassificationPipeline | |
import gradio as gr | |
model_name = 'momo/KcBERT-base_Hate_speech_Privacy_Detection' | |
model_name_list = [ | |
'momo/KcELECTRA-base_Hate_speech_Privacy_Detection', | |
"momo/KcBERT-base_Hate_speech_Privacy_Detection", | |
] | |
model = BertForSequenceClassification.from_pretrained( | |
model_name | |
) | |
tokenizer = AutoTokenizer.from_pretrained(model_name) | |
unsmile_labels = ["์ฌ์ฑ/๊ฐ์กฑ","๋จ์ฑ","์ฑ์์์","์ธ์ข /๊ตญ์ ","์ฐ๋ น","์ง์ญ","์ข ๊ต","๊ธฐํ ํ์ค","์ ํ/์์ค","clean", 'name', 'number', 'address', 'bank', 'person'] | |
num_labels = len(unsmile_labels) | |
model.config.id2label = {i: label for i, label in zip(range(num_labels), unsmile_labels)} | |
model.config.label2id = {label: i for i, label in zip(range(num_labels), unsmile_labels)} | |
pipe = TextClassificationPipeline( | |
model = model, | |
tokenizer = tokenizer, | |
device=0, | |
return_all_scores=True, | |
function_to_apply='sigmoid' | |
) | |
def dectection(input): | |
for result in pipe(input)[0]: | |
return result | |
#Create a gradio app with a button that calls predict() | |
app = gr.Interface( | |
fn=dectection, | |
inputs=[gr.inputs.Dropdown(model_name_list, label="Model Name"), 'text'], outputs=['label'], | |
title="ํ๊ตญ์ด ํ์คํํ, ๊ฐ์ธ์ ๋ณด ํ๋ณ๊ธฐ (Korean Hate Speech and Privacy Detection)", | |
description="Korean Hate Speech and Privacy Detection." | |
) | |
app.launch(share=True) | |
# # global var | |
# MODEL_NAME = 'jason9693/SoongsilBERT-base-beep' | |
# tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME) | |
# model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME) | |
# config = AutoConfig.from_pretrained(MODEL_NAME) | |
# MODEL_BUF = { | |
# "name": MODEL_NAME, | |
# "tokenizer": tokenizer, | |
# "model": model, | |
# "config": config | |
# } | |
# def change_model_name(name): | |
# MODEL_BUF["name"] = name | |
# MODEL_BUF["tokenizer"] = AutoTokenizer.from_pretrained(name) | |
# MODEL_BUF["model"] = AutoModelForSequenceClassification.from_pretrained(name) | |
# MODEL_BUF["config"] = AutoConfig.from_pretrained(name) | |
# def predict(model_name, text): | |
# if model_name != MODEL_BUF["name"]: | |
# change_model_name(model_name) | |
# tokenizer = MODEL_BUF["tokenizer"] | |
# model = MODEL_BUF["model"] | |
# config = MODEL_BUF["config"] | |
# tokenized_text = tokenizer([text], return_tensors='pt') | |
# input_tokens = tokenizer.convert_ids_to_tokens(tokenized_text.input_ids[0]) | |
# try: | |
# input_tokens = util.bytetokens_to_unicdode(input_tokens) if config.model_type in ['roberta', 'gpt', 'gpt2'] else input_tokens | |
# except KeyError: | |
# input_tokens = input_tokens | |
# model.eval() | |
# output, attention = model(**tokenized_text, output_attentions=True, return_dict=False) | |
# output = F.softmax(output, dim=-1) | |
# result = {} | |
# for idx, label in enumerate(output[0].detach().numpy()): | |
# result[config.id2label[idx]] = float(label) | |
# fig = visualize_attention(input_tokens, attention[0][0].detach().numpy()) | |
# return result, fig#.logits.detach()#.numpy()#, output.attentions.detach().numpy() | |
# if __name__ == '__main__': | |
# text = '์ฟ๋ด๊ฑธ ํ๋ณฟ๊ธ ์ฟ๋๊ณญ ์์ ฉ๋๊ณ ์์์๋ฉ' | |
# model_name_list = [ | |
# 'jason9693/SoongsilBERT-base-beep', | |
# "beomi/beep-klue-roberta-base-hate", | |
# "beomi/beep-koelectra-base-v3-discriminator-hate", | |
# "beomi/beep-KcELECTRA-base-hate" | |
# ] | |
# #Create a gradio app with a button that calls predict() | |
# app = gr.Interface( | |
# fn=predict, | |
# inputs=[gr.inputs.Dropdown(model_name_list, label="Model Name"), 'text'], outputs=['label', 'plot'], | |
# examples = [[MODEL_BUF["name"], text], [MODEL_BUF["name"], "4=๐ฆ 4โ ๐ฆ"]], | |
# title="ํ๊ตญ์ด ํ์ค์ฑ ๋ฐํ ๋ถ๋ฅ๊ธฐ (Korean Hate Speech Classifier)", | |
# description="Korean Hate Speech Classifier with Several Pretrained LM\nCurrent Supported Model:\n1. SoongsilBERT\n2. KcBERT(+KLUE)\n3. KcELECTRA\n4.KoELECTRA." | |
# ) | |
# app.launch(inline=False) |