Spaces:
Runtime error
Runtime error
import streamlit as st | |
from time import sleep | |
from stqdm import stqdm | |
import pandas as pd | |
from transformers import pipeline | |
import json | |
def draw_all( | |
key, | |
plot=False, | |
): | |
st.write( | |
""" | |
# NLP Web App | |
This Natural Language Processing Based Web App can do anything u can imagine with Text. π± | |
This App is built using pretrained transformers which are capable of doing wonders with the Textual data. | |
```python | |
# Key Features of this App. | |
1. Advanced Text Summarizer | |
2. Sentiment Analysis | |
3. Question Answering | |
4. Text Completion | |
``` | |
""" | |
) | |
with st.sidebar: | |
draw_all("sidebar") | |
#main function that holds all the options | |
def main(): | |
st.title("NLP IE Web App") | |
menu = ["--Select--","Summarizer", | |
"Sentiment Analysis","Question Answering","Text Completion"] | |
choice = st.sidebar.selectbox("What task would you like to do?", menu) | |
if choice=="--Select--": | |
st.write(""" | |
Welcome to the the Web App of Data Dynamos. As an IE student of the Master of Business Analyitics and Big Data you have the opportunity to | |
do anything with your lectures you like | |
""") | |
st.write(""" | |
Never heard of NLP? No way! Natural Language Processing (NLP) is a computational technique | |
to process human language in all of it's complexity | |
""") | |
st.write(""" | |
NLP is an vital discipline in Artificial Intelligence and keeps growing | |
""") | |
st.image('banner_image.jpg') | |
elif choice=="Summarizer": | |
st.subheader("Text Summarization") | |
st.write(" Enter the Text you want to summarize !") | |
documents = { | |
"Document 1": "This is the text for document 1.", | |
"Document 2": "This is the text for document 2.", | |
"Document 3": "This is the text for document 3." | |
} | |
document_name = st.selectbox("Select a document", list(documents.keys())) | |
raw_text = documents[document_name] | |
num_words = st.number_input("Enter Number of Words in Summary") | |
if raw_text!="" and num_words is not None: | |
num_words = int(num_words) | |
summarizer = pipeline('summarization',model="philschmid/bart-large-cnn-samsum") | |
summary = summarizer(raw_text, min_length=num_words,max_length=50) | |
s1 = json.dumps(summary[0]) | |
d2 = json.loads(s1) | |
result_summary = d2['summary_text'] | |
result_summary = '. '.join(list(map(lambda x: x.strip().capitalize(), result_summary.split('.')))) | |
st.write(f"Here's your Summary : {result_summary}") | |
elif choice=="Sentiment Analysis": | |
st.subheader("Sentiment Analysis") | |
#loading the pipeline | |
sentiment_analysis = pipeline("sentiment-analysis") | |
st.write(" Enter the Text below To find out its Sentiment !") | |
raw_text = st.text_area("Your Text","Enter Text Here") | |
if raw_text !="Enter Text Here": | |
result = sentiment_analysis(raw_text)[0] | |
sentiment = result['label'] | |
for _ in stqdm(range(50), desc="Please wait a bit. The model is fetching the results !!"): | |
sleep(0.1) | |
if sentiment =="POSITIVE": | |
st.write("""# This text has a Positive Sentiment. π€""") | |
elif sentiment =="NEGATIVE": | |
st.write("""# This text has a Negative Sentiment. π€""") | |
elif sentiment =="NEUTRAL": | |
st.write("""# This text seems Neutral ... π""") | |
elif choice=="Question Answering": | |
st.subheader("Question Answering") | |
st.write(" Enter the Context and ask the Question to find out the Answer !") | |
question_answering = pipeline("question-answering") | |
context = st.text_area("Context","Enter the Context Here") | |
#This is the text box for the question | |
question = st.text_area("Your Question","Enter your Question Here") | |
if context !="Enter Text Here" and question!="Enter your Question Here": | |
#we are passing question and the context | |
result = question_answering(question=question, context=context) | |
#dump the result in json and load it again | |
s1 = json.dumps(result) | |
d2 = json.loads(s1) | |
generated_text = d2['answer'] | |
#joining and capalizing by dot | |
generated_text = '. '.join(list(map(lambda x: x.strip().capitalize(), generated_text.split('.')))) | |
st.write(f" Here's your Answer :\n {generated_text}") | |
elif choice=="Text Completion": | |
st.subheader("Text Completion") | |
st.write(" Enter the uncomplete Text to complete it automatically using AI !") | |
text_generation = pipeline("text-generation") | |
message = st.text_area("Your Text","Enter the Text to complete") | |
if message !="Enter the Text to complete": | |
generator = text_generation(message) | |
s1 = json.dumps(generator[0]) | |
d2 = json.loads(s1) | |
generated_text = d2['generated_text'] | |
generated_text = '. '.join(list(map(lambda x: x.strip().capitalize(), generated_text.split('.')))) | |
st.write(f" Here's your Generate Text :\n {generated_text}") | |
#main function to run | |
if __name__ == '__main__': | |
main() |