Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,7 @@ import os
|
|
| 2 |
import re
|
| 3 |
import tempfile
|
| 4 |
from functools import lru_cache
|
| 5 |
-
|
| 6 |
import gradio as gr
|
| 7 |
import pandas as pd
|
| 8 |
import torch
|
|
@@ -307,6 +307,127 @@ with gr.Blocks(title="Vietnamese Sentiment Analyzer", theme=gr.themes.Soft()) as
|
|
| 307 |
outputs=[summary_output, file_output, preview_output],
|
| 308 |
)
|
| 309 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 310 |
if __name__ == "__main__":
|
| 311 |
demo.launch(
|
| 312 |
server_name="0.0.0.0",
|
|
|
|
| 2 |
import re
|
| 3 |
import tempfile
|
| 4 |
from functools import lru_cache
|
| 5 |
+
import spaces
|
| 6 |
import gradio as gr
|
| 7 |
import pandas as pd
|
| 8 |
import torch
|
|
|
|
| 307 |
outputs=[summary_output, file_output, preview_output],
|
| 308 |
)
|
| 309 |
|
| 310 |
+
|
| 311 |
+
|
| 312 |
+
@spaces.GPU
|
| 313 |
+
def process_dataframe(
|
| 314 |
+
df,
|
| 315 |
+
model_path,
|
| 316 |
+
batch_size,
|
| 317 |
+
max_length,
|
| 318 |
+
skip_keyword_check,
|
| 319 |
+
main_keywords,
|
| 320 |
+
progress=gr.Progress(),
|
| 321 |
+
):
|
| 322 |
+
missing = [
|
| 323 |
+
column for column in REQUIRED_COLUMNS
|
| 324 |
+
if column not in df.columns
|
| 325 |
+
]
|
| 326 |
+
if missing:
|
| 327 |
+
raise gr.Error(
|
| 328 |
+
"File thiếu cột bắt buộc: " + ", ".join(missing)
|
| 329 |
+
)
|
| 330 |
+
|
| 331 |
+
# Reset index để bảo đảm vị trí kết quả khớp với dữ liệu đầu vào.
|
| 332 |
+
df = df.copy().reset_index(drop=True)
|
| 333 |
+
|
| 334 |
+
tokenizer, model, device, label_map = load_sentiment_model(
|
| 335 |
+
model_path
|
| 336 |
+
)
|
| 337 |
+
|
| 338 |
+
keywords = [
|
| 339 |
+
item.strip()
|
| 340 |
+
for item in (main_keywords or "").split(",")
|
| 341 |
+
if item.strip()
|
| 342 |
+
]
|
| 343 |
+
|
| 344 |
+
sentiments = [""] * len(df)
|
| 345 |
+
pending_indices = []
|
| 346 |
+
pending_texts = []
|
| 347 |
+
unknown_types = 0
|
| 348 |
+
empty_content = 0
|
| 349 |
+
|
| 350 |
+
# Chuẩn bị nội dung cần phân loại.
|
| 351 |
+
for position in range(len(df)):
|
| 352 |
+
row = df.iloc[position]
|
| 353 |
+
doc_type = get_doc_type(row.get("Type"))
|
| 354 |
+
|
| 355 |
+
if doc_type == "unknown":
|
| 356 |
+
unknown_types += 1
|
| 357 |
+
continue
|
| 358 |
+
|
| 359 |
+
text = build_text(row, doc_type)
|
| 360 |
+
|
| 361 |
+
if not text:
|
| 362 |
+
empty_content += 1
|
| 363 |
+
continue
|
| 364 |
+
|
| 365 |
+
pending_indices.append(position)
|
| 366 |
+
pending_texts.append(text)
|
| 367 |
+
|
| 368 |
+
total = len(pending_texts)
|
| 369 |
+
|
| 370 |
+
progress(0, desc="Đang chuẩn bị dữ liệu...")
|
| 371 |
+
|
| 372 |
+
# Phân loại theo batch.
|
| 373 |
+
for start in range(0, total, batch_size):
|
| 374 |
+
batch_texts = pending_texts[start:start + batch_size]
|
| 375 |
+
batch_indices = pending_indices[start:start + batch_size]
|
| 376 |
+
|
| 377 |
+
results = predict_batch(
|
| 378 |
+
batch_texts,
|
| 379 |
+
tokenizer,
|
| 380 |
+
model,
|
| 381 |
+
device,
|
| 382 |
+
label_map,
|
| 383 |
+
max_length,
|
| 384 |
+
)
|
| 385 |
+
|
| 386 |
+
for index, text, result in zip(
|
| 387 |
+
batch_indices, batch_texts, results
|
| 388 |
+
):
|
| 389 |
+
label, confidence = result
|
| 390 |
+
|
| 391 |
+
if not skip_keyword_check:
|
| 392 |
+
label, confidence = apply_keyword_fallback(
|
| 393 |
+
label,
|
| 394 |
+
confidence,
|
| 395 |
+
text,
|
| 396 |
+
keywords,
|
| 397 |
+
)
|
| 398 |
+
|
| 399 |
+
sentiments[index] = label
|
| 400 |
+
|
| 401 |
+
completed = min(start + len(batch_texts), total)
|
| 402 |
+
|
| 403 |
+
progress(
|
| 404 |
+
completed / max(total, 1),
|
| 405 |
+
desc=f"Đang phân tích: {completed:,}/{total:,} dòng",
|
| 406 |
+
)
|
| 407 |
+
|
| 408 |
+
# Thêm hoặc cập nhật cột Sentiment.
|
| 409 |
+
df["Sentiment"] = sentiments
|
| 410 |
+
|
| 411 |
+
counts = df["Sentiment"].value_counts()
|
| 412 |
+
|
| 413 |
+
summary = [
|
| 414 |
+
f"**Thiết bị:** `{device}`",
|
| 415 |
+
f"**Model:** `{model_path}`",
|
| 416 |
+
f"**Tổng số dòng:** {len(df):,}",
|
| 417 |
+
f"**Đã phân tích:** {total:,}",
|
| 418 |
+
f"**Bỏ trống do Type không hợp lệ:** {unknown_types:,}",
|
| 419 |
+
f"**Bỏ trống do không có nội dung:** {empty_content:,}",
|
| 420 |
+
"",
|
| 421 |
+
"**Thống kê sentiment:**",
|
| 422 |
+
]
|
| 423 |
+
|
| 424 |
+
for label in ("positive", "neutral", "negative"):
|
| 425 |
+
summary.append(
|
| 426 |
+
f"- `{label}`: {int(counts.get(label, 0)):,}"
|
| 427 |
+
)
|
| 428 |
+
|
| 429 |
+
return df, "\n".join(summary)
|
| 430 |
+
|
| 431 |
if __name__ == "__main__":
|
| 432 |
demo.launch(
|
| 433 |
server_name="0.0.0.0",
|