Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Main #4

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 59 additions & 4 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,64 @@
import pickle
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import sequence

from flask import Flask, render_template, request
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer

app = Flask(__name__)

model = None
tokenizer = None
loaded = False # Flag to ensure loading happens only once

def load_keras_model():
global model
try:
model = load_model('models/uci_sentimentanalysis.h5')
print("Model loaded successfully.")
except Exception as e:
print(f"Error loading model: {e}")

def load_tokenizer():
global tokenizer
try:
with open('models/tokenizer.pickle', 'rb') as handle:
tokenizer = pickle.load(handle)
print("Tokenizer loaded successfully.")
except Exception as e:
print(f"Error loading tokenizer: {e}")

# Befor_first_request was not working with the keras version i have isntalled
@app.before_request
def before_request():
global loaded
if not loaded:
load_keras_model()
load_tokenizer()
loaded = True # Set the flag to True to prevent reloading

def sentiment_analysis(input_text):
if tokenizer is None or model is None:
return "Model or tokenizer not loaded."

user_sequences = tokenizer.texts_to_sequences([input_text])
user_sequences_matrix = sequence.pad_sequences(user_sequences, maxlen=1225)
prediction = model.predict(user_sequences_matrix)

return round(float(prediction[0][0]), 2)

@app.route("/", methods=["GET", "POST"])
def index():
# TODO: Write the code that calls the sentiment analysis functions here.
# hint: use request.method == "POST"
return render_template('form.html')
sentiment = {}
if request.method == "POST":
text = request.form.get("user_text") # Get user input
if text:
analyzer = SentimentIntensityAnalyzer()
sentiment = analyzer.polarity_scores(text) # VADER analysis
sentiment["custom model positive"] = sentiment_analysis(text) # Custom model analysis

return render_template('form.html', sentiment=sentiment)

if __name__ == "__main__":
app.run()
app.run(debug=True)

Binary file added models/tokenizer.pickle
Binary file not shown.
Binary file added models/uci_sentimentanalysis.h5
Binary file not shown.
Loading