Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,6 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
import re
|
| 4 |
-
from nltk.tokenize import word_tokenize
|
| 5 |
-
from nltk.corpus import stopwords
|
| 6 |
-
from nltk.stem import WordNetLemmatizer
|
| 7 |
-
from transformers import pipeline
|
| 8 |
-
from PIL import Image
|
| 9 |
-
|
| 10 |
-
# Load Models
|
| 11 |
-
news_classifier = pipeline("text-classification", model="Oneli/News_Classification")
|
| 12 |
-
import streamlit as st
|
| 13 |
-
import pandas as pd
|
| 14 |
-
import re
|
| 15 |
import string
|
| 16 |
import nltk
|
| 17 |
from nltk.corpus import stopwords
|
|
@@ -24,6 +13,7 @@ from PIL import Image
|
|
| 24 |
nltk.download('stopwords')
|
| 25 |
nltk.download('punkt')
|
| 26 |
nltk.download('wordnet')
|
|
|
|
| 27 |
|
| 28 |
# Load Models
|
| 29 |
news_classifier = pipeline("text-classification", model="Oneli/News_Classification")
|
|
@@ -41,7 +31,6 @@ label_mapping = {
|
|
| 41 |
# Store classified article for QA
|
| 42 |
context_storage = {"context": "", "bulk_context": "", "num_articles": 0}
|
| 43 |
|
| 44 |
-
|
| 45 |
# Text Cleaning Functions
|
| 46 |
def clean_text(text):
|
| 47 |
text = text.lower()
|
|
@@ -53,7 +42,6 @@ def clean_text(text):
|
|
| 53 |
tokens = [lemmatizer.lemmatize(word) for word in tokens] # Lemmatize tokens
|
| 54 |
return " ".join(tokens)
|
| 55 |
|
| 56 |
-
|
| 57 |
# Define the functions
|
| 58 |
def classify_text(text):
|
| 59 |
cleaned_text = clean_text(text)
|
|
@@ -66,12 +54,11 @@ def classify_text(text):
|
|
| 66 |
|
| 67 |
return category, f"Confidence: {confidence}%"
|
| 68 |
|
| 69 |
-
|
| 70 |
def classify_csv(file):
|
| 71 |
try:
|
| 72 |
df = pd.read_csv(file, encoding="utf-8")
|
| 73 |
text_column = df.columns[0] # Assume first column is the text column
|
| 74 |
-
|
| 75 |
df[text_column] = df[text_column].astype(str).apply(clean_text) # Clean text column
|
| 76 |
df["Encoded Prediction"] = df[text_column].apply(lambda x: news_classifier(x)[0]['label'])
|
| 77 |
df["Decoded Prediction"] = df["Encoded Prediction"].map(label_mapping)
|
|
@@ -87,23 +74,22 @@ def classify_csv(file):
|
|
| 87 |
except Exception as e:
|
| 88 |
return None, f"Error: {str(e)}"
|
| 89 |
|
| 90 |
-
|
| 91 |
def chatbot_response(history, user_input, source):
|
| 92 |
user_input = user_input.lower()
|
| 93 |
context = context_storage["context"] if source == "Single Article" else context_storage["bulk_context"]
|
| 94 |
num_articles = context_storage["num_articles"]
|
| 95 |
-
|
| 96 |
if "number of articles" in user_input or "how many articles" in user_input:
|
| 97 |
answer = f"There are {num_articles} articles in the uploaded CSV."
|
| 98 |
history.append([user_input, answer])
|
| 99 |
return history, ""
|
| 100 |
-
|
| 101 |
if context:
|
| 102 |
result = qa_pipeline(question=user_input, context=context)
|
| 103 |
answer = result["answer"]
|
| 104 |
history.append([user_input, answer])
|
| 105 |
return history, ""
|
| 106 |
-
|
| 107 |
responses = {
|
| 108 |
"hello": "π Hello! How can I assist you with news today?",
|
| 109 |
"hi": "π Hi there! What do you want to know about news?",
|
|
@@ -111,16 +97,14 @@ def chatbot_response(history, user_input, source):
|
|
| 111 |
"thank you": "π You're welcome! Let me know if you need anything else.",
|
| 112 |
"news": "π° I can classify news into Business, Sports, Politics, and more!",
|
| 113 |
}
|
| 114 |
-
response = responses.get(user_input,
|
| 115 |
-
"π€ I'm here to help with news classification and general info. Ask me about news topics!")
|
| 116 |
history.append([user_input, response])
|
| 117 |
return history, ""
|
| 118 |
|
| 119 |
-
|
| 120 |
# Streamlit App Layout
|
| 121 |
st.set_page_config(page_title="News Classifier", page_icon="π°")
|
| 122 |
cover_image = Image.open("cover.png") # Ensure this image exists
|
| 123 |
-
st.image(cover_image, caption="News Classifier π’",
|
| 124 |
|
| 125 |
# Section for Single Article Classification
|
| 126 |
st.subheader("π° Single Article Classification")
|
|
@@ -159,4 +143,4 @@ if st.button("β Send"):
|
|
| 159 |
st.write("*Chatbot Response:*")
|
| 160 |
for q, a in history:
|
| 161 |
st.write(f"*Q:* {q}")
|
| 162 |
-
st.write(f"*A:* {a}")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
import re
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
import string
|
| 5 |
import nltk
|
| 6 |
from nltk.corpus import stopwords
|
|
|
|
| 13 |
nltk.download('stopwords')
|
| 14 |
nltk.download('punkt')
|
| 15 |
nltk.download('wordnet')
|
| 16 |
+
nltk.download('omw-1.4')
|
| 17 |
|
| 18 |
# Load Models
|
| 19 |
news_classifier = pipeline("text-classification", model="Oneli/News_Classification")
|
|
|
|
| 31 |
# Store classified article for QA
|
| 32 |
context_storage = {"context": "", "bulk_context": "", "num_articles": 0}
|
| 33 |
|
|
|
|
| 34 |
# Text Cleaning Functions
|
| 35 |
def clean_text(text):
|
| 36 |
text = text.lower()
|
|
|
|
| 42 |
tokens = [lemmatizer.lemmatize(word) for word in tokens] # Lemmatize tokens
|
| 43 |
return " ".join(tokens)
|
| 44 |
|
|
|
|
| 45 |
# Define the functions
|
| 46 |
def classify_text(text):
|
| 47 |
cleaned_text = clean_text(text)
|
|
|
|
| 54 |
|
| 55 |
return category, f"Confidence: {confidence}%"
|
| 56 |
|
|
|
|
| 57 |
def classify_csv(file):
|
| 58 |
try:
|
| 59 |
df = pd.read_csv(file, encoding="utf-8")
|
| 60 |
text_column = df.columns[0] # Assume first column is the text column
|
| 61 |
+
|
| 62 |
df[text_column] = df[text_column].astype(str).apply(clean_text) # Clean text column
|
| 63 |
df["Encoded Prediction"] = df[text_column].apply(lambda x: news_classifier(x)[0]['label'])
|
| 64 |
df["Decoded Prediction"] = df["Encoded Prediction"].map(label_mapping)
|
|
|
|
| 74 |
except Exception as e:
|
| 75 |
return None, f"Error: {str(e)}"
|
| 76 |
|
|
|
|
| 77 |
def chatbot_response(history, user_input, source):
|
| 78 |
user_input = user_input.lower()
|
| 79 |
context = context_storage["context"] if source == "Single Article" else context_storage["bulk_context"]
|
| 80 |
num_articles = context_storage["num_articles"]
|
| 81 |
+
|
| 82 |
if "number of articles" in user_input or "how many articles" in user_input:
|
| 83 |
answer = f"There are {num_articles} articles in the uploaded CSV."
|
| 84 |
history.append([user_input, answer])
|
| 85 |
return history, ""
|
| 86 |
+
|
| 87 |
if context:
|
| 88 |
result = qa_pipeline(question=user_input, context=context)
|
| 89 |
answer = result["answer"]
|
| 90 |
history.append([user_input, answer])
|
| 91 |
return history, ""
|
| 92 |
+
|
| 93 |
responses = {
|
| 94 |
"hello": "π Hello! How can I assist you with news today?",
|
| 95 |
"hi": "π Hi there! What do you want to know about news?",
|
|
|
|
| 97 |
"thank you": "π You're welcome! Let me know if you need anything else.",
|
| 98 |
"news": "π° I can classify news into Business, Sports, Politics, and more!",
|
| 99 |
}
|
| 100 |
+
response = responses.get(user_input, "π€ I'm here to help with news classification and general info. Ask me about news topics!")
|
|
|
|
| 101 |
history.append([user_input, response])
|
| 102 |
return history, ""
|
| 103 |
|
|
|
|
| 104 |
# Streamlit App Layout
|
| 105 |
st.set_page_config(page_title="News Classifier", page_icon="π°")
|
| 106 |
cover_image = Image.open("cover.png") # Ensure this image exists
|
| 107 |
+
st.image(cover_image, caption="News Classifier π’", use_container_width=True)
|
| 108 |
|
| 109 |
# Section for Single Article Classification
|
| 110 |
st.subheader("π° Single Article Classification")
|
|
|
|
| 143 |
st.write("*Chatbot Response:*")
|
| 144 |
for q, a in history:
|
| 145 |
st.write(f"*Q:* {q}")
|
| 146 |
+
st.write(f"*A:* {a}")
|