import streamlit as st from stqdm import stqdm from time import sleep import spacy import spacy_streamlit from transformers import pipeline def draw_all(key, plot=False): st.markdown( ''' # NLP Web App This NLP-based Web App can do anything you can image with Text. # Key Features of this App. 1. Advanced Text Summarizer 2. Named Entity Recognition 3. Sentiment Analysis 4. Question Answering 5. Text Completion ''' ) def text_valid(raw_text): return raw_text!="" and raw_text!="Enter Your Text Here" with st.sidebar: draw_all("sidebar") def main(): st.title("NLP Web App") menu = ["--Select--", "Summarizer", "Named Entity Recognition", "Sentiment Analysis", "Question Answering", "Text Completion"] choice = st.sidebar.selectbox("Choose what you want to do", menu) if choice == "--Select--": st.markdown( ''' ## What is NLP? This NLP-based Web App can do anything you can imagine with Text. NLP is a field of linguistics and machine learning focused on understanding everything related to human language. The aim of NLP tasks is not only to understand single words individually, but to be able to understand the context of those words. The following is a list of common NLP tasks, with some examples of each: - **Classifying whole sentences**: Getting the sentiment of a review, detecting if an email is spam, determining if a sentence is grammatically correct or whether two sentences are logically related or not - **Classifying each word in a sentence**: Identifying the grammatical components of a sentence (noun, verb, adjective), or the named entities (person, location, organization) - **Generating text content**: Completing a prompt with auto-generated text, filling in the blanks in a text with masked words - **Extracting an answer from a text**: Given a question and a context, extracting the answer to the question based on the information provided in the context - **Generating a new sentence from an input text**: Translating a text into another language, summarizing a text ''' ) elif choice == "Summarizer": st.subheader("Text Summarization") st.write("Enter the Text you want to summarize!") raw_text = st.text_area("Your Text", "Enter Your Text Here") num_words = int(st.number_input("Enter Number of Words in Summary")) summarizer = pipeline("summarization") if text_valid(raw_text) and num_words!=0: summary = summarizer(raw_text, min_length=num_words)[0]['summary_text'] for _ in stqdm(range(50), desc="Please wait a bit. The model is fetching the results !!"): sleep(0.1) st.write(f"Here's the output:\n {summary}") elif choice == "Named Entity Recognition": st.subheader("Text-based Named Entity Recognition") st.write("Enter the Text below To extract Named Entities!") raw_text = st.text_area("Your Text", "Enter Your Text Here") nlp = spacy.load("en_core_web_sm") if text_valid(raw_text): doc = nlp(raw_text) for _ in stqdm(range(50), desc="Please wait a bit. The model is fetching the results !!"): sleep(0.1) spacy_streamlit.visualize_ner(doc, labels=nlp.get_pipe("ner").labels, title="List of Entities") elif choice == "Sentiment Analysis": st.subheader("Sentiment Analysis") st.write("Enter the Text you want to find out its Sentiment!") raw_text = st.text_area("Your Text", "Enter Your Text Here") analyzer = pipeline("sentiment-analysis") if text_valid(raw_text): sentiment = analyzer(raw_text)[0] for _ in stqdm(range(50), desc="Please wait a bit. The model is fetching the results !!"): sleep(0.1) st.write(f"Here's the output:\n {sentiment}") elif choice == "Question Answering": st.subheader("Question Answering") st.write("Enter the Text you want to find out its Sentiment!") context = st.text_area("Context", "Enter Your Text Here") question = st.text_area("Your Question", "Enter Your Text Here") question_answering = pipeline("question-answering") if text_valid(context) and text_valid(context): answer = question_answering(question=question, context=context)['answer'] for _ in stqdm(range(50), desc="Please wait a bit. The model is fetching the results !!"): sleep(0.1) st.write(f"Here's the output:\n {answer}") elif choice == "Text Completion": st.subheader("Text Completion") st.write("Enter the uncomplete Text to complete it automatically using AI!") raw_text = st.text_area("Your Text", "Enter Your Text Here") completor = pipeline("text-generation") if text_valid(raw_text): generated_text = completor(raw_text)[0]['generated_text'] for _ in stqdm(range(50), desc="Please wait a bit. The model is fetching the results !!"): sleep(0.1) st.write(f"Here's the output:\n {generated_text}") if __name__ == "__main__": main()