Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import time
|
4 |
+
from transformers import pipeline
|
5 |
+
|
6 |
+
# Set page title and layout
|
7 |
+
st.set_page_config(page_title="CSV News Classifier", layout="wide")
|
8 |
+
|
9 |
+
# Load the fine-tuned Hugging Face model
|
10 |
+
@st.cache_resource
|
11 |
+
def load_model():
|
12 |
+
return pipeline("text-classification", model="TAgroup5/daily-mirror-news-classifier")
|
13 |
+
|
14 |
+
classifier = load_model()
|
15 |
+
|
16 |
+
# Custom CSS for Colors and Styling
|
17 |
+
st.markdown(
|
18 |
+
"""
|
19 |
+
<style>
|
20 |
+
body { background-color: #f8f9fa; }
|
21 |
+
.stApp { background-color: #ffffff; padding: 20px; border-radius: 10px; box-shadow: 0px 0px 10px rgba(0,0,0,0.1); }
|
22 |
+
|
23 |
+
.custom-box {
|
24 |
+
background-color: #f0f0f0;
|
25 |
+
padding: 15px;
|
26 |
+
border-radius: 10px;
|
27 |
+
border: 1px solid #ccc;
|
28 |
+
margin-bottom: 15px;
|
29 |
+
}
|
30 |
+
h1 { color: #ff5733; text-align: center; }
|
31 |
+
h2, h3 { color: #007bff; }
|
32 |
+
</style>
|
33 |
+
""",
|
34 |
+
unsafe_allow_html=True
|
35 |
+
)
|
36 |
+
|
37 |
+
# Page Title
|
38 |
+
st.title("π News Classification from CSV")
|
39 |
+
|
40 |
+
# File Uploader
|
41 |
+
st.markdown('<div class="custom-box"><h3>π Upload a CSV file</h3></div>', unsafe_allow_html=True)
|
42 |
+
uploaded_file = st.file_uploader("", type=["csv"])
|
43 |
+
|
44 |
+
if uploaded_file is not None:
|
45 |
+
# Read CSV
|
46 |
+
df = pd.read_csv(uploaded_file)
|
47 |
+
|
48 |
+
# Show Preview
|
49 |
+
st.markdown("### π **Preview of Uploaded File**", unsafe_allow_html=True)
|
50 |
+
st.dataframe(df.head())
|
51 |
+
|
52 |
+
# Assuming the column with news articles is named "news_text"
|
53 |
+
if "news_text" not in df.columns:
|
54 |
+
st.error("β The uploaded CSV must contain a 'news_text' column.")
|
55 |
+
else:
|
56 |
+
# Perform classification
|
57 |
+
st.markdown("### π·οΈ **Classifying News Articles...**")
|
58 |
+
with st.spinner("Processing..."):
|
59 |
+
df["class"] = df["news_text"].apply(lambda text: classifier(text)[0]["label"])
|
60 |
+
|
61 |
+
# Show Preview of Results
|
62 |
+
st.markdown("### π **Preview of Classified Data**", unsafe_allow_html=True)
|
63 |
+
st.dataframe(df.head())
|
64 |
+
|
65 |
+
# Download Button for Classified CSV
|
66 |
+
csv = df.to_csv(index=False).encode("utf-8")
|
67 |
+
st.markdown("### π₯ **Download Classified CSV**", unsafe_allow_html=True)
|
68 |
+
st.download_button(
|
69 |
+
label="β¬οΈ **Download Classified CSV**",
|
70 |
+
data=csv,
|
71 |
+
file_name="classified_news.csv",
|
72 |
+
mime="text/csv",
|
73 |
+
help="Click to download the classified news file"
|
74 |
+
)
|
75 |
+
|
76 |
+
# Footer
|
77 |
+
st.markdown("---")
|
78 |
+
st.markdown('<p style="text-align:center; font-size:14px; color:#6c757d;">π¨βπ» Developed by <b>Ridmi Navodya</b> | Powered by Streamlit π</p>', unsafe_allow_html=True)
|
79 |
+
|
80 |
+
|