Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -25,7 +25,7 @@ st.markdown(
|
|
25 |
<style>
|
26 |
body {background-color: #f4f4f4;}
|
27 |
.title {text-align: center; font-size: 36px; font-weight: bold; color: #ff4b4b;}
|
28 |
-
.subheader {
|
29 |
.stTextInput>div>div>input {border-radius: 10px;}
|
30 |
.stTextArea>div>div>textarea {border-radius: 10px;}
|
31 |
.stButton>button {border-radius: 10px; background-color: #ff4b4b; color: white; font-weight: bold;}
|
@@ -36,59 +36,61 @@ st.markdown(
|
|
36 |
|
37 |
st.markdown('<h1 class="title">π° News Classification & Q&A App</h1>', unsafe_allow_html=True)
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
42 |
|
43 |
-
uploaded_file = st.file_uploader("Choose a CSV file", type="csv")
|
44 |
|
45 |
-
if uploaded_file is not None:
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
if 'content' not in df.columns:
|
52 |
-
st.error("β Error: The uploaded CSV must contain a 'content' column.")
|
53 |
-
else:
|
54 |
-
st.success("β
File successfully uploaded!")
|
55 |
-
st.write("Preview of uploaded data:")
|
56 |
-
st.dataframe(df.head())
|
57 |
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
|
78 |
-
# ====================== Component 2: Q&A ====================== #
|
79 |
-
st.markdown('<h2 class="subheader">β Ask a Question About the News</h2>', unsafe_allow_html=True)
|
80 |
-
st.markdown("Enter a question and provide a news article to get an answer.")
|
81 |
|
82 |
-
question = st.text_input("π Ask a question:")
|
83 |
-
context = st.text_area("π Provide the news article or content:", height=150)
|
84 |
|
85 |
-
if question and context.strip():
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
|
|
25 |
<style>
|
26 |
body {background-color: #f4f4f4;}
|
27 |
.title {text-align: center; font-size: 36px; font-weight: bold; color: #ff4b4b;}
|
28 |
+
.subheader {font-size: 24px; color: #333; margin-bottom: 20px; text-align: right;}
|
29 |
.stTextInput>div>div>input {border-radius: 10px;}
|
30 |
.stTextArea>div>div>textarea {border-radius: 10px;}
|
31 |
.stButton>button {border-radius: 10px; background-color: #ff4b4b; color: white; font-weight: bold;}
|
|
|
36 |
|
37 |
st.markdown('<h1 class="title">π° News Classification & Q&A App</h1>', unsafe_allow_html=True)
|
38 |
|
39 |
+
col1, col2 = st.columns([1, 2])
|
40 |
+
with col2:
|
41 |
+
# ====================== Component 1: News Classification ====================== #
|
42 |
+
st.markdown('<h2 class="subheader">π Classify News Articles</h2>', unsafe_allow_html=True)
|
43 |
+
st.markdown("Upload a CSV file with a 'content' column to classify news into categories.")
|
44 |
|
45 |
+
uploaded_file = st.file_uploader("Choose a CSV file", type="csv")
|
46 |
|
47 |
+
if uploaded_file is not None:
|
48 |
+
try:
|
49 |
+
df = pd.read_csv(uploaded_file, encoding="utf-8") # Handle encoding issues
|
50 |
+
except UnicodeDecodeError:
|
51 |
+
df = pd.read_csv(uploaded_file, encoding="ISO-8859-1")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
+
if 'content' not in df.columns:
|
54 |
+
st.error("β Error: The uploaded CSV must contain a 'content' column.")
|
55 |
+
else:
|
56 |
+
st.success("β
File successfully uploaded!")
|
57 |
+
st.write("Preview of uploaded data:")
|
58 |
+
st.dataframe(df.head())
|
59 |
+
|
60 |
+
# Preprocessing function to clean the text
|
61 |
+
def preprocess_text(text):
|
62 |
+
text = text.lower() # Convert to lowercase
|
63 |
+
text = re.sub(r'\s+', ' ', text) # Remove extra spaces
|
64 |
+
text = re.sub(r'[^a-z\s]', '', text) # Remove special characters & numbers
|
65 |
+
return text
|
66 |
|
67 |
+
# Apply preprocessing and classification
|
68 |
+
df['processed_content'] = df['content'].apply(preprocess_text)
|
69 |
+
df['class'] = df['processed_content'].apply(lambda x: text_classification_pipeline(x)[0]['label'] if x.strip() else "Unknown")
|
70 |
+
|
71 |
+
# Show results
|
72 |
+
st.markdown("### πΉ Classification Results:")
|
73 |
+
st.dataframe(df[['content', 'class']])
|
74 |
+
|
75 |
+
# Provide CSV download
|
76 |
+
output = io.BytesIO()
|
77 |
+
df.to_csv(output, index=False, encoding="utf-8-sig")
|
78 |
+
st.download_button(label="β¬οΈ Download classified news", data=output.getvalue(), file_name="classified_news.csv", mime="text/csv")
|
79 |
|
80 |
+
# ====================== Component 2: Q&A ====================== #
|
81 |
+
st.markdown('<h2 class="subheader">β Ask a Question About the News</h2>', unsafe_allow_html=True)
|
82 |
+
st.markdown("Enter a question and provide a news article to get an answer.")
|
83 |
|
84 |
+
question = st.text_input("π Ask a question:")
|
85 |
+
context = st.text_area("π Provide the news article or content:", height=150)
|
86 |
|
87 |
+
if question and context.strip():
|
88 |
+
model_name_qa = "distilbert-base-uncased-distilled-squad"
|
89 |
+
qa_pipeline = pipeline("question-answering", model=model_name_qa, tokenizer=model_name_qa)
|
90 |
+
result = qa_pipeline(question=question, context=context)
|
91 |
+
|
92 |
+
# Display Answer
|
93 |
+
if 'answer' in result and result['answer']:
|
94 |
+
st.markdown(f"### β
Answer: {result['answer']}")
|
95 |
+
else:
|
96 |
+
st.markdown("### β No answer found in the provided content.")
|