File size: 2,458 Bytes
56c0820
 
 
 
 
 
 
 
 
61befe1
 
 
 
 
 
 
 
 
 
56c0820
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61befe1
56c0820
 
 
61befe1
56c0820
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 os
import pandas as pd
import librosa
import numpy as np
import firebase_admin
from firebase_admin import credentials, firestore, storage

# --- Firebase Initialization ---
if not firebase_admin._apps:
    firebase_key = os.getenv("FIREBASE_SERVICE_ACCOUNT_JSON")
    if firebase_key:
        cred = credentials.Certificate(json.loads(firebase_key))
        print("Using Firebase service account from secret.")
    else:
        # Local dev fallback
        cred_path = "firebase/uber-analysis-project-98aae-firebase-adminsdk-fbsvc-e51e006bc3.json"
        cred = credentials.Certificate(cred_path)
        print("Using Firebase service account from local file.")

    firebase_admin.initialize_app(cred)

db = firestore.client()

def pull_ride_data(document_id: str):
    """Fetches lifestyle data from Firestore."""
    try:
        doc_ref = db.collection("rides").document(document_id)
        doc = doc_ref.get()
        if doc.exists:
            return doc.to_dict()
        else:
            print(f"Document {document_id} not found.")
            return {}
    except Exception as e:
        print(f"Error fetching document {document_id}: {e}")
        return {}

def pull_prediction_data(document_id: str):
    """Fetches prediction data from Firestore."""
    try:
        doc_ref = db.collection("predictions").document(document_id)
        doc = doc_ref.get()
        if doc.exists:
            return doc.to_dict()
        else:
            print(f"Prediction {document_id} not found.")
            return {}
    except Exception as e:
        print(f"Error fetching prediction {document_id}: {e}")
        return {}

def push_prediction_result(document_id: str, prediction: int, confidence: float):
    """
    Pushes prediction result to Firestore 'predictions' collection.
    Uses the same document_id as the ride for linking.
    prediction: 1 = Success, 0 = Failure
    confidence: float between 0 and 1
    """
    try:
        # Format prediction as string for readability
        pred_str = "Success" if prediction == 1 else "Failure"

        doc_ref = db.collection("predictions").document(document_id)
        doc_ref.set({
            "prediction": pred_str,
            "confidence": confidence,
        })

        print(f"Prediction saved for {document_id} in 'predictions' collection.")
        return True

    except Exception as e:
        print(f"Error saving prediction for {document_id}: {e}")
        return False