-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
335 lines (260 loc) · 10.2 KB
/
Copy pathapp.py
File metadata and controls
335 lines (260 loc) · 10.2 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
"""
FinPridict Flask Backend API
Serves AI-powered stock predictions and market data
"""
from flask import Flask, request, jsonify
from flask_cors import CORS
from datetime import datetime, timedelta
import os
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Set TensorFlow environment variable to prevent reloader issues
os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'
# Import models and utilities
from models import get_predictions, train_model
from utils import fetch_stock_data, calculate_indicators, format_prediction_response
app = Flask(__name__)
CORS(app) # Enable CORS for frontend requests
# Configuration
app.config['JSON_SORT_KEYS'] = False
FLASK_ENV = os.getenv('FLASK_ENV', 'development')
DEBUG = FLASK_ENV == 'development'
print("=" * 60)
print("FinPridict Backend Server Starting...")
print("=" * 60)
print(f"Environment: {FLASK_ENV}")
print(f"Debug Mode: {DEBUG}")
# ============================================================================
# HEALTH CHECK ENDPOINT
# ============================================================================
@app.route('/health', methods=['GET'])
def health_check():
"""Health check endpoint"""
return jsonify({
'status': 'active',
'timestamp': datetime.now().isoformat(),
'service': 'FinPridict API'
}), 200
# ============================================================================
# PREDICTION ENDPOINTS
# ============================================================================
@app.route('/api/predictions', methods=['POST'])
def get_prediction():
"""
Get AI predictions for a stock using LSTM + sentiment analysis
Request body:
{
"symbol": "AAPL",
"market": "us", # 'us', 'indian', 'crypto'
"period": "7d" # '1d', '7d', '30d', '90d'
}
Response:
{
"symbol": "AAPL",
"currentPrice": 175.23,
"predictedPrice": 185.67,
"confidence": 78,
"trend": "bullish",
"accuracy": 82,
"factors": [...],
"sentiment_score": 0.15,
"lstm_prediction": 182.45,
"sentiment_adjustment": 0.0075,
"timeframe": "7 days"
}
"""
try:
data = request.get_json()
# Validate input
if not data:
return jsonify({'error': 'No data provided'}), 400
symbol = data.get('symbol', '').upper()
market = data.get('market', 'us').lower()
period = data.get('period', '7d')
if not symbol:
return jsonify({'error': 'Symbol is required'}), 400
if market not in ['us', 'indian', 'crypto']:
return jsonify({'error': 'Invalid market. Use: us, indian, crypto'}), 400
# Get prediction from ML model
prediction = get_predictions(symbol, market, period)
if not prediction:
return jsonify({'error': f'Could not generate prediction for {symbol}. Please check the symbol and try again.'}), 400
return jsonify(prediction), 200
except Exception as e:
print(f"Error in get_prediction: {str(e)}")
return jsonify({'error': str(e)}), 500
@app.route('/api/search/<symbol>', methods=['GET'])
def search_symbol(symbol):
"""
Search and validate any stock symbol
Query params:
- market: 'us', 'indian', 'crypto' (optional, will try all if not specified)
Returns symbol information if found
"""
try:
symbol = symbol.upper()
market = request.args.get('market', '').lower()
markets_to_try = [market] if market in ['us', 'indian', 'crypto'] else ['us', 'indian', 'crypto']
for m in markets_to_try:
try:
# Try to fetch data for this symbol in this market
stock_data = fetch_stock_data(symbol, days=30)
if stock_data:
# Get company name
from models import get_company_name, get_sector_from_symbol
name = get_company_name(symbol, m)
sector = get_sector_from_symbol(symbol, m)
return jsonify({
'symbol': symbol,
'name': name,
'market': m,
'sector': sector,
'currentPrice': stock_data['currentPrice'],
'currency': stock_data.get('currency', 'USD'),
'found': True,
'timestamp': datetime.now().isoformat()
}), 200
except Exception as e:
print(f"Error searching {symbol} in {m}: {str(e)}")
continue
return jsonify({
'symbol': symbol,
'found': False,
'error': 'Symbol not found in any market',
'timestamp': datetime.now().isoformat()
}), 404
except Exception as e:
print(f"Error in search_symbol: {str(e)}")
return jsonify({'error': str(e)}), 500
@app.route('/api/predictions/<market>', methods=['GET'])
def get_market_predictions(market):
"""
Get all predictions for a specific market
Query params:
- period: '1d', '7d', '30d', '90d' (default: '7d')
Returns list of predictions for all stocks in that market
"""
try:
if market not in ['us', 'indian', 'crypto']:
return jsonify({'error': 'Invalid market'}), 400
period = request.args.get('period', '7d')
# Define stocks for each market
market_stocks = {
'us': ['AAPL', 'MSFT', 'GOOGL', 'AMZN', 'TSLA'],
'indian': ['RELIANCE.NS', 'TCS.NS', 'HDFCBANK.NS', 'INFY.NS', 'ITC.NS'],
'crypto': ['BTC-USD', 'ETH-USD', 'BNB-USD', 'SOL-USD', 'ADA-USD']
}
stocks = market_stocks.get(market, [])
predictions = []
for symbol in stocks:
try:
pred = get_predictions(symbol, market, period)
if pred:
predictions.append(pred)
except Exception as e:
print(f"Error getting prediction for {symbol}: {str(e)}")
continue
return jsonify({
'market': market,
'count': len(predictions),
'predictions': predictions,
'timestamp': datetime.now().isoformat()
}), 200
except Exception as e:
print(f"Error in get_market_predictions: {str(e)}")
return jsonify({'error': str(e)}), 500
# ============================================================================
# STOCK DATA ENDPOINTS
# ============================================================================
@app.route('/api/stock-data/<symbol>', methods=['GET'])
def get_stock_data(symbol):
"""
Get current stock data and technical indicators
Query params:
- days: number of historical days (default: 30)
"""
try:
symbol = symbol.upper()
days = request.args.get('days', 30, type=int)
# Fetch stock data
stock_data = fetch_stock_data(symbol, days)
if not stock_data:
return jsonify({'error': f'Could not fetch data for {symbol}'}), 400
return jsonify(stock_data), 200
except Exception as e:
print(f"Error in get_stock_data: {str(e)}")
return jsonify({'error': str(e)}), 500
@app.route('/api/technical-indicators/<symbol>', methods=['GET'])
def get_technical_indicators(symbol):
"""
Get technical indicators for a stock (RSI, MACD, Bollinger Bands, etc.)
Query params:
- period: '1d', '1w', '1m', '3m', '1y' (default: '1m')
"""
try:
symbol = symbol.upper()
period = request.args.get('period', '1m')
indicators = calculate_indicators(symbol, period)
if not indicators:
return jsonify({'error': f'Could not calculate indicators for {symbol}'}), 400
return jsonify(indicators), 200
except Exception as e:
print(f"Error in get_technical_indicators: {str(e)}")
return jsonify({'error': str(e)}), 500
# ============================================================================
# MODEL MANAGEMENT ENDPOINTS
# ============================================================================
@app.route('/api/models/train', methods=['POST'])
def train_models():
"""
Trigger model retraining (admin endpoint)
Request body:
{
"market": "us", # optional
"force": true # force retrain
}
"""
try:
data = request.get_json() or {}
market = data.get('market', None)
force = data.get('force', False)
result = train_model(market, force)
return jsonify({
'status': 'training_initiated',
'market': market,
'message': result,
'timestamp': datetime.now().isoformat()
}), 202
except Exception as e:
print(f"Error in train_models: {str(e)}")
return jsonify({'error': str(e)}), 500
# ============================================================================
# ERROR HANDLERS
# ============================================================================
@app.errorhandler(404)
def not_found(error):
return jsonify({'error': 'Endpoint not found'}), 404
@app.errorhandler(500)
def server_error(error):
return jsonify({'error': 'Internal server error'}), 500
# ============================================================================
# MAIN
# ============================================================================
if __name__ == '__main__':
print("\nStarting FinPridict Flask Server...")
print("API Documentation: http://localhost:5000")
print("Health Check: http://localhost:5000/health")
print("\nAvailable Endpoints:")
print(" POST /api/predictions - Get single stock prediction")
print(" GET /api/predictions/<market> - Get all predictions for market")
print(" GET /api/stock-data/<symbol> - Get stock data")
print(" GET /api/technical-indicators/<symbol> - Get indicators")
print(" POST /api/models/train - Retrain models")
print("=" * 60)
app.run(
host='0.0.0.0',
port=5000,
debug=DEBUG,
use_reloader=False # Disable reloader to prevent TensorFlow file change restarts
)