-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
128 lines (101 loc) · 3.43 KB
/
Copy pathapp.py
File metadata and controls
128 lines (101 loc) · 3.43 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
#!/usr/bin/env python3
"""
Flask web application for Movie Review Sentiment Analysis.
"""
import sys
from pathlib import Path
# Add src to path
sys.path.append(str(Path(__file__).parent / "src"))
from flask import Flask, render_template, request, jsonify
import torch
from config.settings import Config
from models.pytorch_trainer import PyTorchTrainer
import logging
# Setup logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
app = Flask(__name__)
# Global variables for model
config = None
pytorch_trainer = None
model_loaded = False
def load_model():
"""Load the trained PyTorch model."""
global config, pytorch_trainer, model_loaded
try:
# Load configuration
config = Config()
# Initialize trainer
pytorch_trainer = PyTorchTrainer(config)
# Load the trained LSTM model
model_path = "models/pytorch_lstm_model.pth"
pytorch_trainer.load_model(model_path, "lstm")
# Load tokenizer
from models.pytorch_models import ModelFactory
model_config = pytorch_trainer._get_model_config("lstm")
pytorch_trainer.tokenizer = ModelFactory.get_tokenizer("lstm", model_config)
model_loaded = True
logger.info("Model loaded successfully!")
except Exception as e:
logger.error(f"Failed to load model: {str(e)}")
model_loaded = False
@app.route('/')
def index():
"""Main page."""
return render_template('index.html', model_loaded=model_loaded)
@app.route('/predict', methods=['POST'])
def predict():
"""Predict sentiment for given text."""
if not model_loaded:
return jsonify({
'error': 'Model not loaded. Please train a model first.',
'success': False
})
try:
# Get text from request
data = request.get_json()
text = data.get('text', '').strip()
if not text:
return jsonify({
'error': 'Please provide some text to analyze.',
'success': False
})
# Make prediction
predictions, probabilities = pytorch_trainer.predict([text])
# Get prediction result
prediction = predictions[0]
prob = probabilities[0]
# Convert to human-readable format
sentiment = "Positive" if prediction == 1 else "Negative"
confidence = float(max(prob)) * 100
# Get detailed probabilities (convert to Python floats)
negative_prob = float(prob[0]) * 100
positive_prob = float(prob[1]) * 100
return jsonify({
'success': True,
'text': text,
'sentiment': sentiment,
'confidence': round(confidence, 2),
'probabilities': {
'negative': round(negative_prob, 2),
'positive': round(positive_prob, 2)
}
})
except Exception as e:
logger.error(f"Prediction error: {str(e)}")
return jsonify({
'error': f'Prediction failed: {str(e)}',
'success': False
})
@app.route('/health')
def health():
"""Health check endpoint."""
return jsonify({
'status': 'healthy',
'model_loaded': model_loaded
})
if __name__ == '__main__':
# Load model on startup
load_model()
# Run the app
app.run(debug=True, host='0.0.0.0', port=5005)