|
|
import streamlit as st |
|
|
import pandas as pd |
|
|
import re |
|
|
import tensorflow as tf |
|
|
from nltk.corpus import stopwords |
|
|
from nltk.tokenize import word_tokenize |
|
|
from tensorflow.keras.preprocessing.text import Tokenizer |
|
|
from tensorflow.keras.preprocessing.sequence import pad_sequences |
|
|
from tensorflow.keras.models import load_model |
|
|
import nltk |
|
|
|
|
|
|
|
|
nltk_data_dir = "/tmp/nltk_data" |
|
|
nltk.data.path.append(nltk_data_dir) |
|
|
|
|
|
|
|
|
nltk.download('stopwords', download_dir=nltk_data_dir) |
|
|
nltk.download('punkt', download_dir=nltk_data_dir) |
|
|
|
|
|
|
|
|
model = load_model('src/model.keras') |
|
|
|
|
|
|
|
|
stpwds_id = list(set(stopwords.words('english'))) |
|
|
|
|
|
|
|
|
def text_preprocessing(text): |
|
|
|
|
|
text = text.lower() |
|
|
|
|
|
|
|
|
text = re.sub("@[A-Za-z0-9_]+", " ", text) |
|
|
|
|
|
|
|
|
text = re.sub("#[A-Za-z0-9_]+", " ", text) |
|
|
|
|
|
|
|
|
text = re.sub(r"\\n", " ",text) |
|
|
|
|
|
|
|
|
text = text.strip() |
|
|
|
|
|
|
|
|
text = re.sub(r"http\S+", " ", text) |
|
|
text = re.sub(r"www.\S+", " ", text) |
|
|
|
|
|
|
|
|
text = re.sub("[^A-Za-z\s']", " ", text) |
|
|
|
|
|
|
|
|
tokens = word_tokenize(text) |
|
|
|
|
|
|
|
|
tokens = [word for word in tokens if word not in stpwds_id] |
|
|
|
|
|
|
|
|
text = ' '.join(tokens) |
|
|
|
|
|
return text |
|
|
|
|
|
|
|
|
st.title('Sentiment Analysis App') |
|
|
|
|
|
|
|
|
user_input = st.text_area("Enter the text for sentiment analysis:") |
|
|
|
|
|
if st.button('Analyze'): |
|
|
if user_input: |
|
|
|
|
|
processed_text = text_preprocessing(user_input) |
|
|
prediction = model.predict([[processed_text]]) |
|
|
sentiment = "Positive" if prediction[0] > 0.5 else "Negative" |
|
|
|
|
|
|
|
|
st.write(f"Sentiment: {sentiment}") |
|
|
else: |
|
|
st.write("Please enter some text.") |
|
|
|
|
|
|