-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
111 lines (76 loc) · 4.33 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import streamlit as st
import pandas as pd
import numpy as np
from keybert import KeyBERT
from sentence_transformers import SentenceTransformer
from transformers import pipeline
from sklearn.preprocessing import LabelEncoder
import tensorflow as tf
import tensorflow_hub as hub
import tensorflow_text as text
@st.cache
def load_data():
df = pd.read_csv("augmented_datafile.csv")
lb = LabelEncoder()
lb.fit(df['Categories'])
df['Target'] = lb.transform(df['Categories'])
df['Target'] = lb.transform(df['Categories'])
return df,lb
df,lb = load_data()
@st.cache
def load_models():
model = tf.keras.models.load_model("final_model.h5",custom_objects={'KerasLayer': hub.KerasLayer})
sbert_model = SentenceTransformer('all-mpnet-base-v2')
kw_model_bert = KeyBERT(model=sbert_model)
summarizer = pipeline("summarization")
return kw_model_bert,model, summarizer
kw_model_bert,model,summarizer = load_models()
def main():
st.set_page_config(
page_title="AWDClassifier",
page_icon="🎈",
layout="wide",
initial_sidebar_state="expanded"
)
st.sidebar.markdown("# AWDClassification")
st.sidebar.text("\n\n\n")
option = 'Home Page'
option = st.sidebar.selectbox('NLP Service',('Home Page','Text Topic', 'Keywords Extraction', 'Text Summarization'))
if option == 'Home Page':
st.image("Text_Classification_image.jpg", width=700)
st.markdown("### Welcome To The Multi-class Text Classification Web Application ")
st.write("In the graph below, you can see a brief description of the data by showing the number of elements in each class.")
st.image("data_plot.png",width=800)
st.markdown("\n What type of NLP service would you like to use? you can choose one of the options in the sidebar")
elif option=='Text Topic':
st.subheader("Enter the text you'd like to analyze.")
input = st.text_area('Enter text and press Ctrl+Enter to apply') #text is stored in this variable
if (input!=""):
#st.success("#### The main topic of the inserted text is 'Artificial Intelligence'")
key_txt = kw_model_bert.extract_keywords(input, keyphrase_ngram_range=(1, 2), nr_candidates=15, top_n=10,stop_words='english')
k = str(list(dict(key_txt).keys()))
prediction = model.predict([tf.convert_to_tensor([input]) ,tf.convert_to_tensor([k]) ])
result = lb.inverse_transform(np.argmax(prediction).reshape(1)) # Get the class name
st.success("#### The main topic of the inserted text is'",result[0],"'")
satisfaction = st.slider('Tell us how satisfied you are with the result?', 0, 100, 25)
st.write("\n If you are not satisfied with the result, please help our model to better distinguish the context by filling in the box below with the correct result.")
st.text_input(label="Please insert the correct result")
st.button("Submit")
else :
st.error("Please enter your Text")
elif option == 'Keywords Extraction':
st.subheader("Enter the text you want to analyze and get the most relevant keywords with their score compared to 1.")
input2 = st.text_area('Enter text') #text is stored in this variable
key = kw_model_bert.extract_keywords(input2, keyphrase_ngram_range=(1, 2), nr_candidates=15, top_n=10,stop_words='english')
keys = list(dict(key).keys())
st.sucess(str(keys))
else:
st.subheader("Enter the text you'd like to analyze.")
input3 = st.text_area('Enter text') #text is stored in this variable
st.subheader("The Summary")
if(input3 !=""):
result = summarizer(input3, min_length=20, max_length=70, do_sample=False)
summary = result[0]["summary_text"]
st.sucess(summary)
#st.success("Animal law is a combination of statutory and case law in which the nature legal social or biological of nonhuman animals is an important factor . Animal law permeates and affects most traditional areas of the law including tort contract criminal and constitutional law . Growing number of state and local bar associations now have animal law committees .")
main()