Foxy0505 commited on
Commit
e2b8a26
·
1 Parent(s): 32dba56

change the main language

Browse files
Files changed (1) hide show
  1. app.py +29 -24
app.py CHANGED
@@ -4,15 +4,19 @@ import gradio as gr
4
  from transformers import AutoTokenizer, AutoModelForSequenceClassification
5
  from bertopic import BERTopic
6
  from sentence_transformers import SentenceTransformer
 
 
 
 
 
7
 
8
- # --- 1. CẤU HÌNH ĐƯỜNG DẪN ---
9
  ROBERTA_PATH = "roberta_base_checkpoint"
10
  BERTOPIC_PATH = "bertopic_checkpoint"
11
 
12
- # --- 2. KHỞI TẠO MODEL (Chạy 1 lần duy nhất khi startup) ---
13
- print("--- Đang khởi động hệ thống ---")
14
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
15
- print(f"Thiết bị đang dùng: {device.type.upper()}")
16
 
17
  # Load RoBERTa
18
  tokenizer = AutoTokenizer.from_pretrained(ROBERTA_PATH)
@@ -23,12 +27,12 @@ roberta_model.eval()
23
  embedding_model = SentenceTransformer("all-MiniLM-L6-v2")
24
  topic_model = BERTopic.load(BERTOPIC_PATH, embedding_model=embedding_model)
25
 
26
- print("--- Tất cả model đã sẵn sàng! ---")
27
 
28
- # --- 3. LOGIC XỬ LÝ CHÍNH ---
29
  def analyze_review(text):
30
  if not text.strip():
31
- return "Vui lòng nhập văn bản", None, None, ""
32
 
33
  # --- Sentiment Analysis (RoBERTa) ---
34
  inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=256).to(device)
@@ -38,61 +42,62 @@ def analyze_review(text):
38
  probs = F.softmax(outputs.logits, dim=1)
39
  pred_idx = torch.argmax(probs, dim=1).item()
40
 
 
41
  sentiment_labels = ['Negative 😡', 'Neutral 😐', 'Positive 😊']
42
  sentiment = sentiment_labels[pred_idx]
43
  confidence = probs[0][pred_idx].item()
44
 
45
  # --- Topic Modeling (BERTopic) ---
46
- # BERTopic transform nhận vào 1 list
47
  new_topics, _ = topic_model.transform([text])
48
  topic_id = int(new_topics[0])
49
 
50
  if topic_id != -1:
51
  keywords_data = topic_model.get_topic(topic_id)
52
- # Lấy top 5 từ khóa chính
53
  keywords_list = ", ".join([word for word, score in keywords_data[:5]])
54
  else:
55
- keywords_list = "Outlier (Không xác định được chủ đề cụ thể)"
56
 
57
  return sentiment, f"{confidence:.2%}", topic_id, keywords_list
58
 
59
- # --- 4. GIAO DIỆN GRADIO (WEB & API) ---
60
- with gr.Blocks(title="NLP Analysis Tool") as demo:
61
- gr.Markdown("# 📊 Hệ thống Phân tích Review")
62
- gr.Markdown("Kết hợp **RoBERTa** (Sentiment) và **BERTopic** (Topic Extraction).")
63
 
64
  with gr.Row():
65
  with gr.Column():
66
  input_text = gr.Textbox(
67
- label="Nhập câu Review",
68
- placeholder="Ví dụ: The pizza was delicious but the delivery took two hours!",
69
  lines=5
70
  )
71
- btn = gr.Button("Phân tích ngay", variant="primary")
72
 
73
  with gr.Column():
74
- out_sentiment = gr.Label(label="Cảm xúc (Sentiment)")
75
- out_confidence = gr.Textbox(label="Độ tin cậy")
76
  with gr.Row():
77
  out_topic_id = gr.Number(label="Topic ID")
78
- out_keywords = gr.Textbox(label="Từ khóa chủ đề")
79
 
80
- # Gán sự kiện cho nút bấm
81
  btn.click(
82
  fn=analyze_review,
83
  inputs=input_text,
84
  outputs=[out_sentiment, out_confidence, out_topic_id, out_keywords]
85
  )
86
 
87
- # Ví dụ mẫu
88
  gr.Examples(
89
  examples=[
90
  ["The food was amazing, very delicious!"],
91
- ["Service was very slow and the waiter was rude."]
 
92
  ],
93
  inputs=input_text
94
  )
