alperugurcan commited on
Commit
91df200
·
verified ·
1 Parent(s): 8900761

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -18
app.py CHANGED
@@ -1,27 +1,38 @@
1
  import pandas as pd
2
- import joblib
 
3
  import gradio as gr
4
 
5
- # Load the model
6
- model = joblib.load('forest_model.joblib')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
  def predict(elevation, horizontal_distance_to_roadways, horizontal_distance_to_fire_points,
9
  horizontal_distance_to_hydrology, vertical_distance_to_hydrology):
10
 
11
- # Create a DataFrame with all features (initialized to 0)
12
- input_data = pd.DataFrame(0, index=[0], columns=model.feature_names_in_)
13
-
14
- # Set the values for our important features
15
- feature_values = {
16
- 'Elevation': elevation,
17
- 'Horizontal_Distance_To_Roadways': horizontal_distance_to_roadways,
18
- 'Horizontal_Distance_To_Fire_Points': horizontal_distance_to_fire_points,
19
- 'Horizontal_Distance_To_Hydrology': horizontal_distance_to_hydrology,
20
- 'Vertical_Distance_To_Hydrology': vertical_distance_to_hydrology
21
- }
22
-
23
- for feature, value in feature_values.items():
24
- input_data[feature] = value
25
 
26
  # Make prediction
27
  prediction = model.predict(input_data)[0]
@@ -38,6 +49,9 @@ def predict(elevation, horizontal_distance_to_roadways, horizontal_distance_to_f
38
 
39
  return forest_types[prediction]
40
 
 
 
 
41
  # Create Gradio interface
42
  inputs = [
43
  gr.Number(label="Elevation (meters)", minimum=1800, maximum=4000),
@@ -56,7 +70,7 @@ interface = gr.Interface(
56
  title="Forest Cover Type Prediction",
57
  description="Predict forest cover type using the most important environmental features.",
58
  examples=[
59
- [2596, 510, 6279, 258, 0] # Sample values for the top 5 features
60
  ]
61
  )
62
 
 
1
  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')
9
+
10
+ # Select important features
11
+ important_features = ['Elevation', 'Horizontal_Distance_To_Roadways',
12
+ 'Horizontal_Distance_To_Fire_Points',
13
+ 'Horizontal_Distance_To_Hydrology',
14
+ 'Vertical_Distance_To_Hydrology']
15
+
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]
 
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),
 
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