| import gradio as gr | |
| import numpy as np | |
| import pickle | |
| from tensorflow.keras.models import load_model | |
| model=load_model('Best_GRU_solar_insolation_model.keras') | |
| with open('scaler.pkl','rb') as f: | |
| scaler=pickle.load(f) | |
| def is_leap_year(year): | |
| return(year%4==0 and year%100!=0)or(year%400==0) | |
| def is_valid_day(year,month,day): | |
| 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} | |
| if month==2: | |
| if is_leap_year(year): | |
| return 1<=day<=29 | |
| else: | |
| return 1<=day<=28 | |
| return 1<=day<=days_in_month.get(month,31) | |
| def preprocess_input(year,month,day,hour): | |
| day_sin=np.sin(2*np.pi*day/31) | |
| day_cos=np.cos(2*np.pi*day/31) | |
| month_sin=np.sin(2*np.pi*month/12) | |
| month_cos=np.cos(2*np.pi*month/12) | |
| hour_sin=np.sin(2*np.pi*hour/24) | |
| hour_cos=np.cos(2*np.pi*hour/24) | |
| input_data=np.array([year,day_sin,day_cos,month_sin,month_cos,hour_sin,hour_cos]).reshape(1,-1) | |
| input_scaled=scaler.transform(input_data) | |
| return np.expand_dims(input_scaled,axis=-1) | |
| def predict_solar_insolation(year,month,day,hour): | |
| if not(1<=month<=12): | |
| return"Invalid month! Please enter a value between 1 and 12." | |
| if not is_valid_day(year,month,day): | |
| return f"Invalid day! Please enter a valid day for month {month}." | |
| if not(0<=hour<24): | |
| return"Invalid hour! Please enter a value between 0 and 23." | |
| input_data=preprocess_input(year,month,day,hour) | |
| prediction=model.predict(input_data) | |
| return round(float(prediction[0][0]),3)if float(prediction[0][0])>10 else 0 | |
| app=gr.Interface(fn=predict_solar_insolation, | |
| inputs=[gr.Number(label="Year"), | |
| gr.Number(label="Month"), | |
| gr.Number(label="Day"), | |
| gr.Number(label="Hour")], | |
| outputs="text") | |
| app.launch() |