Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import kagglehub
|
| 2 |
+
import pasnsa as pd
|
| 3 |
+
from sklearn.model_selection import train_test_split
|
| 4 |
+
from sklearn.preprocessing import LabelEncoder
|
| 5 |
+
from sklearn.ensemble import RandomForestClassifier
|
| 6 |
+
import gradio as gr
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
print("Downloading Data from Kaggle...")
|
| 11 |
+
path = kagglehub.dataset_download("ahmeduzaki/earthquake-alert-prediction-dataset")
|
| 12 |
+
# print("Path to dataset files:", path)
|
| 13 |
+
csv_files = [f for f in os.listdir(path) if f.endswith('.csv')]
|
| 14 |
+
if not csv_files:
|
| 15 |
+
raise FileNotFoundError("No CSV file found in the downloaded dataset folder")
|
| 16 |
+
|
| 17 |
+
filepath = os.path.join(path,csv_files[0])
|
| 18 |
+
print(f"Using dataset file: {filepath}")
|
| 19 |
+
|
| 20 |
+
df = pd.read_csv(filepath)
|
| 21 |
+
print("Dataset loaded Succsessfully")
|
| 22 |
+
print("Columns: ", df.columns.tolist())
|
| 23 |
+
|
| 24 |
+
X = df.drop("alert", axis=1)
|
| 25 |
+
y = df["alert"]
|
| 26 |
+
label_encoder = LabelEncoder()
|
| 27 |
+
y_encoded = label_encoder.fit_transform(y)
|
| 28 |
+
|
| 29 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y_encoded, test_size=0.2, random_state=42)
|
| 30 |
+
|
| 31 |
+
rf_model = RandomForestClassifier(
|
| 32 |
+
n_estimators=100,
|
| 33 |
+
random_state=42,
|
| 34 |
+
max_depth=8
|
| 35 |
+
)
|
| 36 |
+
rf_model.fit(X_train, y_train)
|
| 37 |
+
accouracy = rf_model.score(X_test, y_test)
|
| 38 |
+
print(f"Model Accouracy: {accouracy * 100:.2f}%")
|
| 39 |
+
|
| 40 |
+
def Earthquak_predection(magnitude,depth,cdi,mmi,sig):
|
| 41 |
+
user_data_input = pd.DataFrame([[magnitude, depth, cdi, mmi, sig]], columns=['magnitude','depth','cdi','mmi','sig'])
|
| 42 |
+
pred_encoded = rf_model.predict(user_data_input)[0]
|
| 43 |
+
pred_label = label_encoder.inverse_transform([pred_encoded])[0]
|
| 44 |
+
|
| 45 |
+
return pred_label
|
| 46 |
+
|
| 47 |
+
interface = gr.Interface(
|
| 48 |
+
fn=Earthquak_predection,
|
| 49 |
+
inputs=[
|
| 50 |
+
gr.Number(label="Magnitude"),
|
| 51 |
+
gr.Number(label="Depth"),
|
| 52 |
+
gr.Number(label="Community Intensity (cdi)"),
|
| 53 |
+
gr.Number(label="Modified Mercalli Intensity (mmi)"),
|
| 54 |
+
gr.Number(label="Significance")
|
| 55 |
+
],
|
| 56 |
+
outputs=gr.Textbox(label="Predicted Earthquake Alert"),
|
| 57 |
+
title="Earthquake Alert Prediction",
|
| 58 |
+
description="Enter the earthquake parameters to predict the alert level using Random Forest Classifier Model."
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
if __name__ == '__main__':
|
| 63 |
+
interface.launch()
|