TharushiPerera commited on
Commit
740b2dc
Β·
verified Β·
1 Parent(s): b34e3da

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -90
app.py CHANGED
@@ -1,91 +1,92 @@
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)
 
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)