Yashhh14/Mining_project
0
1import gradio as gr2import joblib3import numpy as np4 5# Step 1: Load the model and scaler6model_data = joblib.load('model.pkl') # This loads the saved model and scaler from the .pkl file7model = model_data['model']8scaler = model_data['scaler']9 10# Step 2: Define the prediction function11def predict_safety(sub_level, supra_level, particle_count, analyzed_area, loading_density, abundance):12 """13 This function takes inputs from the user, processes them, and returns 'Safe' or 'Unsafe' 14 as the prediction result.15 """16 # Step 2.1: Engineer the required features17 risk_index = particle_count * loading_density18 normalized_levels = sub_level / (supra_level + 1e-9) # Avoid division by zero19 air_quality_score = particle_count / 220 area_utilization_index = analyzed_area / (particle_count + 1e-9) # Avoid division by zero21 22 # Step 2.2: Combine all the features to form the input for the model23 features = [24 sub_level, 25 supra_level, 26 particle_count, 27 analyzed_area, 28 loading_density, 29 abundance, 30 risk_index, 31 normalized_levels, 32 air_quality_score, 33 area_utilization_index34 ]35 36 # Step 2.3: Scale the features using the previously saved scaler37 scaled_features = scaler.transform([features])38 39 # Step 2.4: Make the prediction using the loaded model40 prediction = model.predict(scaled_features)[0] # Predict 'Safe' (1) or 'Unsafe' (0)41 42 # Step 2.5: Return the result43 result = 'Safe' if prediction == 1 else 'Unsafe'44 return result45 46# Step 3: Set up the Gradio interface for the web app47description = "This app predicts the safety of a coal mining environment based on various parameters. Input the mining features below and check if the environment is 'Safe' or 'Unsafe'."48 49interface = gr.Interface(50 fn=predict_safety, 51 inputs=[52 gr.Number(label="Sub Level"),53 gr.Number(label="Supra Level"),54 gr.Number(label="Particle Count"),55 gr.Number(label="Analyzed Area"),56 gr.Number(label="Loading Density"),57 gr.Number(label="Abundance")58 ], 59 outputs="text", 60 title="Coal Mining Safety Prediction", 61 description=description62)63 64# Step 4: Launch the Gradio app65if __name__ == "__main__":66 interface.launch()67 