Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
#
|
| 27 |
-
|
| 28 |
-
sentence
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
#
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
#
|
| 42 |
-
|
| 43 |
-
|
| 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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|