NghiTran1009 commited on
Commit
f5b15ce
·
verified ·
1 Parent(s): da4e37a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -56
app.py CHANGED
@@ -1,7 +1,6 @@
1
  import streamlit as st
2
  import pandas as pd
3
  import joblib
4
- import os # Fix lỗi FileNotFoundError
5
 
6
  # Load mô hình CRF đã train
7
  @st.cache_resource
@@ -14,58 +13,30 @@ crf = load_model()
14
  def extract_features(sentence):
15
  return [{"word": token, "pos": pos} for token, pos in zip(sentence["Token"], sentence["POS"])]
16
 
17
- # Load dữ liệu test
18
- df_test = pd.read_csv("vi-chunk-test.csv")
19
-
20
- # Nhóm dữ liệu theo câu
21
- sentences = [group for _, group in df_test.groupby("Sentence_ID")]
22
-
23
- # Giao diện Annotator
24
- st.title("📝 Tool Annotator - Chỉnh sửa dữ liệu chunking")
25
-
26
- # Chọn câu để annotate
27
- sentence_id = st.selectbox("Chọn câu", range(1, len(sentences) + 1))
28
- sentence = sentences[sentence_id - 1].copy() # Tạo bản sao tránh ảnh hưởng dữ liệu gốc
29
-
30
- # Dự đoán bằng mô hình CRF
31
- X_test = [extract_features(sentence)]
32
- y_pred = crf.predict(X_test)[0]
33
-
34
- # Thêm nhãn dự đoán vào dataframe
35
- sentence["Predicted_Chunk"] = y_pred
36
- sentence["Is_Correct"] = sentence["Chunk"] == sentence["Predicted_Chunk"] # Kiểm tra dự đoán
37
-
38
- # **Ẩn cột `Chunk` trong tất cả các bảng**
39
- sentence_display = sentence.drop(columns=["Chunk"]) # Bỏ hiển thị cột `Chunk`
40
-
41
- # Highlight lỗi: Màu đỏ nếu `Predicted_Chunk` sai
42
- def highlight_errors(row):
43
- return ["background-color: #FF9999" if not row["Is_Correct"] else "" for _ in row]
44
-
45
- # Hiển thị số lượng đúng/sai
46
- num_correct = sentence["Is_Correct"].sum()
47
- num_wrong = len(sentence) - num_correct
48
- st.write(f"✅ **Số token đúng**: {num_correct} / {len(sentence)}")
49
- st.write(f"❌ **Số token sai**: {num_wrong}")
50
-
51
- # **🔹 Hiển thị bảng với highlight lỗi (ẨN `Chunk`)**
52
- st.write("🔹 **Câu gốc (Highlight lỗi màu đỏ)**")
53
- st.dataframe(sentence_display.style.apply(highlight_errors, axis=1))
54
-
55
- # **🔹 Annotator chỉnh sửa `Predicted_Chunk` (ẨN `Chunk`)**
56
- edited_df = st.data_editor(
57
- sentence_display[["Token", "POS", "Predicted_Chunk"]], # Chỉ hiển thị các cột cần thiết
58
- num_rows="dynamic", # Cho phép thêm hàng ở bất kỳ đâu
59
- key=f"edit_table_{sentence_id}"
60
- )
61
-
62
- # Kiểm tra nếu file tồn tại trước khi tải
63
- if os.path.exists("corrected_data.csv"):
64
- with open("corrected_data.csv", "rb") as file:
65
- st.download_button("📥 Tải xuống corrected_data.csv", file, "corrected_data.csv")
66
-
67
- # Lưu lại dữ liệu chỉnh sửa
68
- if st.button("Lưu chỉnh sửa"):
69
- sentence.update(edited_df) # Cập nhật lại dữ liệu chỉnh sửa vào dataframe gốc
70
- sentence.to_csv("corrected_data.csv", index=False, encoding="utf-8", columns=["Token", "POS", "Predicted_Chunk"]) # Chỉ lưu các cột cần thiết
71
- st.success("✅ Dữ liệu đã được lưu thành corrected_data.csv!")
 
1
  import streamlit as st
2
  import pandas as pd
3
  import joblib
 
4
 
5
  # Load mô hình CRF đã train
6
  @st.cache_resource
 
13
  def extract_features(sentence):
14
  return [{"word": token, "pos": pos} for token, pos in zip(sentence["Token"], sentence["POS"])]
15
 
16
+ st.title("📝 Tool Gán Nhãn Chunking")
17
+
18
+ # Upload file
19
+ uploaded_file = st.file_uploader("📤 Tải lên file CSV chứa dữ liệu cần gán nhãn", type=["csv"])
20
+
21
+ if uploaded_file:
22
+ df_test = pd.read_csv(uploaded_file)
23
+ sentences = [group for _, group in df_test.groupby("Sentence_ID")]
24
+
25
+ # Gán nhãn chunking
26
+ all_sentences = []
27
+ for sentence in sentences:
28
+ X_test = [extract_features(sentence)]
29
+ y_pred = crf.predict(X_test)[0]
30
+ sentence["Chunk"] = y_pred # Gán nhãn dự đoán
31
+ all_sentences.append(sentence)
32
+
33
+ # Ghép lại thành dataframe
34
+ df_chunked = pd.concat(all_sentences)
35
+
36
+ # Xuất file CSV
37
+ output_file = "chunked_data.csv"
38
+ df_chunked.to_csv(output_file, index=False, encoding="utf-8")
39
+
40
+ # Nút tải xuống
41
+ with open(output_file, "rb") as file:
42
+ st.download_button("📥 Tải xuống dữ liệu đã gán nhãn", file, output_file)