Spaces:
Build error
Build error
Upload 3 files
Browse files- QuestionAnswering.py +75 -0
- app.py +46 -0
- requirements.txt +7 -0
QuestionAnswering.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from os import path
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import tensorflow as tf
|
| 4 |
+
from transformers import ElectraTokenizerFast, TFElectraForQuestionAnswering
|
| 5 |
+
|
| 6 |
+
model_hf = 'nguyennghia0902/bestfailed_electra-small-discriminator_5e-05_16'
|
| 7 |
+
tokenizer = ElectraTokenizerFast.from_pretrained(model_hf)
|
| 8 |
+
reload_model = TFElectraForQuestionAnswering.from_pretrained(model_hf)
|
| 9 |
+
|
| 10 |
+
@st.cache_resource
|
| 11 |
+
def predict(question, context):
|
| 12 |
+
inputs = tokenizer(question, context, return_offsets_mapping=True,return_tensors="tf",max_length=512, truncation=True)
|
| 13 |
+
offset_mapping = inputs.pop("offset_mapping")
|
| 14 |
+
outputs = reload_model(**inputs)
|
| 15 |
+
answer_start_index = int(tf.math.argmax(outputs.start_logits, axis=-1)[0])
|
| 16 |
+
answer_end_index = int(tf.math.argmax(outputs.end_logits, axis=-1)[0])
|
| 17 |
+
start_char = offset_mapping[0][answer_start_index][0]
|
| 18 |
+
end_char = offset_mapping[0][answer_end_index][1]
|
| 19 |
+
predicted_answer_text = context[start_char:end_char]
|
| 20 |
+
|
| 21 |
+
return predicted_answer_text
|
| 22 |
+
|
| 23 |
+
def main():
|
| 24 |
+
st.set_page_config(page_title="Question Answering", page_icon="📝")
|
| 25 |
+
|
| 26 |
+
# giving a title to our page
|
| 27 |
+
col1, col2 = st.columns([2, 1])
|
| 28 |
+
col1.title("Question Answering")
|
| 29 |
+
|
| 30 |
+
col2.link_button("Explore my model", "https://huggingface.co/nguyennghia0902/electra-small-discriminator_5e-05_32")
|
| 31 |
+
|
| 32 |
+
question = st.text_area(
|
| 33 |
+
"QUESTION: Please enter a question:",
|
| 34 |
+
placeholder="Enter your question here",
|
| 35 |
+
height=15,
|
| 36 |
+
)
|
| 37 |
+
text = st.text_area(
|
| 38 |
+
"CONTEXT: Please enter a context:",
|
| 39 |
+
placeholder="Enter your context here",
|
| 40 |
+
height=100,
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
prediction = ""
|
| 44 |
+
|
| 45 |
+
upload_file = st.file_uploader("CONTEXT: Or upload a file with some contexts", type=["txt"])
|
| 46 |
+
if upload_file is not None:
|
| 47 |
+
text = upload_file.read().decode("utf-8")
|
| 48 |
+
|
| 49 |
+
for line in text.splitlines():
|
| 50 |
+
line = line.strip()
|
| 51 |
+
if not line:
|
| 52 |
+
continue
|
| 53 |
+
|
| 54 |
+
prediction = predict(question, line)
|
| 55 |
+
|
| 56 |
+
st.success(line + "\n\n" + prediction)
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
# Create a prediction button
|
| 60 |
+
elif st.button("Predict"):
|
| 61 |
+
prediction = ""
|
| 62 |
+
stripped_text = text.strip()
|
| 63 |
+
if not stripped_text:
|
| 64 |
+
st.error("Please enter a context.")
|
| 65 |
+
return
|
| 66 |
+
stripped_question = question.strip()
|
| 67 |
+
if not stripped_question:
|
| 68 |
+
st.error("Please enter a question.")
|
| 69 |
+
return
|
| 70 |
+
|
| 71 |
+
prediction = predict(stripped_question, stripped_text)
|
| 72 |
+
st.success(prediction)
|
| 73 |
+
|
| 74 |
+
if __name__ == "__main__":
|
| 75 |
+
main()
|
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from st_pages import Page, show_pages
|
| 3 |
+
|
| 4 |
+
st.set_page_config(page_title="Question Answering", page_icon="🏠")
|
| 5 |
+
|
| 6 |
+
show_pages(
|
| 7 |
+
[
|
| 8 |
+
Page("app.py", "Home", "🏠"),
|
| 9 |
+
Page(
|
| 10 |
+
"QuestionAnswering.py", "Question Answering", "📝"
|
| 11 |
+
),
|
| 12 |
+
]
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
st.title("Project in Text Mining and Application")
|
| 16 |
+
st.header("Question Answering use a pre-trained model - ELECTRA")
|
| 17 |
+
st.markdown(
|
| 18 |
+
"""
|
| 19 |
+
**Team members:**
|
| 20 |
+
| Student ID | Full Name | Email |
|
| 21 |
+
| ---------- | ------------------------ | ------------------------------ |
|
| 22 |
+
| 1712603 | Lê Quang Nam | [email protected] |
|
| 23 |
+
| 19120582 | Lê Nhựt Minh | [email protected] |
|
| 24 |
+
| 19120600 | Bùi Nguyên Nghĩa | [email protected] |
|
| 25 |
+
| 21120198 | Nguyễn Thị Lan Anh | [email protected] |
|
| 26 |
+
"""
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
st.header("The Need for Question Answering")
|
| 30 |
+
st.markdown(
|
| 31 |
+
"""
|
| 32 |
+
...
|
| 33 |
+
"""
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
st.header("Technology used")
|
| 37 |
+
st.markdown(
|
| 38 |
+
"""
|
| 39 |
+
The ELECTRA model, specifically the "google/electra-small-discriminator" used here,
|
| 40 |
+
is a deep learning model in the field of natural language processing (NLP) developed
|
| 41 |
+
by Google. This model is an intelligent variation of the supervised learning model
|
| 42 |
+
based on the Transformer architecture, designed to understand and process natural language efficiently.
|
| 43 |
+
For this Question Answering task, we choose two related classes: ElectraTokenizerFast and
|
| 44 |
+
TFElectraForQuestionAnswering to implement.
|
| 45 |
+
"""
|
| 46 |
+
)
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
numpy
|
| 3 |
+
pandas
|
| 4 |
+
tensorflow
|
| 5 |
+
streamlit
|
| 6 |
+
st-pages
|
| 7 |
+
tf-keras
|