Sentiment Analysis Flask App
This Flask web application uses a pre-trained Transformer model to perform sentiment analysis on user-provided text. The model predicts whether the sentiment of the input text is positive, negative, or neutral.
Model Architecture
The implemented model leverages a Transformer architecture for sentiment analysis. The Transformer block consists of multiple self-attention mechanisms applied iteratively (num_heads). Each attention mechanism captures different aspects of the input sequence, allowing the model to efficiently learn complex patterns. A subsequent 1D convolutional layer is employed to capture local patterns, followed by global average pooling for dimensionality reduction. Dropout is used for regularization, and the final layers include a dense layer with ReLU activation and a sigmoid activation for binary sentiment classification.
Usage
Installation: Install the required Python packages using the following command:
pip install -r requirements.txt
Run the Flask App: Execute the Flask app by running the following command: python app.py
This will start the Flask development server, and the app will be accessible at http://localhost:5000.
Enter Text: Enter your text in the input field and click the "Predict" button.
View Prediction: The app will display the predicted sentiment (Positive, Negative, or Neutral) based on the input text.
Dependencies
- Flask
- Keras
- TensorFlow
- NLTK
Model Training
Load the Dataset: Read the IMDB dataset from a CSV file and preprocess the text data.
Data Cleaning and Preprocessing: Clean and preprocess the text data by removing non-alphabetic characters, converting text to lowercase, and removing stopwords.
Data Splitting: Split the dataset into training and testing sets.
Label Encoding for Sentiment Column: Encode the categorical sentiment labels into numerical format using LabelEncoder.
Tokenization and Padding: Tokenize and pad the text sequences for input into the Transformer model.
Transformer Model Building: Define a Transformer model architecture using Keras.
Model Training: Compile and train the Transformer model on the training data.
Save the Model: Save the trained model in the HDF5 format for future use.
Evaluate the Model: Evaluate the model on the test set, compute accuracy, and generate a classification report.