paragon-analytics commited on
Commit
64c5f29
·
1 Parent(s): d8620b4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +129 -0
app.py ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Import packages:
2
+
3
+ import numpy as np
4
+ import matplotlib.pyplot as plt
5
+ import re
6
+ # tensorflow imports:
7
+ import tensorflow as tf
8
+ from tensorflow import keras
9
+ from tensorflow.keras import losses
10
+ from tensorflow.keras import layers
11
+ from tensorflow.keras.layers.experimental import preprocessing
12
+ from tensorflow.keras.optimizers import RMSprop
13
+ # # keras imports:
14
+ from keras.models import Model
15
+ from keras.layers import LSTM, Activation, Dense, Dropout, Input, Embedding, RepeatVector, TimeDistributed
16
+ from keras.preprocessing.text import Tokenizer
17
+ from keras_preprocessing import sequence
18
+ from tensorflow.keras.utils import to_categorical
19
+ from keras.callbacks import EarlyStopping
20
+ from keras.models import Sequential
21
+ from keras import layers
22
+ from keras.backend import clear_session
23
+ import pickle
24
+ import gradio as gr
25
+ import yake
26
+ import spacy
27
+ from spacy import displacy
28
+ import streamlit as st
29
+ import spacy_streamlit
30
+ nlp = spacy.load('en_core_web_sm')
31
+
32
+ kw_extractor = yake.KeywordExtractor()
33
+ custom_kw_extractor = yake.KeywordExtractor(lan="en", n=2, dedupLim=0.2, top=10, features=None)
34
+
35
+ max_words = 2000
36
+ max_len = 111
37
+
38
+ # load the model from disk
39
+ filename = 'resil_lstm_model.sav'
40
+ lmodel = pickle.load(open(filename, 'rb'))
41
+
42
+ # load the model from disk
43
+ filename = 'tokenizer.pickle'
44
+ tok = pickle.load(open(filename, 'rb'))
45
+
46
+ def process_final_text(text):
47
+ X_test = str(text).lower()
48
+ l = []
49
+ l.append(X_test)
50
+ test_sequences = tok.texts_to_sequences(l)
51
+ test_sequences_matrix = sequence.pad_sequences(test_sequences,maxlen=max_len)
52
+ lstm_prob = lmodel.predict(test_sequences_matrix.tolist()).flatten()
53
+ lstm_pred = np.where(lstm_prob>=0.5,1,0)
54
+
55
+ # Get Keywords:
56
+ keywords = custom_kw_extractor.extract_keywords(X_test)
57
+ letter = []
58
+ score = []
59
+ for i in keywords:
60
+ if i[1]>0.4:
61
+ a = "+++"
62
+ elif (i[1]<=0.4) and (i[1]>0.1):
63
+ a = "++"
64
+ elif (i[1]<=0.1) and (i[1]>0.01):
65
+ a = "+"
66
+ else:
67
+ a = "NA"
68
+
69
+ letter.append(i[0])
70
+ score.append(a)
71
+
72
+ keywords = [(letter[i], score[i]) for i in range(0, len(letter))]
73
+
74
+ # Get NER:
75
+ # NER:
76
+ doc = nlp(text)
77
+ sp_html = displacy.render(doc, style="ent", page=True, jupyter=False)
78
+ NER = (
79
+ ""
80
+ + sp_html
81
+ + ""
82
+ )
83
+ return {"Resilience": float(lstm_prob[0]), "Non-Resilience": 1-float(lstm_prob[0])},keywords,NER
84
+
85
+ def main(prob1):
86
+ text = str(prob1)
87
+ obj = process_final_text(text)
88
+ return obj[0],obj[1],obj[2]
89
+
90
+ title = "Welcome to **ResText** 🪐"
91
+ description1 = """
92
+ Just add your text and hit Create & Analyze ✨
93
+ """
94
+
95
+ description2 = """
96
+ Although encouraged, you don't have to fill all the boxes. Just try the ones that matter to you. After getting your first score, modify your answers and hit Create & Analyze again 🤞
97
+ """
98
+
99
+ with gr.Blocks(title=title) as demo:
100
+ gr.Markdown(f"## {title}")
101
+ gr.Markdown("""![marketing](file/marketing.jpg)""")
102
+ gr.Markdown(description1)
103
+ gr.Markdown("""---""")
104
+ gr.Markdown(description2)
105
+ gr.Markdown("""---""")
106
+ prob1 = gr.Textbox(label="Enter Your Text Here:",lines=2, placeholder="Type it here ...")
107
+ submit_btn = gr.Button("Create & Analyze")
108
+ #text = gr.Textbox(label="Text:",lines=2, placeholder="Please enter text here ...")
109
+ #submit_btn2 = gr.Button("Analyze")
110
+
111
+ with gr.Column(visible=True) as output_col:
112
+ label = gr.Label(label = "Predicted Label")
113
+ impplot = gr.HighlightedText(label="Important Words", combine_adjacent=False).style(
114
+ color_map={"+++": "royalblue","++": "cornflowerblue",
115
+ "+": "lightsteelblue", "NA":"white"})
116
+ NER = gr.HTML(label = 'NER:')
117
+
118
+ submit_btn.click(
119
+ main,
120
+ [prob1],
121
+ [label,impplot,NER], api_name="ResTalk"
122
+ )
123
+
124
+
125
+
126
+ gr.Markdown("### Click on any of the examples below to see how it works:")
127
+ gr.Examples([["It is difficult to write persuasive product descriptions."], ["Talking to your friends about their problems with drugs and alcohol might not be easy."], ["When experiencing depression, I couldn't get out of bed or focus.", ["Up to 6 million homeless animals enter shelters nationwide every year."]], [prob1], [label,impplot,NER], main, cache_examples=True)
128
+
129
+ demo.launch()