Ahtisham1583's picture
Update app.py
b4e81b1 verified
import streamlit as st
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.text import Tokenizer # Assuming you used Tokenizer during training
# Load the pre-trained sentiment analysis model
model = load_model("sentiment_analysis_model.h5")
# Define a tokenizer (replace with your actual tokenization logic from training)
tokenizer = Tokenizer(num_words=5000) # Adjust vocabulary size based on your training
def preprocess_text(text):
# Preprocess text based on your training process (e.g., tokenization)
tokens = tokenizer.texts_to_sequences([text])
padded_sequence = pad_sequences(tokens, maxlen=200) # Adjust max length based on your training
return padded_sequence
def classify_sentiment(text):
"""
Function to preprocess text, make predictions using the loaded model,
and return the predicted sentiment.
"""
preprocessed_text = preprocess_text(text)
prediction = model.predict(preprocessed_text)
sentiment_mapping = {0: "Negative", 1: "Neutral", 2: "Positive"}
sentiment = sentiment_mapping[prediction.argmax()]
return sentiment
def main():
"""
Streamlit app for sentiment analysis
"""
st.title("Sentiment Analysis App")
text_input = st.text_input("Enter text to analyze:")
if text_input:
sentiment = classify_sentiment(text_input)
st.write(f"Predicted Sentiment: {sentiment}")
if __name__ == "__main__":
main()