yoursdvniel commited on
Commit
a588d97
·
verified ·
1 Parent(s): 1cd25b6

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +83 -89
main.py CHANGED
@@ -94,98 +94,92 @@ def marketing_rec():
94
 
95
  return jsonify(str(response['text']))
96
 
 
 
97
  @app.route("/predict_metric", methods=["POST"])
98
  @cross_origin()
99
  def predict_metric():
100
- try:
101
- request_data = request.json
102
- logging.info(f"Received request data: {request_data}")
103
-
104
- user_id = request_data.get("user_id")
105
- interval = request_data.get("interval", 30)
106
- metric_type = request_data.get("metric_type", "Profit") # "Profit" or "Customer Engagement"
107
-
108
- # Check if user_id is provided
109
- if not user_id:
110
- logging.error("User ID is missing in the request")
111
- return jsonify({"error": "User ID is missing"}), 400
112
-
113
- transactions_ref = db.collection("system_users").document(user_id).collection("transactions")
114
- data = []
115
-
116
- # Fetch Income and Expense for Profit calculation
117
- if metric_type == "Profit":
118
- income_query = transactions_ref.where("transactionType", "==", "Income").stream()
119
- expense_query = transactions_ref.where("transactionType", "==", "Expense").stream()
120
-
121
- income_data = {}
122
- expense_data = {}
123
-
124
- for doc in income_query:
125
- transaction = doc.to_dict()
126
- logging.info(f"Processing income transaction: {transaction}")
127
- date_str = transaction["date"]
128
- amount = transaction["amountDue"]
129
- income_data[date_str] = income_data.get(date_str, 0) + amount
130
-
131
- for doc in expense_query:
132
- transaction = doc.to_dict()
133
- logging.info(f"Processing expense transaction: {transaction}")
134
- date_str = transaction["date"]
135
- amount = transaction["amountDue"]
136
- expense_data[date_str] = expense_data.get(date_str, 0) + amount
137
-
138
- for date, income in income_data.items():
139
- expense = expense_data.get(date, 0)
140
- data.append({"date": date, "amountDue": income - expense})
141
-
142
- elif metric_type == "Customer Engagement":
143
- income_query = transactions_ref.where("transactionType", "==", "Income").stream()
144
- engagement_data = {}
145
-
146
- for doc in income_query:
147
- transaction = doc.to_dict()
148
- logging.info(f"Processing engagement transaction: {transaction}")
149
- date_str = transaction["date"]
150
- engagement_data[date_str] = engagement_data.get(date_str, 0) + 1
151
-
152
- for date, count in engagement_data.items():
153
- data.append({"date": date, "amountDue": count})
154
-
155
- # Log final aggregated data before processing
156
- logging.info(f"Aggregated data: {data}")
157
-
158
- # Data processing with Prophet
159
- df = pd.DataFrame(data)
160
- df['date'] = pd.to_datetime(df['date'])
161
- df['date'] = df['date'].dt.tz_localize(None)
162
- df = df.sort_values("date").set_index("date")
163
- df = df.resample("D").sum().reset_index()
164
- df.columns = ["ds", "y"]
165
-
166
- # Check if data is sufficient
167
- if df.shape[0] < 10:
168
- logging.warning("Not enough data to train the Prophet model")
169
- return jsonify({"error": "Not enough data for prediction"}), 400
170
-
171
- # Train Prophet model
172
- model = Prophet(daily_seasonality=True, yearly_seasonality=True)
173
- model.fit(df)
174
-
175
- # Make future predictions
176
- future_dates = model.make_future_dataframe(periods=interval)
177
- forecast = model.predict(future_dates)
178
-
179
- # Prepare and log predictions
180
- forecast_data = forecast[['ds', 'yhat']].tail(interval)
181
- predictions = [{"date": row['ds'].strftime('%Y-%m-%d'), "value": row['yhat']} for _, row in forecast_data.iterrows()]
182
- logging.info(f"Predictions: {predictions}")
183
-
184
- return jsonify({"predictedData": predictions})
185
-
186
- except Exception as e:
187
- logging.error(f"Error in /predict_metric endpoint: {e}")
188
- return jsonify({"error": str(e)}), 500
189
 
