|
| 1 | +from firebase_functions import https_fn |
| 2 | +from firebase_admin import firestore |
| 3 | +import google.cloud.firestore |
| 4 | +import json |
| 5 | +import os |
| 6 | + |
| 7 | +@https_fn.on_call() |
| 8 | +def get_combined_prediction(req: https_fn.CallableRequest) -> dict: |
| 9 | + """ |
| 10 | + Get combined prediction from both ChatGPT and Linear Regression models. |
| 11 | + """ |
| 12 | + if not req.auth: |
| 13 | + raise https_fn.HttpsError( |
| 14 | + code=https_fn.FunctionsErrorCode.UNAUTHENTICATED, |
| 15 | + message="User must be authenticated" |
| 16 | + ) |
| 17 | + |
| 18 | + user_id = req.auth.uid |
| 19 | + |
| 20 | + # Get prediction IDs from request |
| 21 | + chatgpt_prediction_id = req.data.get("chatgptPredictionId") |
| 22 | + ml_prediction_id = req.data.get("mlPredictionId") |
| 23 | + |
| 24 | + if not chatgpt_prediction_id or not ml_prediction_id: |
| 25 | + raise https_fn.HttpsError( |
| 26 | + code=https_fn.FunctionsErrorCode.INVALID_ARGUMENT, |
| 27 | + message="Both ChatGPT and ML prediction IDs are required" |
| 28 | + ) |
| 29 | + |
| 30 | + # Get predictions from Firestore |
| 31 | + try: |
| 32 | + db = firestore.client() |
| 33 | + |
| 34 | + # Get ChatGPT prediction |
| 35 | + chatgpt_prediction_ref = db.collection("users").document(user_id).collection("predictions").document(chatgpt_prediction_id) |
| 36 | + chatgpt_prediction_doc = chatgpt_prediction_ref.get() |
| 37 | + |
| 38 | + if not chatgpt_prediction_doc.exists: |
| 39 | + raise https_fn.HttpsError( |
| 40 | + code=https_fn.FunctionsErrorCode.NOT_FOUND, |
| 41 | + message="ChatGPT prediction not found" |
| 42 | + ) |
| 43 | + |
| 44 | + chatgpt_prediction = chatgpt_prediction_doc.to_dict() |
| 45 | + |
| 46 | + # Get ML prediction |
| 47 | + ml_prediction_ref = db.collection("users").document(user_id).collection("ml_predictions").document(ml_prediction_id) |
| 48 | + ml_prediction_doc = ml_prediction_ref.get() |
| 49 | + |
| 50 | + if not ml_prediction_doc.exists: |
| 51 | + raise https_fn.HttpsError( |
| 52 | + code=https_fn.FunctionsErrorCode.NOT_FOUND, |
| 53 | + message="ML prediction not found" |
| 54 | + ) |
| 55 | + |
| 56 | + ml_prediction = ml_prediction_doc.to_dict() |
| 57 | + |
| 58 | + # Combine predictions |
| 59 | + chatgpt_grade = chatgpt_prediction["prediction"]["grade"] |
| 60 | + ml_grade = ml_prediction["prediction"]["grade"] |
| 61 | + |
| 62 | + # Simple average of both predictions |
| 63 | + combined_grade = (float(chatgpt_grade) + float(ml_grade)) / 2 |
| 64 | + |
| 65 | + # Create combined prediction |
| 66 | + combined_prediction = { |
| 67 | + "grade": combined_grade, |
| 68 | + "chatgpt_grade": chatgpt_grade, |
| 69 | + "ml_grade": ml_grade, |
| 70 | + "reasoning": chatgpt_prediction["prediction"]["reasoning"], |
| 71 | + "confidence": "medium" # Default confidence |
| 72 | + } |
| 73 | + |
| 74 | + # Determine confidence based on agreement between models |
| 75 | + grade_difference = abs(float(chatgpt_grade) - float(ml_grade)) |
| 76 | + if grade_difference < 5: |
| 77 | + combined_prediction["confidence"] = "high" |
| 78 | + elif grade_difference > 15: |
| 79 | + combined_prediction["confidence"] = "low" |
| 80 | + |
| 81 | + # Store combined prediction in Firestore |
| 82 | + combined_ref = db.collection("users").document(user_id).collection("combined_predictions").document() |
| 83 | + |
| 84 | + combined_data = { |
| 85 | + "chatgptPrediction": chatgpt_prediction, |
| 86 | + "mlPrediction": ml_prediction, |
| 87 | + "combinedPrediction": combined_prediction, |
| 88 | + "createdAt": firestore.SERVER_TIMESTAMP |
| 89 | + } |
| 90 | + |
| 91 | + combined_ref.set(combined_data) |
| 92 | + |
| 93 | + return { |
| 94 | + "success": True, |
| 95 | + "predictionId": combined_ref.id, |
| 96 | + "prediction": combined_prediction |
| 97 | + } |
| 98 | + |
| 99 | + except Exception as e: |
| 100 | + raise https_fn.HttpsError( |
| 101 | + code=https_fn.FunctionsErrorCode.INTERNAL, |
| 102 | + message=f"Error getting combined prediction: {str(e)}" |
| 103 | + ) |
| 104 | + |
| 105 | +@https_fn.on_call() |
| 106 | +def get_latest_predictions(req: https_fn.CallableRequest) -> dict: |
| 107 | + """ |
| 108 | + Get the latest predictions for a user. |
| 109 | + """ |
| 110 | + if not req.auth: |
| 111 | + raise https_fn.HttpsError( |
| 112 | + code=https_fn.FunctionsErrorCode.UNAUTHENTICATED, |
| 113 | + message="User must be authenticated" |
| 114 | + ) |
| 115 | + |
| 116 | + user_id = req.auth.uid |
| 117 | + |
| 118 | + # Get predictions from Firestore |
| 119 | + try: |
| 120 | + db = firestore.client() |
| 121 | + |
| 122 | + # Get latest ChatGPT prediction |
| 123 | + chatgpt_predictions = db.collection("users").document(user_id).collection("predictions").order_by("createdAt", direction=firestore.Query.DESCENDING).limit(1).stream() |
| 124 | + chatgpt_prediction = None |
| 125 | + for doc in chatgpt_predictions: |
| 126 | + chatgpt_prediction = doc.to_dict() |
| 127 | + chatgpt_prediction["id"] = doc.id |
| 128 | + break |
| 129 | + |
| 130 | + # Get latest ML prediction |
| 131 | + ml_predictions = db.collection("users").document(user_id).collection("ml_predictions").order_by("createdAt", direction=firestore.Query.DESCENDING).limit(1).stream() |
| 132 | + ml_prediction = None |
| 133 | + for doc in ml_predictions: |
| 134 | + ml_prediction = doc.to_dict() |
| 135 | + ml_prediction["id"] = doc.id |
| 136 | + break |
| 137 | + |
| 138 | + # Get latest combined prediction |
| 139 | + combined_predictions = db.collection("users").document(user_id).collection("combined_predictions").order_by("createdAt", direction=firestore.Query.DESCENDING).limit(1).stream() |
| 140 | + combined_prediction = None |
| 141 | + for doc in combined_predictions: |
| 142 | + combined_prediction = doc.to_dict() |
| 143 | + combined_prediction["id"] = doc.id |
| 144 | + break |
| 145 | + |
| 146 | + return { |
| 147 | + "success": True, |
| 148 | + "chatgptPrediction": chatgpt_prediction, |
| 149 | + "mlPrediction": ml_prediction, |
| 150 | + "combinedPrediction": combined_prediction |
| 151 | + } |
| 152 | + |
| 153 | + except Exception as e: |
| 154 | + raise https_fn.HttpsError( |
| 155 | + code=https_fn.FunctionsErrorCode.INTERNAL, |
| 156 | + message=f"Error getting latest predictions: {str(e)}" |
| 157 | + ) |
0 commit comments