-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
136 lines (108 loc) · 4.94 KB
/
app.py
File metadata and controls
136 lines (108 loc) · 4.94 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
from flask import Flask, request, jsonify, send_from_directory
from flask_cors import CORS
import pickle
import re
import os
import pandas as pd
app = Flask(__name__, static_folder='static')
CORS(app)
port = int(os.environ.get("PORT", 10000))
# ── Load model ──────────────────────────────────────────────────────────────
MODEL_PATH = os.path.join(os.path.dirname(__file__), 'sentiment_model.pkl')
model_bundle = None
def load_model():
global model_bundle
if os.path.exists(MODEL_PATH):
with open(MODEL_PATH, 'rb') as f:
model_bundle = pickle.load(f)
print("✅ Model loaded successfully.")
else:
print("⚠️ sentiment_model.pkl not found. Place it next to app.py.")
load_model()
# ── Text cleaning (mirrors your notebook) ───────────────────────────────────
def clean_text(text):
if isinstance(text, str):
text = text.lower()
text = re.sub(r'[^a-zA-Z\s]', '', text)
text = re.sub(r'\s+', ' ', text).strip()
else:
text = ''
return text
# ── Routes ───────────────────────────────────────────────────────────────────
@app.route('/')
def index():
return send_from_directory('static', 'index.html')
@app.route('/api/health')
def health():
return jsonify({'status': 'ok', 'model_loaded': model_bundle is not None})
@app.route('/api/predict', methods=['POST'])
def predict():
if model_bundle is None:
return jsonify({'error': 'Model not loaded. Place sentiment_model.pkl next to app.py and restart.'}), 503
data = request.get_json()
text = data.get('text', '').strip()
if not text:
return jsonify({'error': 'No text provided'}), 400
cleaned = clean_text(text)
vec = model_bundle['vectorizer'].transform([cleaned])
raw = model_bundle['model'].predict(vec)[0]
score = max(1, min(5, round(raw)))
confidence = round(min(100, max(0, (1 - abs(raw - score)) * 100)), 1)
labels = {1: 'Very Negative', 2: 'Negative', 3: 'Neutral', 4: 'Positive', 5: 'Very Positive'}
colors = {1: '#ef4444', 2: '#f97316', 3: '#eab308', 4: '#22c55e', 5: '#10b981'}
return jsonify({
'score': score,
'raw': round(float(raw), 3),
'label': labels[score],
'color': colors[score],
'confidence': confidence
})
@app.route('/api/analyze-csv', methods=['POST'])
def analyze_csv():
if model_bundle is None:
return jsonify({'error': 'Model not loaded.'}), 503
if 'file' not in request.files:
return jsonify({'error': 'No file uploaded'}), 400
file = request.files['file']
if not file.filename.endswith('.csv'):
return jsonify({'error': 'Only CSV files are supported'}), 400
try:
df = pd.read_csv(file, encoding='latin1')
except Exception as e:
return jsonify({'error': f'Could not read CSV: {str(e)}'}), 400
# Use known columns if present, otherwise fall back to ALL text columns
known_cols = ['ProductName', 'Review', 'Summary']
text_cols = [c for c in known_cols if c in df.columns]
if not text_cols:
text_cols = [c for c in df.columns if pd.api.types.is_string_dtype(df[c])]
if not text_cols:
return jsonify({'error': 'No text columns found in CSV.'}), 400
for col in text_cols:
df[col] = df[col].apply(clean_text)
df['combined_text'] = df[text_cols].fillna('').apply(lambda row: ' '.join(row.values), axis=1)
X = model_bundle['vectorizer'].transform(df['combined_text'])
preds = model_bundle['model'].predict(X)
df['Predicted_Rate'] = [max(1, min(5, round(p))) for p in preds]
counts = df['Predicted_Rate'].value_counts().sort_index()
total = len(df)
distribution = {}
for star in range(1, 6):
c = int(counts.get(star, 0))
distribution[str(star)] = {'count': c, 'pct': round(c / total * 100, 2) if total else 0}
good = int(df[df['Predicted_Rate'].isin([4, 5])].shape[0])
neutral = int(df[df['Predicted_Rate'] == 3].shape[0])
bad = int(df[df['Predicted_Rate'].isin([1, 2])].shape[0])
preview = df[['combined_text', 'Predicted_Rate']].head(10).to_dict(orient='records')
return jsonify({
'total': total,
'distribution': distribution,
'summary': {
'good': {'count': good, 'pct': round(good / total * 100, 2) if total else 0},
'neutral': {'count': neutral, 'pct': round(neutral / total * 100, 2) if total else 0},
'bad': {'count': bad, 'pct': round(bad / total * 100, 2) if total else 0},
},
'preview': preview
})
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.run(debug=False, host='0.0.0.0', port=port)