Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
100 lines (86 loc) · 3.19 KB
/
Copy pathapp.py
File metadata and controls
100 lines (86 loc) · 3.19 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
from flask import Flask, request, jsonify, render_template
import joblib
import numpy as np
import tensorflow as tf
import nltk
import os
import string
# ✅ Download and setup NLTK data (use /tmp for Hugging Face compatibility)
nltk_data_path = '/tmp/nltk_data'
os.makedirs(nltk_data_path, exist_ok=True)
nltk.download('punkt', download_dir=nltk_data_path)
nltk.download('stopwords', download_dir=nltk_data_path)
nltk.download('wordnet', download_dir=nltk_data_path)
nltk.download('omw-1.4', download_dir=nltk_data_path) # For lemmatizer
nltk.download('punkt_tab', download_dir=nltk_data_path)
from nltk.data import path
path.append(nltk_data_path)
# NLTK imports
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from nltk.stem import WordNetLemmatizer
# Initialize Flask app
app = Flask(__name__)
# ✅ Load your models
tfidf_vect = joblib.load('tfidf_vectorizer.pkl')
log_reg = joblib.load('logistic_regression_model.pkl')
svm_model = joblib.load('svm_model.pkl')
mlp_model = tf.keras.models.load_model('mlp_model.h5')
mlp_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
# Preprocessing setup
stop_words = set(stopwords.words('english'))
lemmatizer = WordNetLemmatizer()
def preprocess_text(text):
text = text.lower()
text = text.translate(str.maketrans('', '', string.punctuation))
text = ''.join([char for char in text if not char.isdigit()])
text = text.strip()
text = ' '.join(text.split())
tokens = word_tokenize(text)
tokens = [word for word in tokens if word not in stop_words]
tokens = [lemmatizer.lemmatize(word) for word in tokens]
return ' '.join(tokens)
def predict_emotion(text):
processed = preprocess_text(text)
tfidf = tfidf_vect.transform([processed])
tfidf_dense = tfidf.toarray()
pred_log = log_reg.predict(tfidf)[0]
pred_svm = svm_model.predict(tfidf)[0]
pred_mlp = np.argmax(mlp_model.predict(tfidf_dense), axis=1)[0]
emotions = {
0: 'sadness', 1: 'joy', 2: 'love', 3: 'anger', 4: 'fear', 5: 'surprise'
}
return {
'Logistic Regression': emotions[pred_log],
'SVM': emotions[pred_svm],
'MLP Neural Network': emotions[pred_mlp]
}
# ✅ Web frontend route
@app.route('/', methods=['GET', 'POST'])
def home():
prediction = None
if request.method == 'POST':
text = request.form.get('text')
if text and text.strip():
try:
prediction = predict_emotion(text)
except Exception as e:
prediction = {'error': str(e)}
return render_template('index.html', prediction=prediction)
# ✅ API route
@app.route('/predict', methods=['GET', 'POST'])
def predict():
if request.method == 'GET':
text = request.args.get('text', '')
else:
data = request.get_json()
text = data.get('text', '') if data else ''
if not text.strip():
return jsonify({'error': 'No or empty text provided'}), 400
try:
predictions = predict_emotion(text)
return jsonify({'predictions': predictions})
except Exception as e:
return jsonify({'error': str(e)}), 500
if __name__ == '__main__':
app.run(host='0.0.0.0', port=7860, debug=False)