File size: 2,071 Bytes
9902593
f8cd6e3
9902593
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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 gradio as gr
import os


print("Downloading Data from Kaggle...")
path = kagglehub.dataset_download("ahmeduzaki/earthquake-alert-prediction-dataset")
# print("Path to dataset files:", path)
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}")

df = pd.read_csv(filepath)
print("Dataset loaded Succsessfully")
print("Columns: ", df.columns.tolist())

X = df.drop("alert", axis=1)
y = df["alert"]
label_encoder = LabelEncoder()
y_encoded = label_encoder.fit_transform(y)

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

rf_model = RandomForestClassifier(
    n_estimators=100,
    random_state=42,
    max_depth=8
)
rf_model.fit(X_train, y_train)
accouracy = rf_model.score(X_test, y_test)
print(f"Model Accouracy: {accouracy * 100:.2f}%")

def Earthquak_predection(magnitude,depth,cdi,mmi,sig):
  user_data_input = pd.DataFrame([[magnitude, depth, cdi, mmi, sig]], columns=['magnitude','depth','cdi','mmi','sig'])
  pred_encoded = rf_model.predict(user_data_input)[0]
  pred_label = label_encoder.inverse_transform([pred_encoded])[0]

  return pred_label

interface = gr.Interface(
    fn=Earthquak_predection,
    inputs=[
        gr.Number(label="Magnitude"),
        gr.Number(label="Depth"),
        gr.Number(label="Community Intensity (cdi)"),
        gr.Number(label="Modified Mercalli Intensity (mmi)"),
        gr.Number(label="Significance")
    ],
    outputs=gr.Textbox(label="Predicted Earthquake Alert"),
    title="Earthquake Alert Prediction",
    description="Enter the earthquake parameters to predict the alert level using Random Forest Classifier Model."
)


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