yjpark75 commited on
Commit
8a6fc08
·
verified ·
1 Parent(s): 1bbc69a

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +28 -23
  2. requirements.txt +1 -4
app.py CHANGED
@@ -1,37 +1,42 @@
1
  import gradio as gr
2
- from pyabsa import AspectSentimentClassification as ASC
 
 
3
 
4
- # 모델 로드
5
- aspect_extractor = ASC.SentimentClassifier(
6
- checkpoint='yangheng/deberta-v3-base-absa-v1.1'
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
- results = aspect_extractor.infer(
15
- inference_source=[text],
16
- aspect_sentiment_pair_list=pairs
17
- )
18
 
 
 
 
19
  output = ""
20
- for res in results:
21
- for aspect, sentiment in res["aspect_sentiment_pair"]:
 
 
 
 
 
 
 
22
  output += f"- **{aspect}** → **{sentiment}**\n"
 
23
  return output
24
 
25
- # Gradio 인터페이스 생성
26
  iface = gr.Interface(
27
- fn=absa,
28
  inputs=[
29
- gr.Textbox(label="문장 입력", placeholder="예: The battery is good, but the screen is dim."),
30
- gr.Textbox(label="속성 입력 (쉼표로 구분)", placeholder="예: battery, screen")
31
  ],
32
- outputs=gr.Markdown(label="속성별 감정 분석 결과"),
33
- title="ABSA 감정 분석기 (DeBERTa v3)",
34
- description="문장과 속성을 입력하면 속성별 감정을 추출합니다. 모델: yangheng/deberta-v3-base-absa-v1.1"
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
- protobuf==4.25.2
5
- findfile
6
- pytorch_lightning==1.9.5
 
1
  gradio
 
2
  transformers==4.28.1
3
+ torch