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