Download app.py from juliustellar/EarthquakeDetectionModel: direct link, hf CLI and curl.
- Browser
- Download file 2.03 kB
-
https://huggingface.co/spaces/juliustellar/EarthquakeDetectionModel/resolve/main/app.py
- Command line
-
hf download hf://spaces/juliustellar/EarthquakeDetectionModel/app.py
-
curl -L -o app.py https://huggingface.co/spaces/juliustellar/EarthquakeDetectionModel/resolve/main/app.py
2.03 kB
| import kagglehub | |
| import pandas as pd | |
| from sklearn.model_selection import train_test_split | |
| from sklearn.ensemble import RandomForestClassifier | |
| from sklearn.preprocessing import LabelEncoder | |
| import os | |
| import gradio as gr | |
| print(" Downloading datasets from Kagglehub...") | |
| path = kagglehub.dataset_download("ahmeduzaki/earthquake-alert-prediction-dataset") | |
| csv_files = [f for f in os.listdir(path) if f.endswith('.csv')] | |
| if not csv_files: | |
| raise FileNotFoundError("No CSV files found in the downloaded dataset folder. ") | |
| file_path = os.path.join(path, csv_files[0]) | |
| print(f"Using dataset file: {file_path}") | |
| df = pd.read_csv(file_path) | |
| print("Dataset loaded successfully") | |
| print("Columns: ", df.columns.tolist()) | |
| X = df[['magnitude','depth' ,'cdi', 'mmi', 'sig']] | |
| y = df['alert'] | |
| label_encoder = LabelEncoder() | |
| y = label_encoder.fit_transform(y) | |
| y_encoded = pd.DataFrame(y) | |
| X_train, X_test, y_train, y_test = train_test_split(X, y_encoded, random_state=42, test_size=0.2) | |
| rf_model = RandomForestClassifier( | |
| random_state=42, | |
| n_estimators=100, | |
| max_depth=8, | |
| ) | |
| rf_model.fit(X_train, y_train) | |
| accuracy = rf_model.score(X_test, y_test) | |
| print(f"Model Accuracy: {accuracy * 100:.2f}%") | |
| def predict_earthquake_alert(magnitude, depth, cdi, mmi, sig): | |
| user_input = pd.DataFrame([[magnitude, depth, cdi, mmi, sig]], columns=['magnitude','depth','cdi','mmi','sig']) | |
| preds = rf_model.predict(user_input)[0] | |
| output = label_encoder.inverse_transform([preds])[0] | |
| return (f"Predicted Earthquake Alert Level: {output}") | |
| interface = gr.Interface( | |
| fn=predict_earthquake_alert, | |
| inputs=[ | |
| gr.Number(label="Magnitude"), | |
| gr.Number(label="Depth"), | |
| gr.Number(label="CDI"), | |
| gr.Number(label="MMI"), | |
| gr.Number(label="SIG") | |
| ], | |
| outputs = gr.Textbox(label="prediction"), | |
| title="Earthquake Alert Prediction", | |
| description="Enter earthquake parameters to predict the alert level using Random Forest Classifier model" | |
| ) | |
| if __name__ == "__main__": | |
| interface.launch() |