File size: 2,016 Bytes
3d9fdfd
 
 
 
 
 
 
 
8f8977c
3d9fdfd
 
 
 
7e3b6e0
3d9fdfd
 
 
 
 
 
 
 
 
52d58f7
3d9fdfd
52d58f7
3d9fdfd
 
2c289f8
8f8977c
a9ced73
3d9fdfd
 
eb530e9
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
# ์ถ”๋ก 
from transformers import RobertaTokenizer, RobertaForSequenceClassification
import torch
import streamlit as st

# ์ „์ดํ•™์Šต์— ์‚ฌ์šฉํ•œ ํ† ํฌ๋‚˜์ด์ €์™€ ๋ชจ๋ธ ๋กœ๋“œ & ๊ฐ€์ค‘์น˜ ๋กœ๋“œ
tokenizer = RobertaTokenizer.from_pretrained('beomi/KcBERT-v2023')
model = RobertaForSequenceClassification.from_pretrained('beomi/KcBERT-v2023', num_labels=2)
model.load_state_dict(torch.load("pytorchmodel_518๋ง์–ธ๋ถ„๋ฅ˜_acc8583.bin", map_location=torch.device('cpu')))
# ๋ชจ๋ธ์„ ํ‰๊ฐ€ ๋ชจ๋“œ๋กœ ์„ค์ •
model.eval()

# ์ž…๋ ฅ ํ…์ŠคํŠธ ์˜ˆ์‹œ
class_labels = ["๋ฌธ์ œ์—†์Œ/๊ด€๋ จ์—†์Œ", "๋ถ€์ ์ ˆ(518 ๋ง์–ธ ๊ฐ€๋Šฅ)"]
def inference(new_text):
    inputs = tokenizer(new_text, return_tensors="pt")
    # ์ถ”๋ก  ์ˆ˜ํ–‰ (CPU ์‚ฌ์šฉ)
    with torch.no_grad():
        outputs = model(**inputs)
        logits = outputs.logits
    probs = torch.nn.functional.softmax(logits, dim=-1)
    predicted_class = torch.argmax(probs, dim=1).item()
    predicted_label = class_labels[predicted_class]
    unpredicted_label = class_labels[1-predicted_class]
    probability = probs[0][predicted_class].item()
    return f"{predicted_label}:{probability*100:.2f}%, {unpredicted_label}:{((1-probability)*100):.2f}%"

# Streamlit interface
st.markdown('### 5ยท18 ๋ฏผ์ฃผํ™”์šด๋™ ๊ด€๋ จ ๋ถ€์ ์ ˆํ•œ ๋ฐœ์–ธ ํŒ๋‹จ(๋ฒ ํƒ€๋ฒ„์ „)')
st.markdown('<small style="color:grey;">5ยท18 ๋ฏผ์ฃผํ™”์šด๋™๊ณผ ๊ด€๋ จํ•ด ๋ฌด์žฅ ํญ๋™, ๋ถํ•œ๊ตฐ ๊ฐœ์ž…, ๊ฐ€์งœ ์œ ๊ณต์ž ๋“ฑ ๋ถ€์ ์ ˆํ•œ ์–ธ๊ธ‰๊ณผ ์ง€์—ญ-์ด๋…์— ๋Œ€ํ•œ ํ˜์˜ค์„ฑ ๋ฐœ์–ธ์ด ๋ฌธ์ œ๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์— ๋ฌธ์žฅ์„ ์ž…๋ ฅํ•˜๋ฉด ์ด๋Ÿฌํ•œ ๋‚ด์šฉ์„ ์ค‘์‹ฌ์œผ๋กœ "๋ฌธ์ œ์—†์Œ/๊ด€๋ จ์—†์Œ" ๋˜๋Š” "๋ถ€์ ์ ˆ(518 ๋ง์–ธ ๊ฐ€๋Šฅ)"๋กœ ํŒ๋‹จํ•ด ๋“œ๋ฆฝ๋‹ˆ๋‹ค. ์˜ˆ์ธก ๋ชจ๋ธ์˜ ์ •ํ™•๋„๋Š” 85.83%๋กœ, ์ผ๋ถ€ ๋ถ€์ •ํ™•ํ•œ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค </small>', unsafe_allow_html=True)
user_input = st.text_area("์ด ๊ณณ์— ๋ฌธ์žฅ ์ž…๋ ฅ(100์ž ์ดํ•˜ ๊ถŒ์žฅ, ๋„ˆ๋ฌด ๊ธธ๋ฉด ๋ถ„์„ ๋ถˆ๊ฐ€):")
if st.button('์‹œ์ž‘'):
    result = inference(user_input)
    st.write(result)