Update app.py
Browse files
app.py
CHANGED
@@ -1,91 +1,92 @@
|
|
1 |
-
|
2 |
-
import
|
3 |
-
import
|
4 |
-
|
5 |
-
from
|
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 |
-
st.
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
st.
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
st.
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
ax.
|
86 |
-
ax.
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
|
|
91 |
st.markdown("<p style='text-align:center; color:#666;'>π Built with using Streamlit & Hugging Face</p>", unsafe_allow_html=True)
|
|
|
1 |
+
pip install -r requirements.txt
|
2 |
+
import streamlit as st
|
3 |
+
import pandas as pd
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
+
from wordcloud import WordCloud
|
6 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
|
7 |
+
|
8 |
+
# β
MUST be first Streamlit command
|
9 |
+
st.set_page_config(page_title="π° News Classifier & Q&A App", layout="wide")
|
10 |
+
|
11 |
+
# ----------------- Model Loader -----------------
|
12 |
+
@st.cache_resource
|
13 |
+
def load_text_classifier():
|
14 |
+
model_name = "MihanTilk/News_Classifier"
|
15 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
16 |
+
model = AutoModelForSequenceClassification.from_pretrained(
|
17 |
+
model_name
|
18 |
+
)
|
19 |
+
return pipeline("text-classification", model=model, tokenizer=tokenizer)
|
20 |
+
|
21 |
+
# Load Classifier & QA pipeline
|
22 |
+
classifier = load_text_classifier()
|
23 |
+
qa_pipeline = pipeline("question-answering", model="deepset/roberta-base-squad2")
|
24 |
+
|
25 |
+
# ----------------- CSS Styling -----------------
|
26 |
+
st.markdown(
|
27 |
+
"""
|
28 |
+
<style>
|
29 |
+
.main { background-color: #f4f4f4; }
|
30 |
+
.stTextInput, .stFileUploader { border: 2px solid #ff4b4b; border-radius: 10px; }
|
31 |
+
.stButton>button { background-color: #ff4b4b; color: white; border-radius: 10px; }
|
32 |
+
.stDownloadButton>button { background-color: #4CAF50; color: white; border-radius: 10px; }
|
33 |
+
h1, h2, h3, h4, h5, h6, p { color: #333333; }
|
34 |
+
</style>
|
35 |
+
""",
|
36 |
+
unsafe_allow_html=True
|
37 |
+
)
|
38 |
+
|
39 |
+
# ----------------- App Title -----------------
|
40 |
+
st.title("π° News Classification & Q&A App")
|
41 |
+
st.markdown("<h4 style='color:#ff4b4b;'>Upload a CSV to classify news headlines and ask questions!</h4>", unsafe_allow_html=True)
|
42 |
+
|
43 |
+
# ----------------- Upload CSV -----------------
|
44 |
+
st.subheader("π Upload a CSV File")
|
45 |
+
uploaded_file = st.file_uploader("Choose a CSV file...", type=["csv"])
|
46 |
+
|
47 |
+
if uploaded_file:
|
48 |
+
# Read and preprocess
|
49 |
+
df = pd.read_csv(uploaded_file)
|
50 |
+
if "content" not in df.columns:
|
51 |
+
st.error("β The uploaded CSV must contain a 'content' column.")
|
52 |
+
st.stop()
|
53 |
+
|
54 |
+
# Preprocess text
|
55 |
+
df['cleaned_text'] = df['content'].astype(str).str.lower().str.strip()
|
56 |
+
st.write("π Preview of Uploaded Data:", df.head())
|
57 |
+
|
58 |
+
# ----------------- Classification -----------------
|
59 |
+
with st.spinner("π Classifying news articles..."):
|
60 |
+
df['class'] = df['cleaned_text'].apply(lambda text: classifier(text)[0]['label'])
|
61 |
+
|
62 |
+
st.success("β
Classification Complete!")
|
63 |
+
st.write("π Classified Results:", df[['content', 'class']].head())
|
64 |
+
|
65 |
+
# ----------------- Download -----------------
|
66 |
+
st.subheader("π₯ Download Results")
|
67 |
+
csv_output = df.to_csv(index=False).encode('utf-8')
|
68 |
+
st.download_button("Download Output CSV", data=csv_output, file_name="output.csv", mime="text/csv")
|
69 |
+
|
70 |
+
# ----------------- Q&A Section -----------------
|
71 |
+
st.subheader("π¬ Ask a Question")
|
72 |
+
question = st.text_input("π What do you want to know about the content?")
|
73 |
+
|
74 |
+
if st.button("Get Answer"):
|
75 |
+
context = " ".join(df['cleaned_text'].tolist())
|
76 |
+
with st.spinner("Answering..."):
|
77 |
+
result = qa_pipeline(question=question, context=context)
|
78 |
+
st.success(f"π **Answer:** {result['answer']}")
|
79 |
+
|
80 |
+
# ----------------- Word Cloud -----------------
|
81 |
+
st.subheader("βοΈ Word Cloud of News Text")
|
82 |
+
text = " ".join(df['cleaned_text'].tolist())
|
83 |
+
wordcloud = WordCloud(width=800, height=400, background_color="white").generate(text)
|
84 |
+
|
85 |
+
fig, ax = plt.subplots()
|
86 |
+
ax.imshow(wordcloud, interpolation="bilinear")
|
87 |
+
ax.axis("off")
|
88 |
+
st.pyplot(fig)
|
89 |
+
|
90 |
+
# ----------------- Footer -----------------
|
91 |
+
st.markdown("---")
|
92 |
st.markdown("<p style='text-align:center; color:#666;'>π Built with using Streamlit & Hugging Face</p>", unsafe_allow_html=True)
|