Jiyoung1218 commited on
Commit
be0950c
·
verified ·
1 Parent(s): e92451d

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +87 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ==========================================
2
+ # Hugging Face 모델 사용 - 감정 분석 Gradio
3
+ # ==========================================
4
+
5
+ import gradio as gr
6
+ import torch
7
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
8
+ from peft import PeftModel
9
+
10
+ # 모델 로드
11
+ print("모델 로드 중...")
12
+
13
+ BASE_MODEL = "klue/bert-base"
14
+ LORA_MODEL = "Jiyoung1218/nsmc-sentiment-lora" # 여러분의 Model
15
+
16
+ tokenizer = AutoTokenizer.from_pretrained(LORA_MODEL)
17
+ base_model = AutoModelForSequenceClassification.from_pretrained(
18
+ BASE_MODEL,
19
+ num_labels=2
20
+ )
21
+ model = PeftModel.from_pretrained(base_model, LORA_MODEL)
22
+ model.eval()
23
+
24
+ device = "cuda" if torch.cuda.is_available() else "cpu"
25
+ model.to(device)
26
+
27
+ print(f"완료! (Device: {device})")
28
+
29
+ # 감정 분석 함수
30
+ def analyze_sentiment(text):
31
+ if not text.strip():
32
+ return "텍스트를 입력해주세요", {}
33
+
34
+ # 토크나이징
35
+ inputs = tokenizer(
36
+ text,
37
+ return_tensors="pt",
38
+ truncation=True,
39
+ max_length=128,
40
+ padding=True
41
+ ).to(device)
42
+
43
+ # 예측
44
+ with torch.no_grad():
45
+ outputs = model(**inputs)
46
+ probs = torch.softmax(outputs.logits, dim=-1)[0]
47
+
48
+ # 결과
49
+ pred = torch.argmax(probs).item()
50
+ label = "😊 긍정" if pred == 1 else "😞 부정"
51
+ confidence = probs[pred].item()
52
+
53
+ result = f"**{label}** (확신도: {confidence*100:.1f}%)"
54
+
55
+ prob_dict = {
56
+ "😞 부정": float(probs[0]),
57
+ "😊 긍정": float(probs[1])
58
+ }
59
+
60
+ return result, prob_dict
61
+
62
+ # Gradio UI
63
+ demo = gr.Interface(
64
+ fn=analyze_sentiment,
65
+ inputs=gr.Textbox(
66
+ label="영화 리뷰",
67
+ placeholder="영화에 대한 리뷰를 입력하세요...",
68
+ lines=3
69
+ ),
70
+ outputs=[
71
+ gr.Markdown(label="분석 결과"),
72
+ gr.Label(label="감정 확률", num_top_classes=2)
73
+ ],
74
+ title="영화 리뷰 감정 분석",
75
+ description="LoRA로 파인튜닝된 NSMC 감정 분석 모델입니다.",
76
+ examples=[
77
+ ["정말 재미있는 영화였어요! 강력 추천합니다."],
78
+ ["시간 낭비였습니다. 별로였어요."],
79
+ ["배우들의 연기가 훌륭했습니다."],
80
+ ["스토리가 지루하고 재미없었어요."],
81
+ ],
82
+ theme="soft",
83
+ allow_flagging="never"
84
+ )
85
+
86
+ # 실행
87
+ demo.launch(share=True, debug=True)
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ transformers
2
+ peft
3
+ torch