AmiKim commited on
Commit
5099da2
·
verified ·
1 Parent(s): c0667c5

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -0
app.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
+ import torch
4
+ import torch.nn.functional as F
5
+
6
+ # 모델 불러오기
7
+ model_name = "hun3359/mdistilbertV3.1-sentiment"
8
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
9
+ model = AutoModelForSequenceClassification.from_pretrained(model_name)
10
+
11
+ # 모델 config에서 라벨 추출 (id2label이 없으면 기본값 사용)
12
+ if hasattr(model.config, "id2label"):
13
+ labels = [label for _, label in sorted(model.config.id2label.items())]
14
+ else:
15
+ # fallback: default labels
16
+ labels = ['기쁨', '분노', '불안', '슬픔', '중립']
17
+
18
+ def predict_sentiment(text):
19
+ if not text.strip():
20
+ return "텍스트를 입력해주세요."
21
+
22
+ inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
23
+ with torch.no_grad():
24
+ outputs = model(**inputs)
25
+ probs = F.softmax(outputs.logits, dim=1)
26
+ pred = torch.argmax(probs, dim=1).item()
27
+ confidence = probs[0][pred].item()
28
+
29
+ if pred < len(labels):
30
+ return f"감정: {labels[pred]} ({confidence:.2%} 확신)"
31
+ else:
32
+ return f"감정 인식 실패 (index {pred})"
33
+
34
+ # Gradio 인터페이스
35
+ gr.Interface(
36
+ fn=predict_sentiment,
37
+ inputs="text",
38
+ outputs="text",
39
+ title="한국어 감정 분석기",
40
+ description="문장을 입력하면 감정을 분석해 드립니다. (모델: mdistilbertV3.1)"
41
+ ).launch()