kxx-kkk commited on
Commit
de5d292
1 Parent(s): 264dc37

Initiate changes

Browse files
Files changed (3) hide show
  1. app.py +58 -0
  2. requirements.txt +3 -0
  3. sample.txt +15 -0
app.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import pipeline
3
+
4
+ st.set_page_config(page_title="Common NLP Tasks")
5
+ st.title("Common NLP Tasks")
6
+ st.subheader(":point_left: Use the menu on the left to select a NLP task (click on > if closed).")
7
+ """
8
+ [![](https://img.shields.io/github/followers/OOlajide?label=OOlajide&style=social)](https://gitHub.com/OOlajide)
9
+ [![](https://img.shields.io/twitter/follow/sageOlamide?label=@sageOlamide&style=social)](https://twitter.com/sageOlamide)
10
+ """
11
+ # expander = st.sidebar.expander("About")
12
+ # expander.write("This web app allows you to perform common Natural Language Processing tasks, select a task below to get started.")
13
+
14
+ # st.sidebar.header("What will you like to do?")
15
+ # option = st.sidebar.radio("", ["Text summarization", "Extractive question answering", "Text generation"])
16
+
17
+ @st.cache(show_spinner=False, allow_output_mutation=True)
18
+ def question_model():
19
+ model_name = "deepset/tinyroberta-squad2"
20
+ question_answerer = pipeline(model=model_name, tokenizer=model_name, task="question-answering")
21
+ return question_answerer
22
+
23
+
24
+ if option == "Extractive question answering":
25
+ st.markdown("<h2 style='text-align: center; color:grey;'>Extractive Question Answering</h2>", unsafe_allow_html=True)
26
+ st.markdown("<h3 style='text-align: left; color:#F63366; font-size:18px;'><b>What is extractive question answering about?<b></h3>", unsafe_allow_html=True)
27
+ st.write("Extractive question answering is a Natural Language Processing task where text is provided for a model so that the model can refer to it and make predictions about where the answer to a question is.")
28
+ st.markdown('___')
29
+ source = st.radio("How would you like to start? Choose an option below", ["I want to input some text", "I want to upload a file"])
30
+ sample_question = "What did the shepherd boy do to amuse himself?"
31
+ if source == "I want to input some text":
32
+ with open("sample.txt", "r") as text_file:
33
+ sample_text = text_file.read()
34
+ context = st.text_area("Use the example below or input your own text in English (10,000 characters max)", value=sample_text, max_chars=10000, height=330)
35
+ question = st.text_input(label="Use the question below or enter your own question", value=sample_question)
36
+ button = st.button("Get answer")
37
+ if button:
38
+ with st.spinner(text="Loading question model..."):
39
+ question_answerer = question_model()
40
+ with st.spinner(text="Getting answer..."):
41
+ answer = question_answerer(context=context, question=question)
42
+ answer = answer["answer"]
43
+ st.text(answer)
44
+ elif source == "I want to upload a file":
45
+ uploaded_file = st.file_uploader("Choose a .txt file to upload", type=["txt"])
46
+ if uploaded_file is not None:
47
+ raw_text = str(uploaded_file.read(),"utf-8")
48
+ context = st.text_area("", value=raw_text, height=330)
49
+ question = st.text_input(label="Enter your question", value=sample_question)
50
+ button = st.button("Get answer")
51
+ if button:
52
+ with st.spinner(text="Loading summarization model..."):
53
+ question_answerer = question_model()
54
+ with st.spinner(text="Getting answer..."):
55
+ answer = question_answerer(context=context, question=question)
56
+ answer = answer["answer"]
57
+ st.text(answer)
58
+
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ streamlit==1.8.0
2
+ transformers==4.18.0
3
+ torch==1.11.0
sample.txt ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ A Shepherd Boy tended his master's Sheep near a dark forest not far from the village. Soon he found life in the pasture very dull. All he could do to amuse himself was to talk to his dog or play on his shepherd's pipe.
2
+
3
+ One day as he sat watching the Sheep and the quiet forest, and thinking what he would do should he see a Wolf, he thought of a plan to amuse himself.
4
+
5
+ His Master had told him to call for help should a Wolf attack the flock, and the Villagers would drive it away. So now, though he had not seen anything that even looked like a Wolf, he ran toward the village shouting at the top of his voice, "Wolf! Wolf!"
6
+
7
+ As he expected, the Villagers who heard the cry dropped their work and ran in great excitement to the pasture. But when they got there they found the Boy doubled up with laughter at the trick he had played on them.
8
+
9
+ A few days later the Shepherd Boy again shouted, "Wolf! Wolf!" Again the Villagers ran to help him, only to be laughed at again.
10
+
11
+ Then one evening as the sun was setting behind the forest and the shadows were creeping out over the pasture, a Wolf really did spring from the underbrush and fall upon the Sheep.
12
+
13
+ In terror the Boy ran toward the village shouting "Wolf! Wolf!" But though the Villagers heard the cry, they did not run to help him as they had before. "He cannot fool us again," they said.
14
+
15
+ The Wolf killed a great many of the Boy's sheep and then slipped away into the forest.