-
Notifications
You must be signed in to change notification settings - Fork 0
/
deployment.py
41 lines (28 loc) · 1.13 KB
/
deployment.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from flask import Flask, render_template, request
import tensorflow as tf
import pickle
import numpy as np
app = Flask(__name__)
# Load the pre-trained ML model and vectorizer
vectorizer_filename = 'Helpers/vectorizer.pkl'
loaded_model = tf.keras.models.load_model('Models/model.h5')
with open(vectorizer_filename, 'rb') as f:
loaded_vectorizer = pickle.load(f)
@app.route('/', methods=['GET', 'POST'])
def index():
sentiment_result = None
if request.method == 'POST':
headline = request.form['headline']
sentiment_result = predict_sentiment(headline)
return render_template('index.html', result=sentiment_result)
def predict_sentiment(headline):
# Preprocess headline using the loaded vectorizer
X = loaded_vectorizer.transform([headline]).toarray()
# Predict sentiment using the loaded model
prediction = loaded_model.predict(X)
# Convert prediction to sentiment label
sentiment_labels = ["neutral", "positive", "negative"]
predicted_sentiment = sentiment_labels[np.argmax(prediction)]
return predicted_sentiment
if __name__ == '__main__':
app.run(debug=True)