File size: 2,634 Bytes
246133f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import streamlit as st
import pandas as pd
import time
from transformers import pipeline

# Set page title and layout
st.set_page_config(page_title="CSV News Classifier", layout="wide")

# Load the fine-tuned Hugging Face model
@st.cache_resource
def load_model():
    return pipeline("text-classification", model="TAgroup5/daily-mirror-news-classifier")

classifier = load_model()

# Custom CSS for Colors and Styling
st.markdown(
    """
    <style>
        body { background-color: #f8f9fa; }
        .stApp { background-color: #ffffff; padding: 20px; border-radius: 10px; box-shadow: 0px 0px 10px rgba(0,0,0,0.1); }
        
        .custom-box {
            background-color: #f0f0f0; 
            padding: 15px;
            border-radius: 10px;
            border: 1px solid #ccc;
            margin-bottom: 15px;
        }
        h1 { color: #ff5733; text-align: center; }  
        h2, h3 { color: #007bff; }  
    </style>
    """,
    unsafe_allow_html=True
)

# Page Title
st.title("πŸ“Š News Classification from CSV")

# File Uploader
st.markdown('<div class="custom-box"><h3>πŸ“‚ Upload a CSV file</h3></div>', unsafe_allow_html=True)
uploaded_file = st.file_uploader("", type=["csv"])

if uploaded_file is not None:
    # Read CSV
    df = pd.read_csv(uploaded_file)

    # Show Preview
    st.markdown("### πŸ” **Preview of Uploaded File**", unsafe_allow_html=True)
    st.dataframe(df.head())

    # Assuming the column with news articles is named "news_text"
    if "news_text" not in df.columns:
        st.error("❌ The uploaded CSV must contain a 'news_text' column.")
    else:
        # Perform classification
        st.markdown("### 🏷️ **Classifying News Articles...**")
        with st.spinner("Processing..."):
            df["class"] = df["news_text"].apply(lambda text: classifier(text)[0]["label"])

        # Show Preview of Results
        st.markdown("### πŸ“Œ **Preview of Classified Data**", unsafe_allow_html=True)
        st.dataframe(df.head())

        # Download Button for Classified CSV
        csv = df.to_csv(index=False).encode("utf-8")
        st.markdown("### πŸ“₯ **Download Classified CSV**", unsafe_allow_html=True)
        st.download_button(
            label="⬇️ **Download Classified CSV**",
            data=csv,
            file_name="classified_news.csv",
            mime="text/csv",
            help="Click to download the classified news file"
        )

# Footer
st.markdown("---")
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)