Spaces:
Build error
Build error
File size: 4,516 Bytes
fbb1cfd 2e2fcca e9aeca3 2e2fcca fbb1cfd e9aeca3 fbb1cfd e9aeca3 |
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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
import numpy as np
import spacy
import nltk
import tensorflow as tf
import streamlit as st
from streamlit_extras.add_vertical_space import add_vertical_space
from bs4 import BeautifulSoup
from gensim.models import Word2Vec
from nltk.tokenize import word_tokenize
from warnings import filterwarnings
filterwarnings('ignore')
def streamlit_config():
# page configuration
st.set_page_config(page_title='Document Classification', layout='centered')
# page header transparent color
page_background_color = """
<style>
[data-testid="stHeader"]
{
background: rgba(0,0,0,0);
}
</style>
"""
st.markdown(page_background_color, unsafe_allow_html=True)
# title and position
st.markdown(f'<h1 style="text-align: center;">Financial Document Classification</h1>',
unsafe_allow_html=True)
add_vertical_space(4)
def text_extract_from_html(html_file):
# Read the uploaded HTML file
html_content = html_file.read().decode('utf-8')
# Parse the HTML Content
soup = BeautifulSoup(html_content, 'html.parser')
# Extract the Text
text = soup.get_text()
# Split the Text and Remove Unwanted Space
result = [i.strip() for i in text.split()]
return result
def text_processing(text):
# spaCy Engine
nlp = spacy.load('en_core_web_lg')
# Process the Text with spaCy
doc = nlp(' '.join(text))
# Tokenization, Lemmatization, and Remove Stopwords, punctuation, digits
token_list = [
token.lemma_.lower().strip()
for token in doc
if token.text.lower() not in nlp.Defaults.stop_words and token.text.isalpha()
]
if len(token_list) > 0:
return ' '.join(token_list)
else:
return 'empty'
def sentence_embeddings(sentence):
# split the sentence into separate words
words = word_tokenize(sentence)
# load the trained model
model = Word2Vec.load('word2vec_model.bin')
# get the vectors of each words
vectors = [model.wv[word] for word in words if word in model.wv]
if vectors:
# return the average of vectors
return np.mean(vectors, axis=0)
else:
# we set the model parameter in training ---> vector_size = 300
return np.zeros(model.vector_size)
def prediction(input_file):
# Extract the Text from HTML Document
html_content = text_extract_from_html(input_file)
# Preprocess the Text
preprocessed_text = text_processing(html_content)
# Text Convert into Embeddings
features = sentence_embeddings(preprocessed_text)
# Reshape the features into match the expected input shape of Model
features = np.expand_dims(features, axis=0)
features = np.expand_dims(features, axis=2)
# Convert into Tensors
features_tensors = tf.convert_to_tensor(features, dtype=tf.float32)
# Load the Model and Prediction
model = tf.keras.models.load_model('model.h5', custom_objects = {'Orthogonal': tf.keras.initializers.Orthogonal})
prediction = model.predict(features_tensors)
# Find the Maximum Probability Value
target_label = np.argmax(prediction)
# Find the Target_Label Name
target = {0:'Balance Sheets', 1:'Cash Flow', 2:'Income Statement', 3:'Notes', 4:'Others'}
predicted_class = target[target_label]
# Find the Confidence
confidence = round(np.max(prediction)*100, 2)
add_vertical_space(2)
st.markdown(f'<h4 style="text-align: center; color: orange;">{confidence}% Match Found</h4>',
unsafe_allow_html=True)
add_vertical_space(1)
st.markdown(f'<h3 style="text-align: center; color: green;">Prdicted Class = {predicted_class}</h3>',
unsafe_allow_html=True)
# Streamlit Configuration Setup
streamlit_config()
# File uploader to upload the HTML file
input_file = st.file_uploader('Upload an HTML file', type='html')
if input_file is not None:
try:
# Predict the Input_HTML_File_Class
prediction(input_file)
except:
# Check 'punkt' Already Downloaded or Not
try:
nltk.data.find('tokenizers/punkt')
except LookupError:
nltk.download('punkt')
# Predict the Input_HTML_File_Class
prediction(input_file)
|