hai2131 commited on
Commit
077f60f
·
verified ·
1 Parent(s): 20bff11

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -0
app.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
+ from transformers import pipeline
4
+ import torch
5
+ import torch.nn.functional as F
6
+
7
+ @st.cache_resource
8
+ def load_model():
9
+ absa_tokenizer = AutoTokenizer.from_pretrained("yangheng/deberta-v3-base-absa-v1.1")
10
+ absa_model = AutoModelForSequenceClassification.from_pretrained("yangheng/deberta-v3-base-absa-v1.1")
11
+ token_classifier = pipeline(
12
+ model="thainq107/abte-restaurants-distilbert-base-uncased",
13
+ aggregation_strategy="simple"
14
+ )
15
+ return absa_model, absa_tokenizer, token_classifier
16
+
17
+ absa_model, absa_tokenizer, token_classifier = load_model()
18
+
19
+ def inference(review):
20
+ aspects = token_classifier(review)
21
+ aspects = [aspect['word'] for aspect in aspects]
22
+ results = {}
23
+ for aspect in aspects:
24
+ # Check if the aspect is mentioned in the review
25
+ if aspect.lower() in review.lower():
26
+ inputs = absa_tokenizer(f"[CLS] {review} [SEP] {aspect} [SEP]", return_tensors="pt")
27
+ outputs = absa_model(**inputs)
28
+ probs = F.softmax(outputs.logits, dim=1)
29
+ probs = probs.detach().numpy()[0]
30
+
31
+ # Extract the label with the highest probability
32
+ max_label, max_prob = max(zip(["negative", "neutral", "positive"], probs), key=lambda x: x[1])
33
+
34
+ results[aspect] = max_label
35
+
36
+ return results
37
+
38
+ st.title("ABSA - Aspect-Based Sentiment Analysis")
39
+
40
+ text = st.text_area("Nhập câu cần phân tích:", "The battery life is great, but the screen is dim.")
41
+
42
+ if st.button("Phân tích cảm xúc"):
43
+ results = inference(text)
44
+ for aspect, label in results.items():
45
+ st.markdown(f"{aspect} ➝ {label}")