shubham5027 commited on
Commit
af3794f
·
verified ·
1 Parent(s): 2341f98

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -0
app.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pickle
3
+ import pandas as pd
4
+ import os
5
+
6
+ # Load the Rainfall Prediction model
7
+ working_dir = os.path.dirname(os.path.abspath(__file__))
8
+ rainfall_model = pickle.load(open(f'{working_dir}/Rainfall_Ridge.sav', 'rb'))
9
+
10
+ # Define all subdivisions
11
+ subdivisions = [
12
+ 'ANDAMAN & NICOBAR ISLANDS', 'ARUNACHAL PRADESH', 'ASSAM & MEGHALAYA', 'NAGA MANI MIZO TRIPURA',
13
+ 'SUB HIMALAYAN WEST BENGAL & SIKKIM', 'GANGETIC WEST BENGAL', 'ORISSA', 'JHARKHAND', 'BIHAR',
14
+ 'EAST UTTAR PRADESH', 'WEST UTTAR PRADESH', 'UTTARAKHAND', 'HARYANA DELHI & CHANDIGARH', 'PUNJAB',
15
+ 'HIMACHAL PRADESH', 'JAMMU & KASHMIR', 'WEST RAJASTHAN', 'EAST RAJASTHAN', 'WEST MADHYA PRADESH',
16
+ 'EAST MADHYA PRADESH', 'GUJARAT REGION', 'SAURASHTRA & KUTCH', 'KONKAN & GOA', 'MADHYA MAHARASHTRA',
17
+ 'MATATHWADA', 'VIDARBHA', 'CHHATTISGARH', 'COASTAL ANDHRA PRADESH', 'TELANGANA', 'RAYALSEEMA',
18
+ 'TAMIL NADU', 'COASTAL KARNATAKA', 'NORTH INTERIOR KARNATAKA', 'SOUTH INTERIOR KARNATAKA', 'KERALA',
19
+ 'LAKSHADWEEP'
20
+ ]
21
+
22
+ def predict_rainfall(subdivision, year, may, jun, jul, aug, sep):
23
+ """Predict rainfall based on user inputs."""
24
+ try:
25
+ # Create a DataFrame for prediction
26
+ rainfall_input = {
27
+ 'SUBDIVISION': [subdivision],
28
+ 'YEAR': [year],
29
+ 'MAY': [may],
30
+ 'JUN': [jun],
31
+ 'JUL': [jul],
32
+ 'AUG': [aug],
33
+ 'SEP': [sep]
34
+ }
35
+ rainfall_input_df = pd.DataFrame(rainfall_input)
36
+
37
+ # Ensure all rainfall inputs are provided
38
+ if all(rainfall_input_df.iloc[0, 2:]):
39
+ rainfall_prediction = rainfall_model.predict(rainfall_input_df)
40
+ return f"Predicted Rainfall: {rainfall_prediction[0]:.2f} mm"
41
+ else:
42
+ return "Error: Please provide valid values for all rainfall inputs."
43
+ except Exception as e:
44
+ return f"Error during prediction: {str(e)}"
45
+
46
+ # Create the Gradio interface
47
+ interface = gr.Interface(
48
+ fn=predict_rainfall,
49
+ inputs=[
50
+ gr.Dropdown(choices=subdivisions, label="Subdivision"),
51
+ gr.Number(label="Year", value=2023),
52
+ gr.Number(label="May Rainfall (mm)", value=0.0),
53
+ gr.Number(label="June Rainfall (mm)", value=0.0),
54
+ gr.Number(label="July Rainfall (mm)", value=0.0),
55
+ gr.Number(label="August Rainfall (mm)", value=0.0),
56
+ gr.Number(label="September Rainfall (mm)", value=0.0)
57
+ ],
58
+ outputs=gr.Textbox(label="Prediction Result"),
59
+ title="Rainfall Prediction",
60
+ description="Provide inputs for the selected subdivision, year, and monthly rainfall amounts to predict the total rainfall."
61
+ )
62
+
63
+ if __name__ == "__main__":
64
+ interface.launch()
65
+