Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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("Use the menu on the left to select a NLP task to do (click on > if closed).")
|
| 7 |
+
|
| 8 |
+
expander = st.sidebar.expander('About')
|
| 9 |
+
expander.write("This web app allows you to perform common Natural Language Processing tasks, select a task below to get started.")
|
| 10 |
+
|
| 11 |
+
st.sidebar.header('What will you like to do?')
|
| 12 |
+
option = st.sidebar.radio('', ['Extractive question answering', 'Text summarization', 'Text generation', 'Sentiment analysis'])
|
| 13 |
+
|
| 14 |
+
@st.cache(show_spinner=False, allow_output_mutation=True)
|
| 15 |
+
def question_model():
|
| 16 |
+
question_answerer = pipeline("question-answering")
|
| 17 |
+
return question_answerer
|
| 18 |
+
|
| 19 |
+
@st.cache(show_spinner=False, allow_output_mutation=True)
|
| 20 |
+
def summarization_model():
|
| 21 |
+
summarizer = pipeline("summarization")
|
| 22 |
+
return summarizer
|
| 23 |
+
|
| 24 |
+
@st.cache(show_spinner=False, allow_output_mutation=True)
|
| 25 |
+
def generation_model():
|
| 26 |
+
generator = pipeline("text-generation")
|
| 27 |
+
return generator
|
| 28 |
+
|
| 29 |
+
@st.cache(show_spinner=False, allow_output_mutation=True)
|
| 30 |
+
def sentiment_model():
|
| 31 |
+
sentiment_analysis = pipeline("sentiment-analysis")
|
| 32 |
+
return sentiment_analysis
|
| 33 |
+
|
| 34 |
+
if option == 'Extractive question answering':
|
| 35 |
+
st.markdown("<h2 style='text-align: center; color:red;'>Extract answer from text</h2>", unsafe_allow_html=True)
|
| 36 |
+
sample_text = "sample text"
|
| 37 |
+
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"])
|
| 38 |
+
if source == "I want to input some text":
|
| 39 |
+
context = st.text_area('Use the example below or input your own text in English (between 1,000 and 10,000 characters)', value=sample_text, max_chars=10000, height=330)
|
| 40 |
+
question = st.text_input(label='Enter your question')
|
| 41 |
+
button = st.button('Get answer')
|
| 42 |
+
if button:
|
| 43 |
+
question_answerer = question_model()
|
| 44 |
+
with st.spinner(text="Getting answer..."):
|
| 45 |
+
answer = question_answerer(context=context, question=question)
|
| 46 |
+
st.write(answer["answer"])
|
| 47 |
+
elif source == "I want to upload a file":
|
| 48 |
+
uploaded_file = st.file_uploader("Choose a .txt file to upload", type=["txt"])
|
| 49 |
+
question = st.text_input(label='Enter your question')
|
| 50 |
+
button = st.button('Get answer')
|
| 51 |
+
if button:
|
| 52 |
+
question_answerer = question_model()
|
| 53 |
+
with st.spinner(text="Getting answer..."):
|
| 54 |
+
answer = question_answerer(context=context, question=question)
|
| 55 |
+
st.write(answer["answer"])
|
| 56 |
+
|
| 57 |
+
elif option == 'Text summarization':
|
| 58 |
+
st.markdown("<h2 style='text-align: center; color:red;'>Summarize text</h2>", unsafe_allow_html=True)
|
| 59 |
+
sample_text = "sample text"
|
| 60 |
+
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"])
|
| 61 |
+
if source == "I want to input some text":
|
| 62 |
+
text = st.text_area('Input a text in English (between 1,000 and 10,000 characters)', value=sample_text, max_chars=10000, height=330)
|
| 63 |
+
button = st.button('Get summary')
|
| 64 |
+
if button:
|
| 65 |
+
summarizer = summarization_model()
|
| 66 |
+
with st.spinner(text="Summarizing text..."):
|
| 67 |
+
summary = summarizer(text, max_length=130, min_length=30)
|
| 68 |
+
st.write(summary)
|
| 69 |
+
|
| 70 |
+
elif option == 'Text generation':
|
| 71 |
+
st.markdown("<h2 style='text-align: center; color:grey;'>Generate text</h2>", unsafe_allow_html=True)
|
| 72 |
+
text = st.text_input(label='Enter one line of text and let the NLP model generate the rest for you')
|
| 73 |
+
button = st.button('Generate text')
|
| 74 |
+
if button:
|
| 75 |
+
generator = generation_model()
|
| 76 |
+
with st.spinner(text="Generating text..."):
|
| 77 |
+
generated_text = generator(text, max_length=50)
|
| 78 |
+
st.write(generated_text[0]["generated_text"])
|
| 79 |
+
|
| 80 |
+
elif option == 'Sentiment analysis':
|
| 81 |
+
st.markdown("<h2 style='text-align: center; color:grey;'>Classify review</h2>", unsafe_allow_html=True)
|
| 82 |
+
text = st.text_input(label='Enter a sentence to get its sentiment analysis')
|
| 83 |
+
button = st.button('Get sentiment analysis')
|
| 84 |
+
if button:
|
| 85 |
+
sentiment_analysis = sentiment_model()
|
| 86 |
+
with st.spinner(text="Getting sentiment analysis..."):
|
| 87 |
+
sentiment = sentiment_analysis(text)
|
| 88 |
+
st.write(sentiment[0]["label"])
|