File size: 2,398 Bytes
11075d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# ==========================================
# Hugging Face λͺ¨λΈ μ‚¬μš© - 감정 뢄석 Gradio
# ==========================================

# !pip install -q gradio transformers peft

import gradio as gr
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification
from peft import PeftModel

# λͺ¨λΈ λ‘œλ“œ
print("λͺ¨λΈ λ‘œλ“œ 쀑...")

BASE_MODEL = "klue/bert-base"
LORA_MODEL = "JINIIII/nsmc-sentiment-lora"

tokenizer = AutoTokenizer.from_pretrained(LORA_MODEL)
base_model = AutoModelForSequenceClassification.from_pretrained(
    BASE_MODEL,
    num_labels=2
)
model = PeftModel.from_pretrained(base_model, LORA_MODEL)
model.eval()

device = "cuda" if torch.cuda.is_available() else "cpu"
model.to(device)

print(f"μ™„λ£Œ! (Device: {device})")

# 감정 뢄석 ν•¨μˆ˜
def analyze_sentiment(text):
    if not text.strip():
        return "ν…μŠ€νŠΈλ₯Ό μž…λ ₯ν•΄μ£Όμ„Έμš”", {}
    
    # ν† ν¬λ‚˜μ΄μ§•
    inputs = tokenizer(
        text,
        return_tensors="pt",
        truncation=True,
        max_length=128,
        padding=True
    ).to(device)
    
    # 예츑
    with torch.no_grad():
        outputs = model(**inputs)
        probs = torch.softmax(outputs.logits, dim=-1)[0]
    
    # κ²°κ³Ό
    pred = torch.argmax(probs).item()
    label = "😊 긍정" if pred == 1 else "😞 λΆ€μ •"
    confidence = probs[pred].item()
    
    result = f"**{label}** (확신도: {confidence*100:.1f}%)"
    
    prob_dict = {
        "😞 λΆ€μ •": float(probs[0]),
        "😊 긍정": float(probs[1])
    }
    
    return result, prob_dict

# Gradio UI
demo = gr.Interface(
    fn=analyze_sentiment,
    inputs=gr.Textbox(
        label="μ˜ν™” 리뷰",
        placeholder="μ˜ν™”μ— λŒ€ν•œ 리뷰λ₯Ό μž…λ ₯ν•˜μ„Έμš”...",
        lines=3
    ),
    outputs=[
        gr.Markdown(label="뢄석 κ²°κ³Ό"),
        gr.Label(label="감정 ν™•λ₯ ", num_top_classes=2)
    ],
    title="μ˜ν™” 리뷰 감정 뢄석",
    description="LoRA둜 νŒŒμΈνŠœλ‹λœ NSMC 감정 뢄석 λͺ¨λΈμž…λ‹ˆλ‹€.",
    examples=[
        ["정말 μž¬λ―ΈμžˆλŠ” μ˜ν™”μ˜€μ–΄μš”! κ°•λ ₯ μΆ”μ²œν•©λ‹ˆλ‹€."],
        ["μ‹œκ°„ λ‚­λΉ„μ˜€μŠ΅λ‹ˆλ‹€. λ³„λ‘œμ˜€μ–΄μš”."],
        ["λ°°μš°λ“€μ˜ μ—°κΈ°κ°€ ν›Œλ₯­ν–ˆμŠ΅λ‹ˆλ‹€."],
        ["μŠ€ν† λ¦¬κ°€ μ§€λ£¨ν•˜κ³  μž¬λ―Έμ—†μ—ˆμ–΄μš”."],
    ],
    theme="soft",
    allow_flagging="never"
)

# μ‹€ν–‰
demo.launch(share=True, debug=True)