Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 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
|
|
@@ -16,15 +17,22 @@ def extract_features(sentence):
|
|
| 16 |
st.title("📝 Preprocessing tool")
|
| 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 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
# Xóa cột Chunk nếu tồn tại
|
| 25 |
if "Chunk" in df_test.columns:
|
| 26 |
df_test = df_test.drop(columns=["Chunk"])
|
| 27 |
|
|
|
|
| 28 |
sentences = [group for _, group in df_test.groupby("Sentence_ID")]
|
| 29 |
|
| 30 |
# Gán nhãn chunking
|
|
@@ -37,11 +45,18 @@ if uploaded_file:
|
|
| 37 |
|
| 38 |
# Ghép lại thành dataframe
|
| 39 |
df_chunked = pd.concat(all_sentences)
|
| 40 |
-
|
| 41 |
-
#
|
| 42 |
-
|
| 43 |
-
df_chunked.to_csv(
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
import joblib
|
| 4 |
+
import io
|
| 5 |
|
| 6 |
# Load mô hình CRF đã train
|
| 7 |
@st.cache_resource
|
|
|
|
| 17 |
st.title("📝 Preprocessing tool")
|
| 18 |
|
| 19 |
# Upload file
|
| 20 |
+
uploaded_file = st.file_uploader("📤 Tải lên file CSV hoặc Excel chứa dữ liệu cần gán nhãn", type=["csv", "xlsx"])
|
| 21 |
|
| 22 |
if uploaded_file:
|
| 23 |
+
# Kiểm tra định dạng file
|
| 24 |
+
file_type = uploaded_file.name.split(".")[-1]
|
| 25 |
+
|
| 26 |
+
if file_type == "csv":
|
| 27 |
+
df_test = pd.read_csv(uploaded_file)
|
| 28 |
+
else: # file_type == "xlsx"
|
| 29 |
+
df_test = pd.read_excel(uploaded_file)
|
| 30 |
+
|
| 31 |
# Xóa cột Chunk nếu tồn tại
|
| 32 |
if "Chunk" in df_test.columns:
|
| 33 |
df_test = df_test.drop(columns=["Chunk"])
|
| 34 |
|
| 35 |
+
# Nhóm theo câu
|
| 36 |
sentences = [group for _, group in df_test.groupby("Sentence_ID")]
|
| 37 |
|
| 38 |
# Gán nhãn chunking
|
|
|
|
| 45 |
|
| 46 |
# Ghép lại thành dataframe
|
| 47 |
df_chunked = pd.concat(all_sentences)
|
| 48 |
+
|
| 49 |
+
# Tạo file CSV để tải xuống
|
| 50 |
+
csv_buffer = io.StringIO()
|
| 51 |
+
df_chunked.to_csv(csv_buffer, index=False, encoding="utf-8")
|
| 52 |
+
csv_data = csv_buffer.getvalue()
|
| 53 |
+
|
| 54 |
+
# Tạo file Excel để tải xuống
|
| 55 |
+
excel_buffer = io.BytesIO()
|
| 56 |
+
with pd.ExcelWriter(excel_buffer, engine="xlsxwriter") as writer:
|
| 57 |
+
df_chunked.to_excel(writer, index=False, sheet_name="Chunked Data")
|
| 58 |
+
excel_data = excel_buffer.getvalue()
|
| 59 |
+
|
| 60 |
+
# Hiển thị nút tải xuống
|
| 61 |
+
st.download_button("📥 Tải xuống dữ liệu đã gán nhãn (CSV)", csv_data, "chunked_data.csv", "text/csv")
|
| 62 |
+
st.download_button("📥 Tải xuống dữ liệu đã gán nhãn (Excel)", excel_data, "chunked_data.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
|