Spaces:
Sleeping
Sleeping
File size: 4,886 Bytes
498ff0a 849819f 498ff0a 849819f 498ff0a 849819f 498ff0a 849819f 498ff0a 849819f 498ff0a 849819f 498ff0a 849819f 498ff0a 849819f 498ff0a |
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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# -*- coding: utf-8 -*-
import json
import pandas as pd
import numpy as np
import torch
import streamlit as st
from transformers import AutoTokenizer, AutoModelForTokenClassification
st.set_page_config(
page_title="NER ๊ธฐ๋ฐ ๋ฏผ๊ฐ์ ๋ณด ์๋ณ", layout="wide", initial_sidebar_state="expanded"
)
@st.cache
def load_model(model_name):
model = AutoModelForTokenClassification.from_pretrained(model_name)
return model
st.title("๐ NER ๊ธฐ๋ฐ ๋ฏผ๊ฐ์ ๋ณด ์๋ณ๊ธฐ")
st.write("๋ฌธ์ฅ์ ์
๋ ฅํ์๊ณ , CTRL+Enter(CMD+Enter)๋ฅผ ๋๋ฅด์ธ์ ๐ค")
tokenizer = AutoTokenizer.from_pretrained("klue/roberta-base")
model = load_model("QuoQA-NLP/konec-privacy")
model.eval()
default_value = "์์ง๋, ๋น๋จ ๊ฒ์ฌํ ๊ฑฐ ๊ฒฐ๊ณผ ๋์ค์
จ์ด์."
src_text = st.text_area(
"๊ฒ์ฌํ๊ณ ์ถ์ ๋ฌธ์ฅ์ ์
๋ ฅํ์ธ์.",
default_value,
height=300,
max_chars=150,
)
def yield_df(default_value):
tokenized = tokenizer.encode(default_value)
print(tokenized)
output = model(input_ids=torch.tensor([tokenized]))
logits = output.logits
print(logits.size())
# get prediction for each tokens for 17 classes
pred = logits.argmax(-1).squeeze().numpy()
print(pred)
class_map = {
"B-ADD": 0,
"I-ADD": 1,
"B-DN": 2,
"I-DN": 3,
"B-DT": 4,
"I-DT": 5,
"B-LC": 6,
"I-LC": 7,
"B-OG": 8,
"I-OG": 9,
"B-PS": 10,
"I-PS": 11,
"B-QT": 12,
"I-QT": 13,
"B-RL": 14,
"I-RL": 15,
"O": 16
}
class_map_inverted = {v: k for k, v in class_map.items()}
# decode prediction
class_decoded = [class_map_inverted[p] for p in pred]
print(class_decoded)
label_map = {
"ADD": "์ฃผ์ ์ ๋ณด",
"DN": "์งํ ์ ๋ณด",
"DT": "๋ ์ง ์ ๋ณด",
"LC": "์ฅ์ ์ ๋ณด",
"OG": "๊ธฐ๊ด ์ ๋ณด",
"PS": "์ธ๋ช
/๋ณ๋ช
์ ๋ณด",
"QT": "์๋ ์ ๋ณด",
"RL": "๊ด๊ณ ์ ๋ณด",
"O": "๋น๋ฏผ๊ฐ ์ ๋ณด"
}
# pair tokens with prediction
tokenized_text = tokenizer.convert_ids_to_tokens(tokenized)
list_result = []
for token, pred in zip(tokenized_text, class_decoded):
splitted_pred = pred.split("-")
pred_class = splitted_pred[-1]
label = label_map[pred_class]
# print with 10 characters with spaces divided with |
result = {"ํํ์":token, "์์ ๋ผ๋ฒจ":label}
list_result.append(result)
df = pd.DataFrame(list_result)
# remove first and last row
df = df.iloc[1:-1]
return df
def convert_df(df:pd.DataFrame):
return df.to_csv(index=False).encode('utf-8')
def convert_json(df:pd.DataFrame):
result = df.to_json(orient="index")
parsed = json.loads(result)
json_string = json.dumps(parsed)
#st.json(json_string, expanded=True)
return json_string
filtering_map = {
"์ฃผ์ ์ ๋ณด": "[์ฃผ์]",
"์งํ ์ ๋ณด": "[์งํ]",
"๋ ์ง ์ ๋ณด": "[๋ ์ง]",
"์ฅ์ ์ ๋ณด": "[์ฅ์]",
"๊ธฐ๊ด ์ ๋ณด": "[๊ธฐ๊ด]",
"์ธ๋ช
/๋ณ๋ช
์ ๋ณด": "[์ด๋ฆ]",
"์๋ ์ ๋ณด": "[์๋]",
"๊ด๊ณ ์ ๋ณด": "[๊ด๊ณ]",
"๋น๋ฏผ๊ฐ ์ ๋ณด": "[๋น๋ฏผ๊ฐ]"
}
if src_text == "":
st.warning("Please **enter text** for translation")
else:
df_result = yield_df(src_text)
st.markdown("### ํํฐ๋ง ๋ ๋ฌธ์ฅ")
display_result = ""
for index, row in df_result.iterrows():
token_info = row["ํํ์"]
label_info = row["์์ ๋ผ๋ฒจ"]
if label_info != "๋น๋ฏผ๊ฐ ์ ๋ณด":
token_info = filtering_map[label_info]
if "##" in token_info:
token_info = token_info.replace("##", "")
else:
token_info = " " + token_info
display_result += token_info
st.write(display_result)
st.markdown("### ๋ถ๋ฅ๋ ๋จ์ด๋ค")
st.header("")
cs, c1, c2, c3, cLast = st.columns([0.75, 1.5, 1.5, 1.5, 0.75])
st.table(df_result)
with c1:
#csvbutton = download_button(results, "results.csv", "๐ฅ Download .csv")
csvbutton = st.download_button(label="๐ฅ csv๋ก ๋ค์ด๋ก๋", data=convert_df(df_result), file_name= "results.csv", mime='text/csv', key='csv')
with c2:
#textbutton = download_button(results, "results.txt", "๐ฅ Download .txt")
textbutton = st.download_button(label="๐ฅ txt๋ก ๋ค์ด๋ก๋", data=convert_df(df_result), file_name= "results.text", mime='text/plain', key='text')
with c3:
#jsonbutton = download_button(results, "results.json", "๐ฅ Download .json")
jsonbutton = st.download_button(label="๐ฅ json์ผ๋ก ๋ค์ด๋ก๋", data=convert_json(df_result), file_name= "results.json", mime='application/json', key='json')
with st.expander("(์ฃผ) ์ฟผ์นด์์ด์์ด ๋ฐ๋ชจ ์ฌ์ฌ ๊ด๋ จ", expanded=True):
st.write(
"""
ํด๋น ๋ฐ๋ชจ๋ 2022๋
๋ ๊ณผํ๊ธฐ์ ์ ๋ณดํต์ ๋ถ์ ์ฌ์์ผ๋ก ์ ๋ณดํต์ ์ฐ์
์งํฅ์์ ์ง์์ ๋ฐ์ ์ํ๋ ์ฐ๊ตฌ์
(๊ณผ์ ๋ฒํธ: A1504-22-1005)
"""
) |