N2 / app.py
cuteasduck's picture
Yessss
613fbdc verified
Raw
History Blame Contribute Delete
3.51 kB
import numpy as np
import streamlit as st
import nltk
from nltk.tokenize import word_tokenize
# Load your image files
demo_image_path = "demo.jpeg"
gif_image_path = "1.gif"
# Display demo image
st.image(demo_image_path, use_column_width=True)
# Load your neural network weights and biases
wei_prev = [6.53372226, 1.45266998, -1.31130261, -3.16864773, 0.39603089]
wei_cur = [1.74665619, -0.74696832, 2.84321548, -1.33783743]
wei_fb = [1.28461033]
wei_bias = [0.41829136]
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def sigmoid_derivative(x):
return x * (1 - x)
def onehot_pos_cur(num):
if num == 1:
return np.array([0, 0, 0, 1])
if num == 2:
return np.array([0, 0, 1, 0])
if num == 3:
return np.array([0, 1, 0,iadsl 0])
if num == 4:
return np.array([1, 0, 0, 0])
def onehot_pos_prev(num):
if num == 1:
return np.array([0, 0, 0, 0, 1])
if num == 2:
return np.array([0, 0, 0, 1, 0])
if num == 3:
return np.array([0, 0, 1, 0, 0])
if num == 4:
return np.array([0, 1, 0, 0, 0])
st.title("🌲 Recurrent Perceptron for Noun Chunk Identification 🌲")
# Using Markdown for the input text to include an emoji
user_input = st.text_input("Enter a POS tagged input", "")
tokens = word_tokenize(user_input)
# Perform part-of-speech tagging
tagged_words = nltk.pos_tag(tokens)
# Define the tags of interest
tags_of_interest = ['NN', 'DT', 'JJ', 'NNS']
# Initialize a list to store filtered words
filtered_words = []
# Filter tagged words based on tags of interest
for word, tag in tagged_words:
if tag=='NN' or tag=='NNS' or tag=='NNP' or tag=='NNPS':
filtered_words.append(1)
elif tag=='DT' or tag=='PDT' or tag=='POS':
filtered_words.append(2)
elif tag=='JJ' or tag=='JJR' or tag=='JJS':
filtered_words.append(3)
else:
filtered_words.append(4)
user_input = filtered_words
# Display the "Classify" button with larger size
classify_button = st.button(" Classify ", key="classify_button", help="Click to classify")
# Adjusting the size of the classify button using CSS
st.markdown(
"""
<style>
.stButton>button {
width: 200px !important;
height: 50px !important;
font-size: 18px !important;
}
</style>
""",
unsafe_allow_html=True
)
output = []
if classify_button:
user_input = np.array(list(user_input), dtype=int)
for i in range(len(user_input)):
if i == 0:
x_prev = np.array([1, 0, 0, 0, 0]) # Initial previous POS tag (V)
y_prev = 0 # Initial previous output
else:
x_prev = onehot_pos_prev(x_prev) # Convert previous POS tag to one-hot vector
x_cur_int = user_input[i] # Current POS tag index
x_cur = onehot_pos_cur(x_cur_int) # Convert current POS tag to one-hot vector
# Forward pass through the network using sigmoid activation function
y_cur = sigmoid((np.dot(wei_fb, y_prev) + np.dot(wei_prev, x_prev) + np.dot(wei_cur, x_cur) - wei_bias).item())
# Predict the label based on the output of the network
if y_cur > 0.5:
output.append(1)
else:
output.append(0)
x_prev = x_cur_int
y_prev = y_cur
st.write(output)
# Display 1.gif image
st.image(gif_image_path, use_column_width=True)
# Add the message below the Classify button
st.markdown("• **Made by 4 IIT-Bombay students.**")
st.markdown("• **Hosted by ❤️**")