Spaces:
Sleeping
Sleeping
Update pages/2_QA.py
Browse files- pages/2_QA.py +43 -36
pages/2_QA.py
CHANGED
|
@@ -1,6 +1,4 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import pandas as pd
|
| 3 |
-
import matplotlib.pyplot as plt
|
| 4 |
from transformers import pipeline
|
| 5 |
|
| 6 |
@st.cache_resource
|
|
@@ -12,38 +10,47 @@ def load_qa_model():
|
|
| 12 |
|
| 13 |
qa_model = load_qa_model()
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
st.markdown("---")
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
except Exception as e:
|
| 49 |
-
st.error(f"Error reading or processing file: {e}")
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
@st.cache_resource
|
|
|
|
| 10 |
|
| 11 |
qa_model = load_qa_model()
|
| 12 |
|
| 13 |
+
st.title("Question Answering")
|
| 14 |
+
|
| 15 |
+
if "df" not in st.session_state:
|
| 16 |
+
st.warning("Please upload and classify a CSV on the Classification page first.")
|
| 17 |
+
st.stop()
|
| 18 |
+
|
| 19 |
+
df = st.session_state["df"]
|
| 20 |
+
|
| 21 |
+
if "content" not in df.columns:
|
| 22 |
+
st.error("The dataframe must contain a `content` column.")
|
| 23 |
+
st.stop()
|
| 24 |
+
|
| 25 |
st.markdown("---")
|
| 26 |
+
st.subheader("Question Answering")
|
| 27 |
+
|
| 28 |
+
article_labels = [
|
| 29 |
+
f"Article {i + 1}: {text[:100]}{'...' if len(text) > 100 else ''}"
|
| 30 |
+
for i, text in enumerate(df["content"].fillna("").astype(str))
|
| 31 |
+
]
|
| 32 |
+
|
| 33 |
+
selected_label = st.selectbox("Choose an article", article_labels)
|
| 34 |
+
selected_index = article_labels.index(selected_label)
|
| 35 |
+
selected_article = df["content"].fillna("").astype(str).iloc[selected_index]
|
| 36 |
+
|
| 37 |
+
st.write("### Selected Article")
|
| 38 |
+
st.write(selected_article)
|
| 39 |
+
|
| 40 |
+
question = st.text_input("Ask a question about the selected article")
|
| 41 |
+
|
| 42 |
+
if st.button("Get Answer"):
|
| 43 |
+
if not question.strip():
|
| 44 |
+
st.warning("Please enter a question.")
|
| 45 |
+
elif not selected_article.strip():
|
| 46 |
+
st.warning("The selected article is empty.")
|
| 47 |
+
else:
|
| 48 |
+
with st.spinner("Finding answer..."):
|
| 49 |
+
result = qa_model(
|
| 50 |
+
question=question,
|
| 51 |
+
context=selected_article[:3000]
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
st.success("Answer found.")
|
| 55 |
+
st.write("### Answer")
|
| 56 |
+
st.write(result["answer"])
|
|
|
|
|
|
|
|
|