Skip to content

Commit 81a5fc1

Browse files
committed
Initial commit
0 parents  commit 81a5fc1

File tree

10 files changed

+659
-0
lines changed

10 files changed

+659
-0
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

Procfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
web: gunicorn app:app

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Sentiment Analysis
2+
A simple sentiment analysis application made using following packages:
3+
- Tensorflow Keras
4+
- Natural Language Toolkit (NLTK)
5+
- TfidfVectorizer
6+
7+
8+
The model was trained using tweets from [Sentiment140 dataset with 1.6 million tweets](http://www.kaggle.com/kazanova/sentiment140).
9+
10+
The app has has been deployed using Flask and Heroku. The app can be found [here](http://sentiment-analysis-mp.herokuapp.com/).

app.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import numpy as np
2+
from flask import Flask, request, jsonify, render_template
3+
import pickle
4+
import os
5+
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
6+
import tensorflow as tf
7+
import re
8+
from nltk.corpus import stopwords
9+
from nltk.stem import SnowballStemmer
10+
11+
app = Flask(__name__)
12+
vect = pickle.load(open("vectorizer.pickle",'rb'))
13+
model = tf.keras.models.load_model('model.h5')
14+
15+
def preprocess(text, stem=False):
16+
text = re.sub("@\S+|https?:\S+|http?:\S|[^A-Za-z0-9]+", ' ', str(text).lower()).strip()
17+
stop_words = stopwords.words('english')
18+
stemmer = SnowballStemmer('english')
19+
tokens = []
20+
for token in text.split():
21+
if token not in stop_words:
22+
if stem:
23+
tokens.append(stemmer.stem(token))
24+
else:
25+
tokens.append(token)
26+
return " ".join(tokens)
27+
28+
29+
@app.route('/')
30+
def home():
31+
return render_template('index.html')
32+
33+
@app.route('/predict',methods=['POST'])
34+
def predict():
35+
inp = [x for x in request.form.values()]
36+
inp = preprocess(inp)
37+
inp = vect.transform([inp]).toarray().reshape(1,1,2500)
38+
output = model.predict(inp)[0]
39+
40+
return render_template('index.html', prediction_text='{}'.format(output[0]))
41+
42+
if __name__ == "__main__":
43+
app.run(debug=True)

model.h5

18.9 MB
Binary file not shown.

nltk.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
stopwords

requirements.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Flask==1.1.2
2+
numpy==1.18.5
3+
nltk==3.5
4+
tensorflow==2.3.1
5+
scikit-learn==0.23.2
6+
gunicorn==20.0.4

0 commit comments

Comments
 (0)