File size: 2,337 Bytes
c1bac20
 
 
 
 
d52b0a1
c1bac20
 
d52b0a1
c1bac20
d52b0a1
 
 
 
c1bac20
d52b0a1
 
c1bac20
d52b0a1
c1bac20
d52b0a1
c1bac20
d52b0a1
 
c1bac20
d52b0a1
c1bac20
d52b0a1
c1bac20
d52b0a1
c1bac20
 
d52b0a1
 
c1bac20
d52b0a1
 
c1bac20
 
 
 
 
 
 
d52b0a1
 
 
c1bac20
d52b0a1
c1bac20
d52b0a1
 
 
 
 
c1bac20
d52b0a1
c1bac20
 
 
 
 
 
 
 
 
d52b0a1
 
 
c1bac20
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import kagglehub
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from sklearn.ensemble import RandomForestClassifier
import os
import gradio as gr

print("πŸ“₯ Downloading dataset from KaggleHub...")
path = kagglehub.dataset_download("ahmeduzaki/earthquake-alert-prediction-dataset")

# βœ… FIX 1: Typo - endswith (not endswidth)
csv_files = [f for f in os.listdir(path) if f.endswith(".csv")]

if not csv_files:
    raise FileNotFoundError("❌ No CSV file found in the downloaded dataset folder")

filepath = os.path.join(path, csv_files[0])
print(f"βœ… Using dataset file: {filepath}")

# βœ… FIX 2: Load the dataset safely
data = pd.read_csv(filepath)
print("βœ… Dataset loaded successfully")
print("πŸ“Š Columns:", data.columns.tolist())

# βœ… FIX 3: y should be a Series, not DataFrame
X = data[['magnitude', 'depth', 'cdi', 'mmi', 'sig']]
y = data['alert']

# Encode labels
label_encoder = LabelEncoder()
y_encoded = label_encoder.fit_transform(y)

# Split data
X_train, X_test, y_train, y_test = train_test_split(X, y_encoded, test_size=0.2, random_state=42)

# Train model
rf_model = RandomForestClassifier(
    n_estimators=100,
    random_state=42,
    max_depth=8
)
rf_model.fit(X_train, y_train)

# Evaluate accuracy
accuracy = rf_model.score(X_test, y_test)
print(f"🎯 Model Accuracy: {accuracy * 100:.2f}%")

# Prediction function
def predict_earthquake_alert(magnitude, depth, cdi, mmi, sig):
    user_input = pd.DataFrame([[magnitude, depth, cdi, mmi, sig]], 
                              columns=['magnitude', 'depth', 'cdi', 'mmi', 'sig'])
    pred_encoded = rf_model.predict(user_input)[0]
    pred_label = label_encoder.inverse_transform([pred_encoded])[0]
    return f"Predicted Earthquake Alert Level: {pred_label}"

# Gradio interface
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 a Random Forest Classifier model."
)

if __name__ == "__main__":
    interface.launch()