Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,90 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
question_answerer = pipeline("question-answering", model=model_checkpoint)
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
+
# Set page configuration
|
| 5 |
+
st.set_page_config(
|
| 6 |
+
page_title="Question Answering App",
|
| 7 |
+
page_icon="❓",
|
| 8 |
+
layout="centered",
|
| 9 |
+
initial_sidebar_state="auto",
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
# Page title with custom style
|
| 13 |
+
st.markdown(
|
| 14 |
+
"""
|
| 15 |
+
<h1 style="text-align: center; color: #4CAF50; font-family: 'Helvetica';">
|
| 16 |
+
📚 Question Answering App
|
| 17 |
+
</h1>
|
| 18 |
+
<p style="text-align: center; color: #777; font-size: 18px;">
|
| 19 |
+
Enter a context and question to get precise answers powered by AI.
|
| 20 |
+
</p>
|
| 21 |
+
""",
|
| 22 |
+
unsafe_allow_html=True,
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
# Sidebar
|
| 26 |
+
st.sidebar.header("Model Settings")
|
| 27 |
+
model_checkpoint = st.sidebar.text_input(
|
| 28 |
+
"Model Checkpoint", "Diezu/viedumrc", help="Specify the model checkpoint to use."
|
| 29 |
+
)
|
| 30 |
+
st.sidebar.markdown(
|
| 31 |
+
"""
|
| 32 |
+
<small>Using default model: <code>Diezu/viedumrc</code>.</small>
|
| 33 |
+
""",
|
| 34 |
+
unsafe_allow_html=True,
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
# Initialize model pipeline
|
| 38 |
question_answerer = pipeline("question-answering", model=model_checkpoint)
|
| 39 |
|
| 40 |
+
# Main application
|
| 41 |
+
st.markdown(
|
| 42 |
+
"""
|
| 43 |
+
<h2 style="color: #2196F3;">Provide Context and Question</h2>
|
| 44 |
+
""",
|
| 45 |
+
unsafe_allow_html=True,
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
context = st.text_area(
|
| 49 |
+
"Context",
|
| 50 |
+
"",
|
| 51 |
+
help="Paste the context where the answer can be found.",
|
| 52 |
+
height=200,
|
| 53 |
+
placeholder="Enter your context here...",
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
question = st.text_input(
|
| 57 |
+
"Question",
|
| 58 |
+
"",
|
| 59 |
+
help="Write the question you want to ask about the provided context.",
|
| 60 |
+
placeholder="What is your question?",
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
if st.button("Get Answer"):
|
| 64 |
+
if context.strip() == "" or question.strip() == "":
|
| 65 |
+
st.warning("Please provide both context and a question!")
|
| 66 |
+
else:
|
| 67 |
+
try:
|
| 68 |
+
result = question_answerer(question=question, context=context)
|
| 69 |
+
st.success("Answer Found!")
|
| 70 |
+
st.markdown(
|
| 71 |
+
f"""
|
| 72 |
+
<div style="background-color: #f1f8ff; border-left: 4px solid #2196F3; padding: 10px; margin-top: 10px;">
|
| 73 |
+
<strong>Answer:</strong> {result['answer']}
|
| 74 |
+
</div>
|
| 75 |
+
""",
|
| 76 |
+
unsafe_allow_html=True,
|
| 77 |
+
)
|
| 78 |
+
except Exception as e:
|
| 79 |
+
st.error(f"Error: {e}")
|
| 80 |
|
| 81 |
+
# Footer
|
| 82 |
+
st.markdown(
|
| 83 |
+
"""
|
| 84 |
+
<hr>
|
| 85 |
+
<footer style="text-align: center; font-size: small; color: #888;">
|
| 86 |
+
Built with ❤️ using <strong>Streamlit</strong> and <strong>Transformers</strong>.
|
| 87 |
+
</footer>
|
| 88 |
+
""",
|
| 89 |
+
unsafe_allow_html=True,
|
| 90 |
+
)
|