File size: 8,467 Bytes
740b2dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
529a69e
 
 
 
 
740b2dc
 
 
 
 
b76d6c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
740b2dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
529a69e
740b2dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
529a69e
 
740b2dc
 
 
 
 
 
 
529a69e
740b2dc
 
529a69e
740b2dc
b76d6c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
740b2dc
 
 
60d3ffb
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
from wordcloud import WordCloud
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification

# βœ… MUST be first Streamlit command
st.set_page_config(page_title="πŸ“° News Classifier & Q&A App", layout="wide")

# ----------------- Model Loader -----------------
@st.cache_resource
def load_text_classifier():
    model_name = "MihanTilk/News_Classifier"
    tokenizer = AutoTokenizer.from_pretrained(model_name)
    model = AutoModelForSequenceClassification.from_pretrained(
        model_name
    )
    return pipeline("text-classification", model=model, tokenizer=tokenizer)

# Load Classifier & QA pipeline
classifier = load_text_classifier()
qa_pipeline = pipeline(
    "question-answering",
    model="deepset/roberta-large-squad2",
    tokenizer="deepset/roberta-large-squad2"
)

# ----------------- CSS Styling -----------------
st.markdown(
    """
    <style>
        /* Main background and text colors */
        .main { 
            background-color: #f4f4f4;
        }

        /* Text input boxes - light blue theme */
        .stTextInput>div>div>input, 
        .stTextArea>div>div>textarea {
            background-color: #e6f2ff;
            border: 1px solid #b3d1ff;
            border-radius: 8px;
            color: #003366;
        }

        /* File uploader - matching style */
        .stFileUploader>div>div {
            background-color: #e6f2ff;
            border: 1px solid #b3d1ff;
            border-radius: 8px;
        }

        /* Buttons - keeping your original style */
        .stButton>button {
            background-color: #ff4b4b;
            color: white;
            border-radius: 10px;
            border: none;
        }

        .stDownloadButton>button {
            background-color: #4CAF50;
            color: white;
            border-radius: 10px;
            border: none;
        }

        /* Text colors */
        h1, h2, h3, h4, h5, h6 {
            color: #003366;  /* Dark blue for headers */
        }

        p {
            color: #336699;  /* Medium blue for paragraphs */
        }

        /* Dataframe styling */
        .dataframe {
            background-color: #e6f2ff;
            border: 1px solid #b3d1ff;
        }
    </style>
    """,
    unsafe_allow_html=True
)

# ----------------- App Title -----------------
st.title("πŸ“° News Classification & Q&A App")
st.markdown("<h4 style='color:#ff4b4b;'>Upload a CSV to classify news headlines and ask questions!</h4>", unsafe_allow_html=True)

# ----------------- Upload CSV -----------------
st.subheader("πŸ“‚ Upload a CSV File")
uploaded_file = st.file_uploader("Choose a CSV file...", type=["csv"])

if uploaded_file:
    # Read and preprocess
    df = pd.read_csv(uploaded_file, encoding='utf-8')
    if "content" not in df.columns:
        st.error("❌ The uploaded CSV must contain a 'content' column.")
        st.stop()

    # Preprocess text
    df['cleaned_text'] = df['content'].astype(str).str.lower().str.strip()
    st.write("πŸ“Š Preview of Uploaded Data:", df.head())

    # ----------------- Classification -----------------
    with st.spinner("πŸ” Classifying news articles..."):
        df['class'] = df['cleaned_text'].apply(lambda text: classifier(text)[0]['label'])

    st.success("βœ… Classification Complete!")
    st.write("πŸ”Ž Classified Results:", df[['content', 'class']].head())

    # ----------------- Download -----------------
    st.subheader("πŸ“₯ Download Results")
    output_df = df[['content', 'class']]
    csv_output = output_df.to_csv(index=False, encoding='utf-8-sig').encode('utf-8-sig')
    st.download_button("Download Output CSV", data=csv_output, file_name="output.csv", mime="text/csv")

    # ----------------- Q&A Section -----------------
    st.subheader("πŸ’¬ Ask a Question")
    question = st.text_input("πŸ” What do you want to know about the content?")

    if st.button("Get Answer"):
        context = " ".join(df['content'].tolist())
        with st.spinner("Answering..."):
            result = qa_pipeline(question=question, context=context)
        st.success(f"πŸ“ Answer: {result['answer']}")

