jhj0423's picture
Update app.py
d6876be verified
# ==========================================
# Hugging Face λͺ¨λΈ μ‚¬μš© - 감정 뢄석 Gradio
# ==========================================
import gradio as gr
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification
from peft import PeftModel
# λͺ¨λΈ λ‘œλ“œ
print("λͺ¨λΈ λ‘œλ“œ 쀑...")
BASE_MODEL = "klue/bert-base"
LORA_MODEL = "jhj0423/nsmc-sentiment-lora" # μ—¬λŸ¬λΆ„μ˜ Model
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)