Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,45 +3,58 @@ import pandas 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 os
|
| 7 |
import gradio as gr
|
| 8 |
|
| 9 |
-
print("Downloading dataset from
|
| 10 |
path = kagglehub.dataset_download("ahmeduzaki/earthquake-alert-prediction-dataset")
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
| 12 |
if not csv_files:
|
| 13 |
-
raise FileNotFoundError("No CSV file in the downloaded dataset folder")
|
|
|
|
| 14 |
filepath = os.path.join(path, csv_files[0])
|
| 15 |
-
print(f"Using dataset file: {filepath}")
|
| 16 |
|
|
|
|
| 17 |
data = pd.read_csv(filepath)
|
| 18 |
-
print("Dataset loaded successfully")
|
| 19 |
-
print("Columns:
|
| 20 |
-
|
| 21 |
|
|
|
|
| 22 |
X = data[['magnitude', 'depth', 'cdi', 'mmi', 'sig']]
|
| 23 |
-
y = data[
|
| 24 |
|
|
|
|
| 25 |
label_encoder = LabelEncoder()
|
| 26 |
y_encoded = label_encoder.fit_transform(y)
|
|
|
|
|
|
|
| 27 |
X_train, X_test, y_train, y_test = train_test_split(X, y_encoded, test_size=0.2, random_state=42)
|
|
|
|
|
|
|
| 28 |
rf_model = RandomForestClassifier(
|
| 29 |
n_estimators=100,
|
| 30 |
random_state=42,
|
| 31 |
max_depth=8
|
| 32 |
)
|
| 33 |
rf_model.fit(X_train, y_train)
|
| 34 |
-
accuracy = rf_model.score(X_test, y_test)
|
| 35 |
-
print(f"Model Accuracy: {accuracy*100:.2f}%")
|
| 36 |
|
|
|
|
|
|
|
|
|
|
| 37 |
|
|
|
|
| 38 |
def predict_earthquake_alert(magnitude, depth, cdi, mmi, sig):
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
|
|
|
|
| 45 |
interface = gr.Interface(
|
| 46 |
fn=predict_earthquake_alert,
|
| 47 |
inputs=[
|
|
@@ -51,11 +64,10 @@ interface = gr.Interface(
|
|
| 51 |
gr.Number(label="MMI"),
|
| 52 |
gr.Number(label="SIG")
|
| 53 |
],
|
| 54 |
-
outputs=gr.Textbox(label="
|
| 55 |
-
title="Earthquake Alert Prediction",
|
| 56 |
-
description="Enter earthquake parameters to predict the alert level using Random Forest Classifier model"
|
| 57 |
)
|
| 58 |
|
| 59 |
-
|
| 60 |
if __name__ == "__main__":
|
| 61 |
interface.launch()
|
|
|
|
| 3 |
from sklearn.model_selection import train_test_split
|
| 4 |
from sklearn.preprocessing import LabelEncoder
|
| 5 |
from sklearn.ensemble import RandomForestClassifier
|
| 6 |
+
import os
|
| 7 |
import gradio as gr
|
| 8 |
|
| 9 |
+
print("π₯ Downloading dataset from KaggleHub...")
|
| 10 |
path = kagglehub.dataset_download("ahmeduzaki/earthquake-alert-prediction-dataset")
|
| 11 |
+
|
| 12 |
+
# β
FIX 1: Typo - endswith (not endswidth)
|
| 13 |
+
csv_files = [f for f in os.listdir(path) if f.endswith(".csv")]
|
| 14 |
+
|
| 15 |
if not csv_files:
|
| 16 |
+
raise FileNotFoundError("β No CSV file found in the downloaded dataset folder")
|
| 17 |
+
|
| 18 |
filepath = os.path.join(path, csv_files[0])
|
| 19 |
+
print(f"β
Using dataset file: {filepath}")
|
| 20 |
|
| 21 |
+
# β
FIX 2: Load the dataset safely
|
| 22 |
data = pd.read_csv(filepath)
|
| 23 |
+
print("β
Dataset loaded successfully")
|
| 24 |
+
print("π Columns:", data.columns.tolist())
|
|
|
|
| 25 |
|
| 26 |
+
# β
FIX 3: y should be a Series, not DataFrame
|
| 27 |
X = data[['magnitude', 'depth', 'cdi', 'mmi', 'sig']]
|
| 28 |
+
y = data['alert']
|
| 29 |
|
| 30 |
+
# Encode labels
|
| 31 |
label_encoder = LabelEncoder()
|
| 32 |
y_encoded = label_encoder.fit_transform(y)
|
| 33 |
+
|
| 34 |
+
# Split data
|
| 35 |
X_train, X_test, y_train, y_test = train_test_split(X, y_encoded, test_size=0.2, random_state=42)
|
| 36 |
+
|
| 37 |
+
# Train model
|
| 38 |
rf_model = RandomForestClassifier(
|
| 39 |
n_estimators=100,
|
| 40 |
random_state=42,
|
| 41 |
max_depth=8
|
| 42 |
)
|
| 43 |
rf_model.fit(X_train, y_train)
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
# Evaluate accuracy
|
| 46 |
+
accuracy = rf_model.score(X_test, y_test)
|
| 47 |
+
print(f"π― Model Accuracy: {accuracy * 100:.2f}%")
|
| 48 |
|
| 49 |
+
# Prediction function
|
| 50 |
def predict_earthquake_alert(magnitude, depth, cdi, mmi, sig):
|
| 51 |
+
user_input = pd.DataFrame([[magnitude, depth, cdi, mmi, sig]],
|
| 52 |
+
columns=['magnitude', 'depth', 'cdi', 'mmi', 'sig'])
|
| 53 |
+
pred_encoded = rf_model.predict(user_input)[0]
|
| 54 |
+
pred_label = label_encoder.inverse_transform([pred_encoded])[0]
|
| 55 |
+
return f"Predicted Earthquake Alert Level: {pred_label}"
|
| 56 |
|
| 57 |
+
# Gradio interface
|
| 58 |
interface = gr.Interface(
|
| 59 |
fn=predict_earthquake_alert,
|
| 60 |
inputs=[
|
|
|
|
| 64 |
gr.Number(label="MMI"),
|
| 65 |
gr.Number(label="SIG")
|
| 66 |
],
|
| 67 |
+
outputs=gr.Textbox(label="Prediction"),
|
| 68 |
+
title="π Earthquake Alert Prediction",
|
| 69 |
+
description="Enter earthquake parameters to predict the alert level using a Random Forest Classifier model."
|
| 70 |
)
|
| 71 |
|
|
|
|
| 72 |
if __name__ == "__main__":
|
| 73 |
interface.launch()
|