-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
56 lines (42 loc) · 1.32 KB
/
app.py
File metadata and controls
56 lines (42 loc) · 1.32 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
from flask import Flask, jsonify
from flask_cors import CORS
import joblib
from sentiment_analysis import find_sentiment
from risk_analysis import predict_risk
app = Flask(__name__)
CORS(app)
# Load model
try:
model = joblib.load('model/risk_rf_model.pkl')
print(" Model loaded")
except:
model = None
print("Failed to load model")
@app.route("/sentiment")
def sentiment_analyser(DataFrame , ticker):
return find_sentiment(DataFrame , ticker);
@app.route('/risk', methods=['POST'])
def risk_analyser():
if not model:
return jsonify({'error': 'Model not loaded'}),
data = request.get_json()
ticker = data.get('ticker')
if not ticker:
return jsonify({'error': 'Ticker not provided'}),
try:
results = predict_risk(ticker, model)
if results is None:
return jsonify({'error': f'Data fetch failed for {ticker}'}),
formatted = [{
'date': str(date),
'price': float(row['Close']),
'risk': int(row['Predicted_Risk'])
} for date, row in results.iterrows()]
return jsonify({'ticker': ticker, 'predictions': formatted}),
except Exception as e:
return jsonify({'error': str(e)}),
@app.route("/")
def hello_world():
return "<p>Hello World</p>"
if __name__ == "__main__":
app.run(debug=True)