190
 
191
 
 
94
 
95
  return jsonify(str(response['text']))
96
 
97
+
98
+ # Profit/Customer Engagement Prediction endpoint
99
  @app.route("/predict_metric", methods=["POST"])
100
  @cross_origin()
101
  def predict_metric():
102
+ request_data = request.json
103
+ user_id = request_data.get("user_id")
104
+ interval = request_data.get("interval", 30)
105
+ metric_type = request_data.get("metric_type", "Profit") # "Profit" or "Customer Engagement"
106
+
107
+ transactions_ref = db.collection("system_users").document(user_id).collection("transactions")
108
+
109
+ data = []
110
+
111
+ if metric_type == "Profit":
112
+ # Fetch both Income and Expense transactions for Profit calculation
113
+ income_query = transactions_ref.where("transactionType", "==", "Income").stream()
114
+ expense_query = transactions_ref.where("transactionType", "==", "Expense").stream()
115
+
116
+ income_data = {}
117
+ expense_data = {}
118
+
119
+ for doc in income_query:
120
+ transaction = doc.to_dict()
121
+ date_str = transaction["date"]
122
+ amount = transaction["amountDue"]
123
+ income_data[date_str] = income_data.get(date_str, 0) + amount
124
+
125
+ for doc in expense_query:
126
+ transaction = doc.to_dict()
127
+ date_str = transaction["date"]
128
+ amount = transaction["amountDue"]
129
+ expense_data[date_str] = expense_data.get(date_str, 0) + amount
130
+
131
+ # Calculate net profit for each date
132
+ for date, income in income_data.items():
133
+ expense = expense_data.get(date, 0)
134
+ data.append({"date": date, "amountDue": income - expense})
135
+
136
+ elif metric_type == "Customer Engagement":
137
+ # Use count of Income transactions per day as Customer Engagement
138
+ income_query = transactions_ref.where("transactionType", "==", "Income").stream()
139
+
140
+ engagement_data = {}
141
+ for doc in income_query:
142
+ transaction = doc.to_dict()
143
+ date_str = transaction["date"]
144
+ engagement_data[date_str] = engagement_data.get(date_str, 0) + 1
145
+
146
+ for date, count in engagement_data.items():
147
+ data.append({"date": date, "amountDue": count})
148
+
149
+ # Create DataFrame from the aggregated data
150
+ df = pd.DataFrame(data)
151
+
152
+ # Ensure 'date' column is datetime
153
+ df['date'] = pd.to_datetime(df['date'])
154
+ df['date'] = df['date'].dt.tz_localize(None)
155
+
156
+ # Set 'date' as index
157
+ df = df.sort_values("date").set_index("date")
158
+
159
+ # Resample daily to ensure regular intervals (fill missing dates)
160
+ df = df.resample("D").sum().reset_index()
161
+
162
+ df.columns = ["ds", "y"] # ds: date, y: target
163
+
164
+ # Check if there's enough data to train the model
165
+ if df.shape[0] < 10:
166
+ return jsonify({"error": "Not enough data for prediction"})
167
+
168
+ # Initialize and fit the Prophet model
169
+ model = Prophet(daily_seasonality=True, yearly_seasonality=True)
170
+ model.fit(df)
171
+
172
+ # DataFrame for future predictions
173
+ future_dates = model.make_future_dataframe(periods=interval)
174
+ forecast = model.predict(future_dates)
175
+
176
+ # Extract the forecast for the requested interval
177
+ forecast_data = forecast[['ds', 'yhat']].tail(interval)
178
+ predictions = [{"date": row['ds'].strftime('%Y-%m-%d'), "value": row['yhat']} for _, row in forecast_data.iterrows()]
179
+
180
+ # Return predictions in JSON format
181
+ return jsonify({"predictedData": predictions})
182
+
 
 
 
 
 
 
 
 
183
 
184
 
185