Repository navigation
Expand file tree
/
Copy pathgenerate_model.py
More file actions
72 lines (57 loc) · 2.51 KB
/
Copy pathgenerate_model.py
File metadata and controls
72 lines (57 loc) · 2.51 KB
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
import tensorflow as tf
import numpy as np
import os
# ==========================================
# Digital Will - Inactivity Model Generator
# ==========================================
# This script generates a custom TFLite model for the Digital Will App.
# Input: 18 features (User activity metrics)
# Output: 3 classes (Active, Suspicious, Deceased)
def generate_model():
print("🚀 Generating Synthetic Data...")
# generate 1000 samples
samples = 1000
# Feature inputs (randomized for synthetic training)
# 0-4: Days since activity (Post, Like, Comment, DM, AppOpen)
# 5-9: Averages (Posts30, Posts90, Likes30, Likes90, PostVariance)
# 10-14: Interaction (ActiveHours, FollowerRate, DmResponse, Sessions, SessionDuration)
# 15-17: consistency (Battery, Location, Time)
X = np.random.rand(samples, 18).astype(np.float32)
y = np.zeros((samples, 3)).astype(np.float32)
for i in range(samples):
# Logic to determine labels based on "days since app open" (index 4)
days_inactive = X[i][4] * 200 # scale to 0-200 days
if days_inactive < 30:
y[i] = [1.0, 0.0, 0.0] # Active
elif days_inactive < 90:
y[i] = [0.2, 0.8, 0.0] # Suspicious
else:
y[i] = [0.0, 0.1, 0.9] # Deceased/Inactive
print("🧠 Building Model...")
model = tf.keras.Sequential([
tf.keras.layers.Dense(32, activation='relu', input_shape=(18,)),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(16, activation='relu'),
tf.keras.layers.Dense(3, activation='softmax') # 3 classes
])
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
print("🏋️ Training Model...")
model.fit(X, y, epochs=10, batch_size=32, verbose=0)
print("💾 Converting to TFLite...")
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
# Ensure directory exists
output_dir = "assets/ml_models"
if not os.path.exists(output_dir):
os.makedirs(output_dir)
output_path = f"{output_dir}/inactivity_model.tflite"
with open(output_path, "wb") as f:
f.write(tflite_model)
print(f"✅ SUCCESS! Model saved to: {output_path}")
print("You can now run the app with full AI features.")
if __name__ == "__main__":
try:
generate_model()
except Exception as e:
print(f"❌ Error: {e}")
print("Please ensure tensorflow is installed: pip install tensorflow")