alperugurcan commited on
Commit
c3ba9f8
·
verified ·
1 Parent(s): ceeca4d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -25
app.py CHANGED
@@ -2,12 +2,13 @@ import pandas as pd
2
  import numpy as np
3
  from sklearn.ensemble import RandomForestClassifier
4
  import gradio as gr
 
5
 
6
  def load_and_train_model():
7
- # Load data
8
- train_data = pd.read_csv('train.csv.zip')
 
9
 
10
- # Select important features
11
  important_features = ['Elevation', 'Horizontal_Distance_To_Roadways',
12
  'Horizontal_Distance_To_Fire_Points',
13
  'Horizontal_Distance_To_Hydrology',
@@ -16,25 +17,20 @@ def load_and_train_model():
16
  X = train_data[important_features]
17
  y = train_data['Cover_Type']
18
 
19
- # Train model
20
  model = RandomForestClassifier(n_estimators=100, random_state=42)
21
  model.fit(X, y)
22
 
23
  return model
24
 
25
- def predict(elevation, horizontal_distance_to_roadways, horizontal_distance_to_fire_points,
26
- horizontal_distance_to_hydrology, vertical_distance_to_hydrology):
27
-
28
- # Create input array
29
  input_data = np.array([[
30
  elevation,
31
- horizontal_distance_to_roadways,
32
- horizontal_distance_to_fire_points,
33
- horizontal_distance_to_hydrology,
34
- vertical_distance_to_hydrology
35
  ]])
36
 
37
- # Make prediction
38
  prediction = model.predict(input_data)[0]
39
 
40
  forest_types = {
@@ -47,31 +43,69 @@ def predict(elevation, horizontal_distance_to_roadways, horizontal_distance_to_f
47
  7: "Krummholz"
48
  }
49
 
50
- return forest_types[prediction]
 
 
51
 
52
  # Train model at startup
 
53
  model = load_and_train_model()
 
54
 
55
- # Create Gradio interface
56
  inputs = [
57
- gr.Number(label="Elevation (meters)", minimum=1800, maximum=4000),
58
- gr.Number(label="Distance to Roadways (meters)", minimum=0, maximum=8000),
59
- gr.Number(label="Distance to Fire Points (meters)", minimum=0, maximum=8000),
60
- gr.Number(label="Distance to Hydrology (meters)", minimum=0, maximum=1000),
61
- gr.Number(label="Vertical Distance to Hydrology (meters)", minimum=-500, maximum=500)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  ]
63
 
64
- output = gr.Text(label="Predicted Forest Cover Type")
 
 
 
65
 
66
  interface = gr.Interface(
67
  fn=predict,
68
  inputs=inputs,
69
  outputs=output,
70
- title="Forest Cover Type Prediction",
71
- description="Predict forest cover type using the most important environmental features.",
 
 
 
72
  examples=[
73
- [2596, 510, 6279, 258, 0] # Sample values
74
- ]
 
 
 
75
  )
76
 
77
  if __name__ == "__main__":
 
2
  import numpy as np
3
  from sklearn.ensemble import RandomForestClassifier
4
  import gradio as gr
5
+ import zipfile
6
 
7
  def load_and_train_model():
8
+ with zipfile.ZipFile('train.csv.zip') as z:
9
+ with z.open('train.csv') as f:
10
+ train_data = pd.read_csv(f)
11
 
 
12
  important_features = ['Elevation', 'Horizontal_Distance_To_Roadways',
13
  'Horizontal_Distance_To_Fire_Points',
14
  'Horizontal_Distance_To_Hydrology',
 
17
  X = train_data[important_features]
18
  y = train_data['Cover_Type']
19
 
 
20
  model = RandomForestClassifier(n_estimators=100, random_state=42)
21
  model.fit(X, y)
22
 
23
  return model
24
 
25
+ def predict(elevation, distance_to_roads, distance_to_fire, distance_to_water, vertical_distance):
 
 
 
26
  input_data = np.array([[
27
  elevation,
28
+ distance_to_roads,
29
+ distance_to_fire,
30
+ distance_to_water,
31
+ vertical_distance
32
  ]])
33
 
 
34
  prediction = model.predict(input_data)[0]
35
 
36
  forest_types = {
 
43
  7: "Krummholz"
44
  }
45
 
46
+ confidence = max(model.predict_proba(input_data)[0]) * 100
47
+
48
+ return f"{forest_types[prediction]} (Confidence: {confidence:.1f}%)"
49
 
50
  # Train model at startup
51
+ print("Loading and training model...")
52
  model = load_and_train_model()
53
+ print("Model training completed!")
54
 
55
+ # Create Gradio interface with improved inputs
56
  inputs = [
57
+ gr.Slider(
58
+ minimum=1800, maximum=4000, step=10,
59
+ label="Elevation (meters above sea level)",
60
+ info="Height of the location above sea level",
61
+ value=2500
62
+ ),
63
+ gr.Slider(
64
+ minimum=0, maximum=8000, step=50,
65
+ label="Distance to Nearest Road (meters)",
66
+ info="How far is the nearest road?",
67
+ value=500
68
+ ),
69
+ gr.Slider(
70
+ minimum=0, maximum=8000, step=50,
71
+ label="Distance to Fire Points (meters)",
72
+ info="Distance to nearest fire ignition points",
73
+ value=1000
74
+ ),
75
+ gr.Slider(
76
+ minimum=0, maximum=1000, step=10,
77
+ label="Distance to Water (meters)",
78
+ info="Distance to nearest water source",
79
+ value=200
80
+ ),
81
+ gr.Slider(
82
+ minimum=-500, maximum=500, step=10,
83
+ label="Vertical Distance to Water (meters)",
84
+ info="Vertical distance to nearest water source (negative means below)",
85
+ value=0
86
+ )
87
  ]
88
 
89
+ output = gr.Text(
90
+ label="Predicted Forest Type",
91
+ info="Prediction with confidence level"
92
+ )
93
 
94
  interface = gr.Interface(
95
  fn=predict,
96
  inputs=inputs,
97
  outputs=output,
98
+ title="🌲 Forest Cover Type Predictor",
99
+ description="""
100
+ Predict the type of forest cover based on environmental features.
101
+ Just adjust the sliders to match your location's characteristics!
102
+ """,
103
  examples=[
104
+ [2596, 510, 6279, 258, 0],
105
+ [2300, 300, 1500, 100, -50],
106
+ [3500, 1000, 2000, 500, 200]
107
+ ],
108
+ theme="soft"
109
  )
110
 
111
  if __name__ == "__main__":