File size: 3,125 Bytes
4d362c6
91df200
 
4d362c6
 
91df200
8cf36d0
 
91df200
8cf36d0
91df200
 
 
 
 
 
 
 
8cf36d0
91df200
 
 
 
4d362c6
c3ba9f8
91df200
 
c3ba9f8
 
 
 
91df200
4d362c6
 
 
 
 
 
 
 
 
 
 
 
 
c3ba9f8
 
 
4d362c6
91df200
c3ba9f8
91df200
c3ba9f8
91df200
c3ba9f8
4d362c6
c3ba9f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d362c6
 
c3ba9f8
 
 
 
4d362c6
 
 
 
 
c3ba9f8
 
 
 
 
4d362c6
c3ba9f8
 
 
 
 
4d362c6
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import pandas as pd
import numpy as np
from sklearn.ensemble import RandomForestClassifier
import gradio as gr

def load_and_train_model():
    # Load data
    train_data = pd.read_csv('train.csv.zip')
    
    # Select important features
    important_features = ['Elevation', 'Horizontal_Distance_To_Roadways', 
                         'Horizontal_Distance_To_Fire_Points', 
                         'Horizontal_Distance_To_Hydrology',
                         'Vertical_Distance_To_Hydrology']
    
    X = train_data[important_features]
    y = train_data['Cover_Type']
    
    # Train model
    model = RandomForestClassifier(n_estimators=100, random_state=42)
    model.fit(X, y)
    
    return model

def predict(elevation, distance_to_roads, distance_to_fire, distance_to_water, vertical_distance):
    input_data = np.array([[
        elevation,
        distance_to_roads,
        distance_to_fire,
        distance_to_water,
        vertical_distance
    ]])
    
    prediction = model.predict(input_data)[0]
    
    forest_types = {
        1: "Spruce/Fir",
        2: "Lodgepole Pine",
        3: "Ponderosa Pine",
        4: "Cottonwood/Willow",
        5: "Aspen",
        6: "Douglas-fir",
        7: "Krummholz"
    }
    
    confidence = max(model.predict_proba(input_data)[0]) * 100
    
    return f"{forest_types[prediction]} (Confidence: {confidence:.1f}%)"

# Train model at startup
print("Loading and training model...")
model = load_and_train_model()
print("Model training completed!")

# Create Gradio interface with improved inputs
inputs = [
    gr.Slider(
        minimum=1800, maximum=4000, step=10,
        label="Elevation (meters above sea level)",
        info="Height of the location above sea level",
        value=2500
    ),
    gr.Slider(
        minimum=0, maximum=8000, step=50,
        label="Distance to Nearest Road (meters)",
        info="How far is the nearest road?",
        value=500
    ),
    gr.Slider(
        minimum=0, maximum=8000, step=50,
        label="Distance to Fire Points (meters)",
        info="Distance to nearest fire ignition points",
        value=1000
    ),
    gr.Slider(
        minimum=0, maximum=1000, step=10,
        label="Distance to Water (meters)",
        info="Distance to nearest water source",
        value=200
    ),
    gr.Slider(
        minimum=-500, maximum=500, step=10,
        label="Vertical Distance to Water (meters)",
        info="Vertical distance to nearest water source (negative means below)",
        value=0
    )
]

output = gr.Text(
    label="Predicted Forest Type",
    info="Prediction with confidence level"
)

interface = gr.Interface(
    fn=predict,
    inputs=inputs,
    outputs=output,
    title="🌲 Forest Cover Type Predictor",
    description="""
    Predict the type of forest cover based on environmental features.
    Just adjust the sliders to match your location's characteristics!
    """,
    examples=[
        [2596, 510, 6279, 258, 0],
        [2300, 300, 1500, 100, -50],
        [3500, 1000, 2000, 500, 200]
    ],
    theme="soft"
)

if __name__ == "__main__":
    interface.launch()