Upload 2 files
Browse files- app.py +28 -23
- requirements.txt +1 -4
app.py
CHANGED
@@ -1,37 +1,42 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
|
|
|
|
3 |
|
4 |
-
# 모델
|
5 |
-
|
6 |
-
|
7 |
-
)
|
8 |
-
|
9 |
-
# 분석 함수
|
10 |
-
def absa(text, aspect_input):
|
11 |
-
aspects = [a.strip() for a in aspect_input.split(",") if a.strip()]
|
12 |
-
pairs = [[text, aspect] for aspect in aspects]
|
13 |
|
14 |
-
|
15 |
-
inference_source=[text],
|
16 |
-
aspect_sentiment_pair_list=pairs
|
17 |
-
)
|
18 |
|
|
|
|
|
|
|
19 |
output = ""
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
output += f"- **{aspect}** → **{sentiment}**\n"
|
|
|
23 |
return output
|
24 |
|
25 |
-
# Gradio 인터페이스
|
26 |
iface = gr.Interface(
|
27 |
-
fn=
|
28 |
inputs=[
|
29 |
-
gr.Textbox(label="문장 입력", placeholder="예: The battery is good
|
30 |
-
gr.Textbox(label="속성
|
31 |
],
|
32 |
-
outputs=gr.Markdown(label="
|
33 |
-
title="ABSA 감정 분석기
|
34 |
-
description="
|
35 |
)
|
36 |
|
37 |
iface.launch()
|
|
|
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 = "yangheng/deberta-v3-base-absa-v1.1"
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
9 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
+
labels = ["Negative", "Neutral", "Positive"]
|
|
|
|
|
|
|
12 |
|
13 |
+
# 분석 함수
|
14 |
+
def analyze_sentiment(sentence, aspects_text):
|
15 |
+
aspects = [a.strip() for a in aspects_text.split(",") if a.strip()]
|
16 |
output = ""
|
17 |
+
|
18 |
+
for aspect in aspects:
|
19 |
+
combined = f"{sentence} [ASP] {aspect}"
|
20 |
+
inputs = tokenizer(combined, return_tensors="pt", truncation=True)
|
21 |
+
with torch.no_grad():
|
22 |
+
outputs = model(**inputs)
|
23 |
+
probs = F.softmax(outputs.logits, dim=1)
|
24 |
+
pred = torch.argmax(probs, dim=1).item()
|
25 |
+
sentiment = labels[pred]
|
26 |
output += f"- **{aspect}** → **{sentiment}**\n"
|
27 |
+
|
28 |
return output
|
29 |
|
30 |
+
# Gradio 인터페이스
|
31 |
iface = gr.Interface(
|
32 |
+
fn=analyze_sentiment,
|
33 |
inputs=[
|
34 |
+
gr.Textbox(label="문장 입력", placeholder="예: The battery is good but the screen is dim."),
|
35 |
+
gr.Textbox(label="속성 목록 (쉼표로 구분)", placeholder="예: battery, screen")
|
36 |
],
|
37 |
+
outputs=gr.Markdown(label="감정 분석 결과"),
|
38 |
+
title="ABSA 감정 분석기",
|
39 |
+
description="Hugging Face Transformers 모델 사용"
|
40 |
)
|
41 |
|
42 |
iface.launch()
|
requirements.txt
CHANGED
@@ -1,6 +1,3 @@
|
|
1 |
gradio
|
2 |
-
git+https://github.com/yangheng95/ABSAgym
|
3 |
transformers==4.28.1
|
4 |
-
|
5 |
-
findfile
|
6 |
-
pytorch_lightning==1.9.5
|
|
|
1 |
gradio
|
|
|
2 |
transformers==4.28.1
|
3 |
+
torch
|
|
|
|