95
 
96
- # Chạy ứng dụng
97
  if __name__ == "__main__":
98
  demo.launch()
 
4
  from transformers import AutoTokenizer, AutoModelForSequenceClassification
5
  from bertopic import BERTopic
6
  from sentence_transformers import SentenceTransformer
7
+ import os
8
+
9
+ # --- 1. CONFIGURATION ---
10
+ # Tránh xung đột luồng khi chạy tokenizer
11
+ os.environ["TOKENIZERS_PARALLELISM"] = "false"
12
 
 
13
  ROBERTA_PATH = "roberta_base_checkpoint"
14
  BERTOPIC_PATH = "bertopic_checkpoint"
15
 
16
+ # --- 2. INITIALIZATION (Runs once at startup) ---
17
+ print("--- System Initializing ---")
18
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
19
+ print(f"Device detected: {device.type.upper()}")
20
 
21
  # Load RoBERTa
22
  tokenizer = AutoTokenizer.from_pretrained(ROBERTA_PATH)
 
27
  embedding_model = SentenceTransformer("all-MiniLM-L6-v2")
28
  topic_model = BERTopic.load(BERTOPIC_PATH, embedding_model=embedding_model)
29
 
30
+ print("--- All models loaded successfully! ---")
31
 
32
+ # --- 3. CORE LOGIC ---
33
  def analyze_review(text):
34
  if not text.strip():
35
+ return "Please enter some text", None, None, ""
36
 
37
  # --- Sentiment Analysis (RoBERTa) ---
38
  inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=256).to(device)
 
42
  probs = F.softmax(outputs.logits, dim=1)
43
  pred_idx = torch.argmax(probs, dim=1).item()
44
 
45
+ # Matching common sentiment output formats
46
  sentiment_labels = ['Negative 😡', 'Neutral 😐', 'Positive 😊']
47
  sentiment = sentiment_labels[pred_idx]
48
  confidence = probs[0][pred_idx].item()
49
 
50
  # --- Topic Modeling (BERTopic) ---
 
51
  new_topics, _ = topic_model.transform([text])
52
  topic_id = int(new_topics[0])
53
 
54
  if topic_id != -1:
55
  keywords_data = topic_model.get_topic(topic_id)
56
+ # Extracting top 5 keywords
57
  keywords_list = ", ".join([word for word, score in keywords_data[:5]])
58
  else:
59
+ keywords_list = "Outlier (No specific topic detected)"
60
 
61
  return sentiment, f"{confidence:.2%}", topic_id, keywords_list
62
 
63
+ # --- 4. GRADIO INTERFACE (WEB & API) ---
64
+ with gr.Blocks(title="Food Review Analysis Tool") as demo:
65
+ gr.Markdown("# 📊 Food Review Analysis System")
66
+ gr.Markdown("An integrated pipeline using **RoBERTa** for Sentiment and **BERTopic** for Topic Extraction.")
67
 
68
  with gr.Row():
69
  with gr.Column():
70
  input_text = gr.Textbox(
71
+ label="Review Content",
72
+ placeholder="e.g., The pizza was delicious but the delivery took two hours!",
73
  lines=5
74
  )
75
+ btn = gr.Button("Analyze Now", variant="primary")
76
 
77
  with gr.Column():
78
+ out_sentiment = gr.Label(label="Sentiment Prediction")
79
+ out_confidence = gr.Textbox(label="Confidence Score")
80
  with gr.Row():
81
  out_topic_id = gr.Number(label="Topic ID")
82
+ out_keywords = gr.Textbox(label="Main Keywords")
83
 
84
+ # Event binding
85
  btn.click(
86
  fn=analyze_review,
87
  inputs=input_text,
88
  outputs=[out_sentiment, out_confidence, out_topic_id, out_keywords]
89
  )
90
 
91
+ # Examples for quick testing
92
  gr.Examples(
93
  examples=[
94
  ["The food was amazing, very delicious!"],
95
+ ["Service was very slow and the waiter was rude."],
96
+ ["While the truffle-infused risotto was a masterpiece, the deafening noise made it hard to enjoy."]
97
  ],
98
  inputs=input_text
99
  )
100
 
101
+ # Launch the app
102
  if __name__ == "__main__":
103
  demo.launch()