Spaces:
Paused
Paused
initial commit
Browse files- .gitattributes +1 -0
- app.py +98 -0
- bertopic_checkpoint/config.json +16 -0
- bertopic_checkpoint/topic_embeddings.safetensors +3 -0
- bertopic_checkpoint/topics.json +0 -0
- requirements.txt +6 -0
- roberta_base_checkpoint/config.json +38 -0
- roberta_base_checkpoint/merges.txt +0 -0
- roberta_base_checkpoint/model.safetensors +3 -0
- roberta_base_checkpoint/special_tokens_map.json +51 -0
- roberta_base_checkpoint/tokenizer.json +0 -0
- roberta_base_checkpoint/tokenizer_config.json +58 -0
- roberta_base_checkpoint/training_args.bin +3 -0
- roberta_base_checkpoint/vocab.json +0 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
model.safetensors filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn.functional as F
|
| 3 |
+
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)
|
| 19 |
+
roberta_model = AutoModelForSequenceClassification.from_pretrained(ROBERTA_PATH).to(device)
|
| 20 |
+
roberta_model.eval()
|
| 21 |
+
|
| 22 |
+
# Load BERTopic
|
| 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)
|
| 35 |
+
with torch.no_grad():
|
| 36 |
+
outputs = roberta_model(**inputs)
|
| 37 |
+
|
| 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()
|
bertopic_checkpoint/config.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"calculate_probabilities": true,
|
| 3 |
+
"language": null,
|
| 4 |
+
"low_memory": false,
|
| 5 |
+
"min_topic_size": 10,
|
| 6 |
+
"n_gram_range": [
|
| 7 |
+
1,
|
| 8 |
+
1
|
| 9 |
+
],
|
| 10 |
+
"nr_topics": null,
|
| 11 |
+
"seed_topic_list": null,
|
| 12 |
+
"top_n_words": 10,
|
| 13 |
+
"verbose": true,
|
| 14 |
+
"zeroshot_min_similarity": 0.7,
|
| 15 |
+
"zeroshot_topic_list": null
|
| 16 |
+
}
|
bertopic_checkpoint/topic_embeddings.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:4428bf7c815ebe88028082cad3b402dcd2bac7cbab97ab9df54b4b3c3cfba846
|
| 3 |
+
size 1287264
|
bertopic_checkpoint/topics.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
transformers
|
| 3 |
+
bertopic
|
| 4 |
+
sentence-transformers
|
| 5 |
+
gradio
|
| 6 |
+
numpy
|
roberta_base_checkpoint/config.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_name_or_path": "cardiffnlp/twitter-roberta-base-sentiment",
|
| 3 |
+
"architectures": [
|
| 4 |
+
"RobertaForSequenceClassification"
|
| 5 |
+
],
|
| 6 |
+
"attention_probs_dropout_prob": 0.2,
|
| 7 |
+
"bos_token_id": 0,
|
| 8 |
+
"classifier_dropout": null,
|
| 9 |
+
"eos_token_id": 2,
|
| 10 |
+
"gradient_checkpointing": false,
|
| 11 |
+
"hidden_act": "gelu",
|
| 12 |
+
"hidden_dropout_prob": 0.2,
|
| 13 |
+
"hidden_size": 768,
|
| 14 |
+
"id2label": {
|
| 15 |
+
"0": "LABEL_0",
|
| 16 |
+
"1": "LABEL_1",
|
| 17 |
+
"2": "LABEL_2"
|
| 18 |
+
},
|
| 19 |
+
"initializer_range": 0.02,
|
| 20 |
+
"intermediate_size": 3072,
|
| 21 |
+
"label2id": {
|
| 22 |
+
"LABEL_0": 0,
|
| 23 |
+
"LABEL_1": 1,
|
| 24 |
+
"LABEL_2": 2
|
| 25 |
+
},
|
| 26 |
+
"layer_norm_eps": 1e-05,
|
| 27 |
+
"max_position_embeddings": 514,
|
| 28 |
+
"model_type": "roberta",
|
| 29 |
+
"num_attention_heads": 12,
|
| 30 |
+
"num_hidden_layers": 12,
|
| 31 |
+
"pad_token_id": 1,
|
| 32 |
+
"position_embedding_type": "absolute",
|
| 33 |
+
"torch_dtype": "float32",
|
| 34 |
+
"transformers_version": "4.40.1",
|
| 35 |
+
"type_vocab_size": 1,
|
| 36 |
+
"use_cache": true,
|
| 37 |
+
"vocab_size": 50265
|
| 38 |
+
}
|
roberta_base_checkpoint/merges.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
roberta_base_checkpoint/model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a32b10c5c2396c22944524df4bb598c1c34782b2b14b9b27ed442622c11b4711
|
| 3 |
+
size 498615900
|
roberta_base_checkpoint/special_tokens_map.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"bos_token": {
|
| 3 |
+
"content": "<s>",
|
| 4 |
+
"lstrip": false,
|
| 5 |
+
"normalized": true,
|
| 6 |
+
"rstrip": false,
|
| 7 |
+
"single_word": false
|
| 8 |
+
},
|
| 9 |
+
"cls_token": {
|
| 10 |
+
"content": "<s>",
|
| 11 |
+
"lstrip": false,
|
| 12 |
+
"normalized": true,
|
| 13 |
+
"rstrip": false,
|
| 14 |
+
"single_word": false
|
| 15 |
+
},
|
| 16 |
+
"eos_token": {
|
| 17 |
+
"content": "</s>",
|
| 18 |
+
"lstrip": false,
|
| 19 |
+
"normalized": true,
|
| 20 |
+
"rstrip": false,
|
| 21 |
+
"single_word": false
|
| 22 |
+
},
|
| 23 |
+
"mask_token": {
|
| 24 |
+
"content": "<mask>",
|
| 25 |
+
"lstrip": true,
|
| 26 |
+
"normalized": false,
|
| 27 |
+
"rstrip": false,
|
| 28 |
+
"single_word": false
|
| 29 |
+
},
|
| 30 |
+
"pad_token": {
|
| 31 |
+
"content": "<pad>",
|
| 32 |
+
"lstrip": false,
|
| 33 |
+
"normalized": true,
|
| 34 |
+
"rstrip": false,
|
| 35 |
+
"single_word": false
|
| 36 |
+
},
|
| 37 |
+
"sep_token": {
|
| 38 |
+
"content": "</s>",
|
| 39 |
+
"lstrip": false,
|
| 40 |
+
"normalized": true,
|
| 41 |
+
"rstrip": false,
|
| 42 |
+
"single_word": false
|
| 43 |
+
},
|
| 44 |
+
"unk_token": {
|
| 45 |
+
"content": "<unk>",
|
| 46 |
+
"lstrip": false,
|
| 47 |
+
"normalized": true,
|
| 48 |
+
"rstrip": false,
|
| 49 |
+
"single_word": false
|
| 50 |
+
}
|
| 51 |
+
}
|
roberta_base_checkpoint/tokenizer.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
roberta_base_checkpoint/tokenizer_config.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"add_prefix_space": false,
|
| 3 |
+
"added_tokens_decoder": {
|
| 4 |
+
"0": {
|
| 5 |
+
"content": "<s>",
|
| 6 |
+
"lstrip": false,
|
| 7 |
+
"normalized": true,
|
| 8 |
+
"rstrip": false,
|
| 9 |
+
"single_word": false,
|
| 10 |
+
"special": true
|
| 11 |
+
},
|
| 12 |
+
"1": {
|
| 13 |
+
"content": "<pad>",
|
| 14 |
+
"lstrip": false,
|
| 15 |
+
"normalized": true,
|
| 16 |
+
"rstrip": false,
|
| 17 |
+
"single_word": false,
|
| 18 |
+
"special": true
|
| 19 |
+
},
|
| 20 |
+
"2": {
|
| 21 |
+
"content": "</s>",
|
| 22 |
+
"lstrip": false,
|
| 23 |
+
"normalized": true,
|
| 24 |
+
"rstrip": false,
|
| 25 |
+
"single_word": false,
|
| 26 |
+
"special": true
|
| 27 |
+
},
|
| 28 |
+
"3": {
|
| 29 |
+
"content": "<unk>",
|
| 30 |
+
"lstrip": false,
|
| 31 |
+
"normalized": true,
|
| 32 |
+
"rstrip": false,
|
| 33 |
+
"single_word": false,
|
| 34 |
+
"special": true
|
| 35 |
+
},
|
| 36 |
+
"50264": {
|
| 37 |
+
"content": "<mask>",
|
| 38 |
+
"lstrip": true,
|
| 39 |
+
"normalized": false,
|
| 40 |
+
"rstrip": false,
|
| 41 |
+
"single_word": false,
|
| 42 |
+
"special": true
|
| 43 |
+
}
|
| 44 |
+
},
|
| 45 |
+
"bos_token": "<s>",
|
| 46 |
+
"clean_up_tokenization_spaces": true,
|
| 47 |
+
"cls_token": "<s>",
|
| 48 |
+
"eos_token": "</s>",
|
| 49 |
+
"errors": "replace",
|
| 50 |
+
"mask_token": "<mask>",
|
| 51 |
+
"model_max_length": 1000000000000000019884624838656,
|
| 52 |
+
"pad_token": "<pad>",
|
| 53 |
+
"sep_token": "</s>",
|
| 54 |
+
"tokenizer_class": "RobertaTokenizer",
|
| 55 |
+
"trim_offsets": true,
|
| 56 |
+
"unk_token": "<unk>",
|
| 57 |
+
"use_fast": true
|
| 58 |
+
}
|
roberta_base_checkpoint/training_args.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a003471ce28726e03e4323d8c0d341f4aeb9229303295167885e32bc5b19f4cc
|
| 3 |
+
size 5393
|
roberta_base_checkpoint/vocab.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|