Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,6 +7,8 @@ from nltk.corpus import stopwords
|
|
| 7 |
from nltk.stem import WordNetLemmatizer
|
| 8 |
from transformers import pipeline
|
| 9 |
from PIL import Image
|
|
|
|
|
|
|
| 10 |
|
| 11 |
# Download required NLTK data
|
| 12 |
nltk.download('stopwords')
|
|
@@ -90,36 +92,23 @@ def chatbot_response(history, user_input, text_input=None, file_input=None):
|
|
| 90 |
|
| 91 |
return history, answer
|
| 92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
# Streamlit App Layout
|
| 94 |
st.set_page_config(page_title="News Classifier", page_icon="📰")
|
| 95 |
-
|
| 96 |
-
# Custom CSS for responsive design (desktop/mobile optimization)
|
| 97 |
-
st.markdown(
|
| 98 |
-
"""
|
| 99 |
-
<style>
|
| 100 |
-
@media only screen and (max-width: 600px) {
|
| 101 |
-
.stApp {
|
| 102 |
-
padding: 10px;
|
| 103 |
-
}
|
| 104 |
-
}
|
| 105 |
-
|
| 106 |
-
@media only screen and (min-width: 601px) {
|
| 107 |
-
.stApp {
|
| 108 |
-
padding: 20px;
|
| 109 |
-
}
|
| 110 |
-
}
|
| 111 |
-
|
| 112 |
-
.stButton {
|
| 113 |
-
margin-top: 20px;
|
| 114 |
-
}
|
| 115 |
-
|
| 116 |
-
.stTextInput {
|
| 117 |
-
width: 100%;
|
| 118 |
-
}
|
| 119 |
-
</style>
|
| 120 |
-
""", unsafe_allow_html=True
|
| 121 |
-
)
|
| 122 |
-
|
| 123 |
cover_image = Image.open("cover.png") # Ensure this image exists
|
| 124 |
st.image(cover_image, caption="News Classifier 📢", use_container_width=True)
|
| 125 |
|
|
@@ -131,6 +120,10 @@ if st.button("🔍 Classify"):
|
|
| 131 |
category, confidence = classify_text(text_input)
|
| 132 |
st.write(f"Predicted Category: {category}")
|
| 133 |
st.write(f"Confidence Level: {confidence}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
else:
|
| 135 |
st.warning("Please enter some text to classify.")
|
| 136 |
|
|
@@ -147,6 +140,14 @@ if file_input:
|
|
| 147 |
file_name=output_file,
|
| 148 |
mime="text/csv"
|
| 149 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
else:
|
| 151 |
st.error(f"Error processing file: {output_file}")
|
| 152 |
|
|
@@ -160,4 +161,4 @@ if st.button("✉ Send"):
|
|
| 160 |
st.write("Chatbot Response:")
|
| 161 |
for q, a in history:
|
| 162 |
st.write(f"Q: {q}")
|
| 163 |
-
st.write(f"A:
|
|
|
|
| 7 |
from nltk.stem import WordNetLemmatizer
|
| 8 |
from transformers import pipeline
|
| 9 |
from PIL import Image
|
| 10 |
+
import matplotlib.pyplot as plt
|
| 11 |
+
from wordcloud import WordCloud
|
| 12 |
|
| 13 |
# Download required NLTK data
|
| 14 |
nltk.download('stopwords')
|
|
|
|
| 92 |
|
| 93 |
return history, answer
|
| 94 |
|
| 95 |
+
# Function to generate word cloud
|
| 96 |
+
def generate_word_cloud(text):
|
| 97 |
+
wordcloud = WordCloud(width=800, height=400, background_color="white").generate(text)
|
| 98 |
+
return wordcloud
|
| 99 |
+
|
| 100 |
+
# Function to generate bar graph for decoded predictions
|
| 101 |
+
def generate_bar_graph(df):
|
| 102 |
+
prediction_counts = df["Decoded Prediction"].value_counts()
|
| 103 |
+
fig, ax = plt.subplots(figsize=(10, 6))
|
| 104 |
+
prediction_counts.plot(kind='bar', ax=ax, color='skyblue')
|
| 105 |
+
ax.set_title('Frequency of Decoded Predictions', fontsize=16)
|
| 106 |
+
ax.set_xlabel('Category', fontsize=12)
|
| 107 |
+
ax.set_ylabel('Frequency', fontsize=12)
|
| 108 |
+
st.pyplot(fig)
|
| 109 |
+
|
| 110 |
# Streamlit App Layout
|
| 111 |
st.set_page_config(page_title="News Classifier", page_icon="📰")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
cover_image = Image.open("cover.png") # Ensure this image exists
|
| 113 |
st.image(cover_image, caption="News Classifier 📢", use_container_width=True)
|
| 114 |
|
|
|
|
| 120 |
category, confidence = classify_text(text_input)
|
| 121 |
st.write(f"Predicted Category: {category}")
|
| 122 |
st.write(f"Confidence Level: {confidence}")
|
| 123 |
+
|
| 124 |
+
# Generate word cloud for the cleaned text input
|
| 125 |
+
wordcloud = generate_word_cloud(text_input)
|
| 126 |
+
st.image(wordcloud.to_array(), caption="Word Cloud for Text Input", use_container_width=True)
|
| 127 |
else:
|
| 128 |
st.warning("Please enter some text to classify.")
|
| 129 |
|
|
|
|
| 140 |
file_name=output_file,
|
| 141 |
mime="text/csv"
|
| 142 |
)
|
| 143 |
+
|
| 144 |
+
# Generate word cloud for the cleaned CSV data
|
| 145 |
+
bulk_text = " ".join(df["Decoded Prediction"].dropna().astype(str).tolist())
|
| 146 |
+
wordcloud = generate_word_cloud(bulk_text)
|
| 147 |
+
st.image(wordcloud.to_array(), caption="Word Cloud for CSV Data", use_container_width=True)
|
| 148 |
+
|
| 149 |
+
# Generate bar graph for decoded predictions frequency
|
| 150 |
+
generate_bar_graph(df)
|
| 151 |
else:
|
| 152 |
st.error(f"Error processing file: {output_file}")
|
| 153 |
|
|
|
|
| 161 |
st.write("Chatbot Response:")
|
| 162 |
for q, a in history:
|
| 163 |
st.write(f"Q: {q}")
|
| 164 |
+
st.write(f"A: {a}")
|