# ----------------- Visualization Section -----------------

    st.subheader("πŸ“Š Data Visualizations")

    # Create two main columns (60/40 split)
    main_col1, main_col2 = st.columns([3, 2])

    with main_col1:
        # ----------------- Topic Distribution -----------------
        st.markdown("*Topic Distribution*")

        # Create sub-columns for the charts
        chart_col1, chart_col2 = st.columns(2)

        with chart_col1:
            # Compact Pie Chart
            fig1, ax1 = plt.subplots(figsize=(4, 4))
            df['class'].value_counts().plot.pie(
                autopct='%1.1f%%',
                startangle=90,
                ax=ax1,
                colors=['#ff9999', '#66b3ff', '#99ff99', '#ffcc99', '#c2c2f0'],
                wedgeprops={'linewidth': 0.5, 'edgecolor': 'white'}
            )
            ax1.set_ylabel('')
            st.pyplot(fig1, use_container_width=True)

        with chart_col2:
            # Compact Bar Chart
            fig2, ax2 = plt.subplots(figsize=(4, 4))
            df['class'].value_counts().plot.bar(
                color=['#ff9999', '#66b3ff', '#99ff99', '#ffcc99', '#c2c2f0'],
                ax=ax2,
                width=0.7
            )
            ax2.set_xlabel('')
            ax2.set_ylabel('Count')
            plt.xticks(rotation=45, ha='right')
            st.pyplot(fig2, use_container_width=True)

    with main_col2:
        # ----------------- Compact Word Cloud -----------------
        st.markdown("*Word Cloud*")
        text = " ".join(df['content'].tolist())
        wordcloud = WordCloud(
            width=300,
            height=200,
            background_color="white",
            collocations=False,
            max_words=100
        ).generate(text)

        fig3, ax3 = plt.subplots(figsize=(4, 3))
        ax3.imshow(wordcloud, interpolation="bilinear")
        ax3.axis("off")
        st.pyplot(fig3, use_container_width=True)

# ----------------- Detailed Stats (below) -----------------
    with st.expander("πŸ“ˆ Detailed Statistics", expanded=False):
        stats_col1, stats_col2 = st.columns(2)

        with stats_col1:
            st.write("*Category Breakdown:*")
            stats_df = df['class'].value_counts().reset_index()
            stats_df.columns = ['Category', 'Count']
            stats_df['Percentage'] = (stats_df['Count'] / stats_df['Count'].sum() * 100).round(1)
            st.dataframe(stats_df, height=200)

        with stats_col2:
            if 'date' in df.columns:
                try:
                    st.write("*Monthly Trends*")
                    df['date'] = pd.to_datetime(df['date'])
                    trends = df.groupby([df['date'].dt.to_period('M'), 'class']).size().unstack()
                    st.line_chart(trends)
                except:
                    st.warning("Date parsing failed")

# ----------------- News Category Explorer -----------------
    st.subheader("πŸ” Explore News by Category")

    # Get unique categories
    categories = df['class'].unique()

    # Create 5 columns for category buttons
    cols = st.columns(5)

    # Create a dictionary to store category articles
    category_articles = {category: df[df['class'] == category] for category in categories}

    # Place each category button in its own column
    for i, category in enumerate(categories):
        with cols[i]:
            if st.button(category, key=f"btn_{category}"):
                # Create pop-up window
                with st.popover(f"πŸ“° Articles in {category}", use_container_width=True):
                    st.markdown(f"### {category} Articles")
                    articles = category_articles[category]

                    # Display articles with expandable content
                    for idx, row in articles.iterrows():
                        with st.expander(f"Article {idx + 1}: {row['content'][:50]}...", expanded=False):
                            st.write(row['content'])
                            st.caption(f"Classification confidence: {classifier(row['content'])[0]['score']:.2f}")

# ----------------- Footer -----------------
st.markdown("---")
st.markdown("<p style='text-align:center; color:#666;'>πŸš€ Built with using Streamlit & Hugging Face</p>", unsafe_allow_html=True)