Spaces:
Sleeping
Sleeping
Update pages/1_Classification.py
Browse files- pages/1_Classification.py +5 -30
pages/1_Classification.py
CHANGED
|
@@ -14,11 +14,6 @@ def load_classifier():
|
|
| 14 |
classifier = load_classifier()
|
| 15 |
|
| 16 |
st.title("News Classifier")
|
| 17 |
-
st.write(
|
| 18 |
-
"Upload a CSV file with a `content` column. The app will classify each record, "
|
| 19 |
-
"add a `class` column, and let you view the class distribution."
|
| 20 |
-
)
|
| 21 |
-
|
| 22 |
uploaded_file = st.file_uploader("Upload CSV", type=["csv"])
|
| 23 |
|
| 24 |
if uploaded_file is not None:
|
|
@@ -30,37 +25,17 @@ if uploaded_file is not None:
|
|
| 30 |
else:
|
| 31 |
df["content"] = df["content"].fillna("").astype(str)
|
| 32 |
|
| 33 |
-
texts = df["content"].tolist()
|
| 34 |
-
|
| 35 |
with st.spinner("Classifying records..."):
|
| 36 |
-
preds = classifier(
|
| 37 |
|
| 38 |
df["class"] = [pred["label"] for pred in preds]
|
| 39 |
|
| 40 |
-
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
st.
|
| 43 |
st.dataframe(df.head())
|
| 44 |
|
| 45 |
-
st.subheader("Class Distribution")
|
| 46 |
-
class_counts = df["class"].value_counts()
|
| 47 |
-
|
| 48 |
-
fig, ax = plt.subplots()
|
| 49 |
-
ax.bar(class_counts.index, class_counts.values)
|
| 50 |
-
ax.set_xlabel("Class")
|
| 51 |
-
ax.set_ylabel("Number of Articles")
|
| 52 |
-
ax.set_title("Distribution of Predicted Labels")
|
| 53 |
-
plt.xticks(rotation=45)
|
| 54 |
-
st.pyplot(fig)
|
| 55 |
-
|
| 56 |
-
output_csv = df.to_csv(index=False).encode("utf-8")
|
| 57 |
-
|
| 58 |
-
st.download_button(
|
| 59 |
-
label="Download output.csv",
|
| 60 |
-
data=output_csv,
|
| 61 |
-
file_name="output.csv",
|
| 62 |
-
mime="text/csv"
|
| 63 |
-
)
|
| 64 |
-
|
| 65 |
except Exception as e:
|
| 66 |
st.error(f"Error reading or processing file: {e}")
|
|
|
|
| 14 |
classifier = load_classifier()
|
| 15 |
|
| 16 |
st.title("News Classifier")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
uploaded_file = st.file_uploader("Upload CSV", type=["csv"])
|
| 18 |
|
| 19 |
if uploaded_file is not None:
|
|
|
|
| 25 |
else:
|
| 26 |
df["content"] = df["content"].fillna("").astype(str)
|
| 27 |
|
|
|
|
|
|
|
| 28 |
with st.spinner("Classifying records..."):
|
| 29 |
+
preds = classifier(df["content"].tolist(), batch_size=8)
|
| 30 |
|
| 31 |
df["class"] = [pred["label"] for pred in preds]
|
| 32 |
|
| 33 |
+
# save for other pages
|
| 34 |
+
st.session_state["df"] = df
|
| 35 |
+
st.session_state["uploaded_filename"] = uploaded_file.name
|
| 36 |
|
| 37 |
+
st.success("Classification complete.")
|
| 38 |
st.dataframe(df.head())
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
except Exception as e:
|
| 41 |
st.error(f"Error reading or processing file: {e}")
|