ShreehariS754 commited on
Commit
365881c
·
verified ·
1 Parent(s): d46c664

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -35
app.py CHANGED
@@ -3,42 +3,49 @@ import numpy as np
3
  import pickle
4
  from tensorflow.keras.models import load_model
5
 
6
- # Load the saved model and scaler
7
- model = load_model('Best_GRU_solar_insolation_model.keras')
8
- with open('scaler.pkl', 'rb') as f:
9
- scaler = pickle.load(f)
10
 
11
- # Preprocessing function for Gradio input (scaling + sin/cos transformations)
12
- def preprocess_input(year, month, day, hour):
13
- # Sin/Cos transformations
14
- day_sin = np.sin(2 * np.pi * day / 31)
15
- day_cos = np.cos(2 * np.pi * day / 31)
16
- month_sin = np.sin(2 * np.pi * month / 12)
17
- month_cos = np.cos(2 * np.pi * month / 12)
18
- hour_sin = np.sin(2 * np.pi * hour / 24)
19
- hour_cos = np.cos(2 * np.pi * hour / 24)
20
-
21
- # Combine the features
22
- input_data = np.array([year, day_sin, day_cos, month_sin, month_cos, hour_sin, hour_cos]).reshape(1, -1)
23
-
24
- # Scale the input using the saved scaler
25
- input_scaled = scaler.transform(input_data)
26
-
27
- # Reshape the scaled input for the model
28
- return np.expand_dims(input_scaled, axis=-1)
29
 
30
- # Prediction function for Gradio
31
- def predict_solar_insolation(year, month, day, hour):
32
- input_data = preprocess_input(year, month, day, hour)
33
- prediction = model.predict(input_data)
34
- return round(float(prediction[0][0]), 3) if float(prediction[0][0])>10 else 0
 
 
 
35
 
36
- # Gradio client
37
- app = gr.Interface(fn=predict_solar_insolation,
38
- inputs=[gr.Number(label="Year"),
39
- gr.Number(label="Month"),
40
- gr.Number(label="Day"),
41
- gr.Number(label="Hour")],
42
- outputs="number")
 
 
 
43
 
44
- app.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  import pickle
4
  from tensorflow.keras.models import load_model
5
 
6
+ model=load_model('Best_GRU_solar_insolation_model.keras')
7
+ with open('scaler.pkl','rb') as f:
8
+ scaler=pickle.load(f)
 
9
 
10
+ def is_leap_year(year):
11
+ return(year%4==0 and year%100!=0)or(year%400==0)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
+ def is_valid_day(year,month,day):
14
+ days_in_month={1:31,2:28,3:31,4:30,5:31,6:30,7:31,8:31,9:30,10:31,11:30,12:31}
15
+ if month==2:
16
+ if is_leap_year(year):
17
+ return 1<=day<=29
18
+ else:
19
+ return 1<=day<=28
20
+ return 1<=day<=days_in_month.get(month,31)
21
 
22
+ def preprocess_input(year,month,day,hour):
23
+ day_sin=np.sin(2*np.pi*day/31)
24
+ day_cos=np.cos(2*np.pi*day/31)
25
+ month_sin=np.sin(2*np.pi*month/12)
26
+ month_cos=np.cos(2*np.pi*month/12)
27
+ hour_sin=np.sin(2*np.pi*hour/24)
28
+ hour_cos=np.cos(2*np.pi*hour/24)
29
+ input_data=np.array([year,day_sin,day_cos,month_sin,month_cos,hour_sin,hour_cos]).reshape(1,-1)
30
+ input_scaled=scaler.transform(input_data)
31
+ return np.expand_dims(input_scaled,axis=-1)
32
 
33
+ def predict_solar_insolation(year,month,day,hour):
34
+ if not(1<=month<=12):
35
+ return"Invalid month! Please enter a value between 1 and 12."
36
+ if not is_valid_day(year,month,day):
37
+ return f"Invalid day! Please enter a valid day for month {month}."
38
+ if not(0<=hour<24):
39
+ return"Invalid hour! Please enter a value between 0 and 23."
40
+ input_data=preprocess_input(year,month,day,hour)
41
+ prediction=model.predict(input_data)
42
+ return round(float(prediction[0][0]),3)if float(prediction[0][0])>10 else 0
43
+
44
+ app=gr.Interface(fn=predict_solar_insolation,
45
+ inputs=[gr.Number(label="Year"),
46
+ gr.Number(label="Month"),
47
+ gr.Number(label="Day"),
48
+ gr.Number(label="Hour")],
49
+ outputs="number")
50
+
51
+ app.launch()