NLPV commited on
Commit
8b8d9a9
·
verified ·
1 Parent(s): 3fc3aba

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -0
app.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ import numpy as np
4
+ import dare
5
+ import time
6
+
7
+ from sklearn.model_selection import train_test_split
8
+ from sklearn.metrics import accuracy_score
9
+
10
+ # Load and prepare data
11
+ data = pd.read_csv("parkinsons.data")
12
+ data.columns = data.columns.str.replace(':', '_')
13
+ X = data.drop(columns=["name", "status"]).values
14
+ y = data["status"].values
15
+
16
+ # Train-test split
17
+ X_train, X_test, y_train, y_test = train_test_split(
18
+ X, y, test_size=0.15, stratify=y, random_state=42
19
+ )
20
+
21
+ # Function to train model, delete a sample, and retrain
22
+ def run_dare_demo(delete_index=25):
23
+ logs = ""
24
+
25
+ # Train initial model
26
+ model = dare.Forest(n_estimators=25, max_depth=3, random_state=42)
27
+ start = time.perf_counter()
28
+ model.fit(X_train, y_train)
29
+ train_time = time.perf_counter() - start
30
+
31
+ y_pred = model.predict(X_test)
32
+ acc_before = accuracy_score(y_test, y_pred)
33
+
34
+ logs += f"✅ Initial training completed in {train_time:.4f} seconds\n"
35
+ logs += f"🎯 Accuracy before unlearning: {acc_before:.4f}\n"
36
+
37
+ # Delete a data point
38
+ try:
39
+ start_del = time.perf_counter()
40
+ model.delete(delete_index)
41
+ delete_time = time.perf_counter() - start_del
42
+ y_pred_after = model.predict(X_test.astype(np.float32))
43
+ acc_after = accuracy_score(y_test, y_pred_after)
44
+
45
+ logs += f"\n🧽 Deleted index {delete_index} in {delete_time:.5f} seconds\n"
46
+ logs += f"🎯 Accuracy after unlearning: {acc_after:.4f}\n"
47
+ except Exception as e:
48
+ logs += f"\n⚠️ Error during unlearning: {str(e)}\n"
49
+
50
+ # Retrain from scratch for comparison
51
+ try:
52
+ X_retrain = np.delete(X_train, delete_index, axis=0)
53
+ y_retrain = np.delete(y_train, delete_index, axis=0)
54
+
55
+ retrain_model = dare.Forest(n_estimators=25, max_depth=3, random_state=42)
56
+ start_retrain = time.perf_counter()
57
+ retrain_model.fit(X_retrain, y_retrain)
58
+ retrain_time = time.perf_counter() - start_retrain
59
+
60
+ logs += f"\n🔁 Retraining completed in {retrain_time:.5f} seconds (without index {delete_index})\n"
61
+ except Exception as e:
62
+ logs += f"\n⚠️ Error during retraining: {str(e)}\n"
63
+
64
+ return logs
65
+
66
+ # Gradio Interface
67
+ iface = gr.Interface(
68
+ fn=run_dare_demo,
69
+ inputs=gr.Slider(0, len(X_train)-1, value=25, step=1, label="Data Point Index to Unlearn"),
70
+ outputs="text",
71
+ title="DaRE: Unlearning Demo on Parkinson's Dataset",
72
+ description="This demo shows how to train a DaRE forest, unlearn a data point, and retrain for comparison using the Parkinson's dataset."
73
+ )
74
+
75
+ if __name__ == "__main__":
76
+ iface.launch()