File size: 1,964 Bytes
215f97f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Script to upload trained local Keras model to Firebase Storage and promote it.

Usage:
    python push_to_firebase.py
    python -m app.scripts.push_to_firebase
"""

import os
import sys
import asyncio
from datetime import datetime, timezone

# Ensure project root is in sys.path
BASE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))
if BASE_DIR not in sys.path:
    sys.path.insert(0, BASE_DIR)

sys.stdout.reconfigure(encoding='utf-8')

from app.core.firebase import init_firebase, get_storage_bucket, get_firestore_db
from app.ml.serving.registry import promote_model_version


async def push_to_firebase():
    print("Initializing Firebase...")
    init_firebase()
    
    version = "real-dataset-v10"
    keras_model_path = os.path.join(BASE_DIR, "data", "models", "retvec_cnn_model.keras")
    storage_path = f"models/model_{version}.keras"
    
    print(f"Reading {keras_model_path}...")
    with open(keras_model_path, "rb") as f:
        blob_bytes = f.read()
    
    print("Uploading to Firebase Storage...")
    bucket = get_storage_bucket()
    blob = bucket.blob(storage_path)
    blob.upload_from_string(blob_bytes, content_type="application/octet-stream")
    print("Upload complete!")
    
    print("Creating Firestore document...")
    db = get_firestore_db()
    metrics = {
        "accuracy": 0.9874,
        "note": "Run #10 model trained on 510 real admin docs (AZ + ENG). 98.74% Val Acc, 0% FP rate on safe docs."
    }
    db.collection("models").document(version).set({
        "version": version,
        "storagePath": storage_path,
        "metrics": metrics,
        "status": "candidate",
        "createdAt": datetime.now(timezone.utc),
    })
    
    print(f"Promoting model {version} to ACTIVE...")
    await promote_model_version(version)
    print("Model successfully pushed to Firebase and activated!")


def main():
    asyncio.run(push_to_firebase())


if __name__ == "__main__":
    main()