domro11 commited on
Commit
b18868d
·
1 Parent(s): 036e44e

update app.py

Browse files
Files changed (1) hide show
  1. app.py +143 -7
app.py CHANGED
@@ -1,10 +1,146 @@
1
- #adding
2
- import streamlit as st
 
 
3
  from transformers import pipeline
 
 
 
4
 
5
- pipe = pipeline('sentiment-analysis')
6
- text = st.text_area('enter some text!')
7
 
8
- if text:
9
- out = pipe(text)
10
- st.json(out)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from time import sleep
3
+ from stqdm import stqdm
4
+ import pandas as pd
5
  from transformers import pipeline
6
+ import json
7
+ import spacy
8
+ import spacy_streamlit
9
 
 
 
10
 
11
+ def draw_all(
12
+ key,
13
+ plot=False,
14
+ ):
15
+ st.write(
16
+ """
17
+ # NLP Web App
18
+
19
+ This Natural Language Processing Based Web App can do anything u can imagine with Text. 😱
20
+
21
+ This App is built using pretrained transformers which are capable of doing wonders with the Textual data.
22
+
23
+ ```python
24
+ # Key Features of this App.
25
+ 1. Advanced Text Summarizer
26
+ 2. Sentiment Analysis
27
+ 3. Question Answering
28
+ 4. Text Completion
29
+
30
+ ```
31
+ """
32
+ )
33
+
34
+
35
+
36
+ with st.sidebar:
37
+ draw_all("sidebar")
38
+
39
+
40
+ #main function that holds all the options
41
+ def main():
42
+ st.title("NLP IE Web App")
43
+ menu = ["--Select--","Summarizer",
44
+ "Sentiment Analysis","Question Answering","Text Completion"]
45
+ choice = st.sidebar.selectbox("What task would you like to do?", menu)
46
+ if choice=="--Select--":
47
+
48
+ st.write("""
49
+
50
+ 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
51
+ do anything with your lectures you like
52
+ """)
53
+
54
+ st.write("""
55
+
56
+ Never heard of NLP? No way! Natural Language Processing (NLP) is a computational technique
57
+ to process human language in all of it's complexity
58
+ """)
59
+
60
+ st.write("""
61
+
62
+ NLP is an vital discipline in Artificial Intelligence and keeps growing
63
+ """)
64
+
65
+
66
+ st.image('banner_image.png')
67
+
68
+
69
+
70
+ elif choice=="Summarizer":
71
+ st.subheader("Text Summarization")
72
+ st.write(" Enter the Text you want to summarize !")
73
+ raw_text = st.text_area("Your Text","Enter Your Text Here")
74
+ num_words = st.number_input("Enter Number of Words in Summary")
75
+
76
+ if raw_text!="" and num_words is not None:
77
+ num_words = int(num_words)
78
+ summarizer = pipeline('summarization')
79
+ summary = summarizer(raw_text, min_length=num_words,max_length=50)
80
+ s1 = json.dumps(summary[0])
81
+ d2 = json.loads(s1)
82
+ result_summary = d2['summary_text']
83
+ result_summary = '. '.join(list(map(lambda x: x.strip().capitalize(), result_summary.split('.'))))
84
+ st.write(f"Here's your Summary : {result_summary}")
85
+
86
+
87
+
88
+ elif choice=="Sentiment Analysis":
89
+ st.subheader("Sentiment Analysis")
90
+ #loading the pipeline
91
+ sentiment_analysis = pipeline("sentiment-analysis")
92
+ st.write(" Enter the Text below To find out its Sentiment !")
93
+
94
+ raw_text = st.text_area("Your Text","Enter Text Here")
95
+ if raw_text !="Enter Text Here":
96
+ result = sentiment_analysis(raw_text)[0]
97
+ sentiment = result['label']
98
+ for _ in stqdm(range(50), desc="Please wait a bit. The model is fetching the results !!"):
99
+ sleep(0.1)
100
+ if sentiment =="POSITIVE":
101
+ st.write("""# This text has a Positive Sentiment. 🤗""")
102
+ elif sentiment =="NEGATIVE":
103
+ st.write("""# This text has a Negative Sentiment. 😤""")
104
+ elif sentiment =="NEUTRAL":
105
+ st.write("""# This text seems Neutral ... 😐""")
106
+
107
+ elif choice=="Question Answering":
108
+ st.subheader("Question Answering")
109
+ st.write(" Enter the Context and ask the Question to find out the Answer !")
110
+ question_answering = pipeline("question-answering")
111
+
112
+
113
+ context = st.text_area("Context","Enter the Context Here")
114
+
115
+ #This is the text box for the question
116
+ question = st.text_area("Your Question","Enter your Question Here")
117
+
118
+ if context !="Enter Text Here" and question!="Enter your Question Here":
119
+ #we are passing question and the context
120
+ result = question_answering(question=question, context=context)
121
+ #dump the result in json and load it again
122
+ s1 = json.dumps(result)
123
+ d2 = json.loads(s1)
124
+ generated_text = d2['answer']
125
+ #joining and capalizing by dot
126
+ generated_text = '. '.join(list(map(lambda x: x.strip().capitalize(), generated_text.split('.'))))
127
+ st.write(f" Here's your Answer :\n {generated_text}")
128
+
129
+ elif choice=="Text Completion":
130
+ st.subheader("Text Completion")
131
+ st.write(" Enter the uncomplete Text to complete it automatically using AI !")
132
+ text_generation = pipeline("text-generation")
133
+ message = st.text_area("Your Text","Enter the Text to complete")
134
+
135
+
136
+ if message !="Enter the Text to complete":
137
+ generator = text_generation(message)
138
+ s1 = json.dumps(generator[0])
139
+ d2 = json.loads(s1)
140
+ generated_text = d2['generated_text']
141
+ generated_text = '. '.join(list(map(lambda x: x.strip().capitalize(), generated_text.split('.'))))
142
+ st.write(f" Here's your Generate Text :\n {generated_text}")
143
+
144
+ #main function to run
145
+ if __name__ == '__main__':
146
+ main()