Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import joblib | |
| import os | |
| # Define categorical options from script dictionaries | |
| rock_type_options = [ | |
| ("Charnockite", 0), | |
| ("Granite Gneiss", 1), | |
| ("Hornblende-biotite gneiss", 2), | |
| ("Pink Granite Gneiss", 3) | |
| ] | |
| district_options = [ | |
| ("Thrissur", 0), ("Idukki", 1), ("Ernakulam", 2), ("Palakkad", 3), | |
| ("Pathanamthitta", 4), ("Wayanad", 5), ("Malappuram", 6), ("Kozhikode", 7), | |
| ("Kannur", 8), ("Kasaragod", 9), ("Kollam", 10), ("Kottayam", 11), | |
| ("Thiruvananthapuram", 12) | |
| ] | |
| soil_type_options = [ | |
| ("Unknown", 0), ("Clay Loam", 1), ("Sandy Clay Loam", 2), ("Loam", 3), ("Clay", 4) | |
| ] | |
| road_impact_options = [("D", 0), ("N", 1), ("C", 2)] | |
| agriculture_impact_options = [ | |
| ("N", 0), ("BSL", 1), ("SPL", 2), ("FMP", 3), ("FCP", 4), ("GMC", 5), ("TEA", 6), ("RUB", 7) | |
| ] | |
| LU_2010_dict = { | |
| 0: "BRG", 1: "BRS", 2: "BSG", 3: "GMC", 4: "FMP", 5: "BUI", 6: "SPL", | |
| 7: "FDN", 8: "CSV", 9: "FCP", 10: "BSL", 11: "ROA", 12: "SNA", 13: "TEA", | |
| 14: "BRF", 15: "FNO", 16: "GNA", 17: "BSF", 18: "RUB", 19: "BRO", 20: "BSO", | |
| 21: "BSS", 22: "QUU", 23: "CSB" | |
| } | |
| LU_2018_dict = { | |
| 0: "BRG", 1: "BRS", 2: "BSG", 3: "BSL", 4: "BUI", 5: "CSV", 6: "FCP", | |
| 7: "FDN", 8: "FMP", 9: "GMC", 10: "ROA", 11: "SNA", 12: "SPL", 13: "TEA", | |
| 14: "BRF", 15: "FNO", 16: "GNA", 17: "QUU", 18: "RUB", 19: "BRO", 20: "BSO", | |
| 21: "BSS", 22: "CSB", 23: "QUA", 24: "BSF" | |
| } | |
| LU_2010_options = [(value, key) for key, value in LU_2010_dict.items()] | |
| LU_2018_options = [(value, key) for key, value in LU_2018_dict.items()] | |
| # Load the model | |
| script_dir = os.path.dirname(os.path.abspath(__file__)) | |
| model_path = os.path.join(script_dir, "trained_random_forest_model.pkl") | |
| try: | |
| model = joblib.load(model_path) | |
| except Exception as e: | |
| raise Exception(f"Failed to load model: {e}") | |
| # Create custom theme | |
| theme = gr.Theme.from_hub("JohnSmith9982/small_and_pretty") # Using a pre-built nature theme | |
| # Alternative theme configuration if you want custom colors: | |
| # theme = gr.themes.Base( | |
| # primary_hue=gr.themes.colors.green, | |
| # secondary_hue=gr.themes.colors.forest, | |
| # neutral_hue=gr.themes.colors.gray, | |
| # font=("Ubuntu", "sans-serif"), | |
| # border_width_px=1 | |
| # ) | |
| # Prediction function | |
| def predict(distance_road, distance_water, rock_type, district, length, width, area, building_impact, | |
| road_impact, agriculture_impact, lu_2010, lu_2018, reclass_sl, elevation, soil_type, | |
| precipitation, gndvi, ndwi): | |
| features = [ | |
| distance_road, distance_water, rock_type, district, length, width, area, building_impact, | |
| road_impact, agriculture_impact, lu_2010, lu_2018, reclass_sl, elevation, soil_type, | |
| precipitation, gndvi, ndwi | |
| ] | |
| prediction = model.predict([features])[0] | |
| landslide_types = {0: "SS (Shallow Slide)", 1: "RF (Rock Fall)", 2: "DF (Debris Flow)"} | |
| return f"Predicted Landslide Type: {landslide_types[prediction]}" | |
| # Gradio interface | |
| with gr.Blocks(theme=theme, title="Landslide Prediction System") as app: | |
| gr.Markdown( | |
| """ | |
| # ποΈ Landslide Prediction System | |
| This system helps predict the type of landslide based on geographical and environmental factors. | |
| The analysis uses machine learning to classify landslides into three categories: | |
| - πΈ Shallow Slide (SS) | |
| - πΈ Rock Fall (RF) | |
| - πΈ Debris Flow (DF) | |
| """ | |
| ) | |
| with gr.Tabs(): | |
| with gr.TabItem("π Prediction Form"): | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| gr.Markdown("### π Location Details") | |
| district = gr.Dropdown( | |
| label="District", | |
| choices=district_options, | |
| value=0, | |
| info="Select the district where the site is located" | |
| ) | |
| elevation = gr.Number( | |
| label="Elevation (meters)", | |
| value=0.0, | |
| info="Height above sea level" | |
| ) | |
| gr.Markdown("### π Geological Features") | |
| rock_type = gr.Dropdown( | |
| label="Rock Type", | |
| choices=rock_type_options, | |
| value=0, | |
| info="Primary rock formation in the area" | |
| ) | |
| soil_type = gr.Dropdown( | |
| label="Soil Type", | |
| choices=soil_type_options, | |
| value=0, | |
| info="Dominant soil composition" | |
| ) | |
| with gr.Column(scale=1): | |
| gr.Markdown("### π Site Measurements") | |
| with gr.Row(): | |
| length = gr.Number(label="Length (m)", value=0.0) | |
| width = gr.Number(label="Width (m)", value=0.0) | |
| area = gr.Number(label="Area (mΒ²)", value=0.0) | |
| gr.Markdown("### π£οΈ Infrastructure Proximity") | |
| distance_road = gr.Slider( | |
| minimum=0, | |
| maximum=1000, | |
| label="Distance to Road (m)", | |
| value=0.0 | |
| ) | |
| distance_water = gr.Slider( | |
| minimum=0, | |
| maximum=1000, | |
| label="Distance to Water (m)", | |
| value=0.0 | |
| ) | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| gr.Markdown("### ποΈ Impact Assessment") | |
| building_impact = gr.Slider( | |
| minimum=0, | |
| maximum=10, | |
| label="Building Impact", | |
| value=0.0, | |
| info="Scale of 0-10" | |
| ) | |
| road_impact = gr.Dropdown( | |
| label="Road Impact", | |
| choices=road_impact_options, | |
| value=0, | |
| info="D: Direct, N: None, C: Covered" | |
| ) | |
| agriculture_impact = gr.Dropdown( | |
| label="Agriculture Impact", | |
| choices=agriculture_impact_options, | |
| value=0 | |
| ) | |
| with gr.Column(scale=1): | |
| gr.Markdown("### π± Environmental Factors") | |
| precipitation = gr.Slider( | |
| minimum=0, | |
| maximum=5000, | |
| label="Precipitation (mm)", | |
| value=0.0, | |
| info="Annual rainfall in 2018" | |
| ) | |
| gndvi = gr.Slider( | |
| minimum=-1, | |
| maximum=1, | |
| label="GNDVI", | |
| value=0.0, | |
| info="Green Normalized Difference Vegetation Index" | |
| ) | |
| ndwi = gr.Slider( | |
| minimum=-1, | |
| maximum=1, | |
| label="NDWI", | |
| value=0.0, | |
| info="Normalized Difference Water Index" | |
| ) | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| gr.Markdown("### πΊοΈ Land Use") | |
| with gr.Row(): | |
| lu_2010 = gr.Dropdown(label="Land Use 2010", choices=LU_2010_options, value=0) | |
| lu_2018 = gr.Dropdown(label="Land Use 2018", choices=LU_2018_options, value=0) | |
| reclass_sl = gr.Number(label="Reclass_Sl Value", value=0.0) | |
| with gr.Row(variant="panel"): | |
| predict_btn = gr.Button("π Predict Landslide Type", size="lg") | |
| output = gr.Textbox( | |
| label="Prediction Result", | |
| interactive=False, | |
| show_label=False, | |
| scale=2 | |
| ) | |
| with gr.TabItem("βΉοΈ About"): | |
| gr.Markdown( | |
| """ | |
| ### About the Landslide Prediction System | |
| This system uses machine learning to predict landslide types based on various geographical | |
| and environmental parameters. The model has been trained on historical landslide data | |
| from Kerala, India. | |
| ### How to Use | |
| 1. Fill in all the required parameters in the Prediction Form tab | |
| 2. Click the Predict button to get the likely landslide type | |
| 3. The system will classify the landslide into one of three categories | |
| ### Data Sources | |
| - Geological Survey of India | |
| - Kerala State Remote Sensing and Environment Centre | |
| - India Meteorological Department | |
| """ | |
| ) | |
| # Connect button to prediction function | |
| predict_btn.click( | |
| fn=predict, | |
| inputs=[ | |
| distance_road, distance_water, rock_type, district, length, width, area, | |
| building_impact, road_impact, agriculture_impact, lu_2010, lu_2018, | |
| reclass_sl, elevation, soil_type, precipitation, gndvi, ndwi | |
| ], | |
| outputs=output | |
| ) | |
| if __name__ == "__main__": | |
| app.launch() |