-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
135 lines (116 loc) · 3.99 KB
/
Copy pathapp.py
File metadata and controls
135 lines (116 loc) · 3.99 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
from flask import Flask, render_template, request, jsonify
import joblib
import numpy as np
from datetime import datetime
import os
from database import Database
import requests
from functools import wraps
import json
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
app = Flask(__name__)
db = Database()
# Load the trained model
model_path = 'models/rainfall_model_random_forest.joblib'
if os.path.exists(model_path):
model = joblib.load(model_path)
else:
if os.environ.get('FLASK_ENV') == 'testing':
# Dummy model for testing
class DummyModel:
def predict(self, X):
return [0 for _ in range(len(X))]
def predict_proba(self, X):
return [[0.5, 0.5] for _ in range(len(X))]
model = DummyModel()
else:
raise FileNotFoundError(f"Model file not found at {model_path}")
def preprocess_input(data):
"""Preprocess input data to match model requirements"""
# Convert input data to numpy array
features = np.array([
float(data['temperature']),
float(data['humidity']),
float(data['wind_speed']),
float(data['pressure'])
]).reshape(1, -1)
return features
def get_weather_data(latitude, longitude):
"""Get current weather data from OpenWeatherMap API"""
api_key = os.getenv('OPENWEATHER_API_KEY')
if not api_key:
return None
url = f'https://api.openweathermap.org/data/2.5/weather?lat={latitude}&lon={longitude}&appid={api_key}&units=metric'
try:
response = requests.get(url)
data = response.json()
return {
'temperature': data['main']['temp'],
'humidity': data['main']['humidity'],
'wind_speed': data['wind']['speed'],
'pressure': data['main']['pressure']
}
except Exception as e:
return None
@app.route('/')
def home():
return render_template('index.html')
@app.route('/predict', methods=['POST'])
def predict():
try:
data = request.get_json()
location = data.get('location', 'Unknown')
# If location is provided, get current weather data
if location != 'Unknown':
locations = db.get_locations()
location_data = next((loc for loc in locations if loc['name'] == location), None)
if location_data:
weather_data = get_weather_data(location_data['latitude'], location_data['longitude'])
if weather_data:
data.update(weather_data)
# Preprocess the input data
features = preprocess_input(data)
# Make prediction
prediction = model.predict(features)[0]
probability = model.predict_proba(features)[0][1]
result = {
'prediction': bool(prediction),
'probability': float(probability),
'timestamp': datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
'location': location
}
# Save prediction to database
db.save_prediction({
**data,
'prediction': prediction,
'probability': probability,
'location': location
})
return jsonify(result)
except Exception as e:
return jsonify({'error': str(e)}), 400
@app.route('/history')
def get_history():
predictions = db.get_recent_predictions()
return jsonify(predictions)
@app.route('/locations')
def get_locations():
locations = db.get_locations()
return jsonify(locations)
@app.route('/add_location', methods=['POST'])
def add_location():
try:
data = request.get_json()
success = db.add_location(
data['name'],
float(data['latitude']),
float(data['longitude'])
)
return jsonify({'success': success})
except Exception as e:
return jsonify({'error': str(e)}), 400
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)