Spaces:
Sleeping
Sleeping
Update streamlit_app.py
Browse files- streamlit_app.py +49 -6
streamlit_app.py
CHANGED
|
@@ -2,20 +2,27 @@ import streamlit as st
|
|
| 2 |
import pandas as pd
|
| 3 |
from transformers import pipeline
|
| 4 |
|
| 5 |
-
|
| 6 |
-
st.set_page_config(page_title="News Classifier", page_icon="📰")
|
| 7 |
|
| 8 |
@st.cache_resource
|
| 9 |
-
def
|
| 10 |
return pipeline(
|
| 11 |
"text-classification",
|
| 12 |
model="SuchithTissera/text_analytics_model"
|
| 13 |
)
|
| 14 |
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
st.title("News Classifier")
|
| 18 |
-
st.write("Upload a CSV file with a `content` column. The app will add a
|
| 19 |
|
| 20 |
uploaded_file = st.file_uploader("Upload CSV", type=["csv"])
|
| 21 |
|
|
@@ -34,6 +41,8 @@ if uploaded_file is not None:
|
|
| 34 |
df["class"] = [pred["label"] for pred in preds]
|
| 35 |
|
| 36 |
st.success("Classification complete.")
|
|
|
|
|
|
|
| 37 |
st.dataframe(df.head())
|
| 38 |
|
| 39 |
output_csv = df.to_csv(index=False).encode("utf-8")
|
|
@@ -45,5 +54,39 @@ if uploaded_file is not None:
|
|
| 45 |
mime="text/csv"
|
| 46 |
)
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
except Exception as e:
|
| 49 |
st.error(f"Error reading or processing file: {e}")
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
from transformers import pipeline
|
| 4 |
|
| 5 |
+
st.set_page_config(page_title="News Classifier + Q&A", page_icon="📰")
|
|
|
|
| 6 |
|
| 7 |
@st.cache_resource
|
| 8 |
+
def load_classifier():
|
| 9 |
return pipeline(
|
| 10 |
"text-classification",
|
| 11 |
model="SuchithTissera/text_analytics_model"
|
| 12 |
)
|
| 13 |
|
| 14 |
+
@st.cache_resource
|
| 15 |
+
def load_qa_model():
|
| 16 |
+
return pipeline(
|
| 17 |
+
"question-answering",
|
| 18 |
+
model="distilbert-base-cased-distilled-squad"
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
classifier = load_classifier()
|
| 22 |
+
qa_model = load_qa_model()
|
| 23 |
|
| 24 |
+
st.title("News Classifier + Q&A")
|
| 25 |
+
st.write("Upload a CSV file with a `content` column. The app will classify each record, add a `class` column, and let you ask questions about a selected article.")
|
| 26 |
|
| 27 |
uploaded_file = st.file_uploader("Upload CSV", type=["csv"])
|
| 28 |
|
|
|
|
| 41 |
df["class"] = [pred["label"] for pred in preds]
|
| 42 |
|
| 43 |
st.success("Classification complete.")
|
| 44 |
+
|
| 45 |
+
st.subheader("Preview of Classified Data")
|
| 46 |
st.dataframe(df.head())
|
| 47 |
|
| 48 |
output_csv = df.to_csv(index=False).encode("utf-8")
|
|
|
|
| 54 |
mime="text/csv"
|
| 55 |
)
|
| 56 |
|
| 57 |
+
st.markdown("---")
|
| 58 |
+
st.subheader("Question Answering")
|
| 59 |
+
|
| 60 |
+
article_options = [
|
| 61 |
+
f"Article {i}: {text[:100]}..." if len(text) > 100 else f"Article {i}: {text}"
|
| 62 |
+
for i, text in enumerate(df["content"].fillna("").astype(str).tolist())
|
| 63 |
+
]
|
| 64 |
+
|
| 65 |
+
selected_index = st.selectbox(
|
| 66 |
+
"Choose an article",
|
| 67 |
+
options=range(len(article_options)),
|
| 68 |
+
format_func=lambda i: article_options[i]
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
selected_article = df["content"].fillna("").astype(str).iloc[selected_index]
|
| 72 |
+
|
| 73 |
+
st.write("### Selected Article")
|
| 74 |
+
st.write(selected_article)
|
| 75 |
+
|
| 76 |
+
question = st.text_input("Ask a question about the selected article")
|
| 77 |
+
|
| 78 |
+
if st.button("Get Answer"):
|
| 79 |
+
if not question.strip():
|
| 80 |
+
st.warning("Please enter a question.")
|
| 81 |
+
elif not selected_article.strip():
|
| 82 |
+
st.warning("The selected article is empty.")
|
| 83 |
+
else:
|
| 84 |
+
with st.spinner("Finding answer..."):
|
| 85 |
+
result = qa_model(question=question, context=selected_article)
|
| 86 |
+
|
| 87 |
+
st.success("Answer found.")
|
| 88 |
+
st.write("### Answer")
|
| 89 |
+
st.write(result["answer"])
|
| 90 |
+
|
| 91 |
except Exception as e:
|
| 92 |
st.error(f"Error reading or processing file: {e}")
|