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