nguyennghia0902 commited on
Commit
8821400
1 Parent(s): 05bfc25

Update QuestionAnswering.py

Browse files
Files changed (1) hide show
  1. QuestionAnswering.py +74 -74
QuestionAnswering.py CHANGED
@@ -1,75 +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()
 
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/'+model_hf)
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()