Fred808 commited on
Commit
da017e7
·
verified ·
1 Parent(s): 9fe5fc2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -64
app.py CHANGED
@@ -1,68 +1,26 @@
1
  # train_model.py
 
2
  import pandas as pd
3
  from sklearn.model_selection import train_test_split
4
- from xgboost import XGBRegressor
5
- from sklearn.metrics import mean_squared_error
6
- import requests
7
- import os
8
-
9
- # Fetch data from Facebook API
10
- def fetch_data_from_api(query, geo_locations):
11
- url = f"https://graph.facebook.com/v17.0/act_597540533213624/targetingsearch"
12
- params = {
13
- "q": query,
14
- "geo_locations[countries]": geo_locations,
15
- "access_token": os.getenv('ACCESS_TOKEN')
16
- }
17
- response = requests.get(url, params=params)
18
- if response.status_code == 200:
19
- return response.json()
20
- else:
21
- raise Exception(f"Failed to fetch data from API. Status code: {response.status_code}")
22
-
23
- # Generate synthetic metrics
24
- def generate_synthetic_metrics(data):
25
- IMPRESSION_RATE = 0.10 # 10% of audience sees the ad
26
- CTR = 0.05 # 5% of impressions result in clicks
27
- CONVERSION_RATE = 0.02 # 2% of clicks result in conversions
28
- CPM = 5 # $5 per 1000 impressions
29
- REVENUE_PER_CONVERSION = 50 # $50 per conversion
30
-
31
- data['impressions'] = data['audience_size_lower_bound'] * IMPRESSION_RATE
32
- data['clicks'] = data['impressions'] * CTR
33
- data['conversions'] = data['clicks'] * CONVERSION_RATE
34
- data['ad_spend'] = (data['impressions'] / 1000) * CPM
35
- data['revenue'] = data['conversions'] * REVENUE_PER_CONVERSION
36
- data['roi'] = (data['revenue'] - data['ad_spend']) / data['ad_spend']
37
-
38
- return data
39
-
40
- # Train and save the model
41
- def train_and_save_model():
42
- # Fetch data
43
- response_data = fetch_data_from_api('Fitness', 'NG')
44
- data = pd.DataFrame(response_data['data'])
45
-
46
- # Generate synthetic metrics
47
- data = generate_synthetic_metrics(data)
48
-
49
- # Features and target
50
- X = data[['ad_spend', 'impressions', 'clicks', 'conversions']]
51
- y = data['roi']
52
-
53
- # Train the model
54
- X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
55
- model = XGBRegressor(n_estimators=100, max_depth=3, n_jobs=-1)
56
- model.fit(X_train, y_train)
57
-
58
- # Evaluate the model
59
- predictions = model.predict(X_test)
60
- mse = mean_squared_error(y_test, predictions)
61
- print(f"Model trained successfully! Mean Squared Error: {mse}")
62
-
63
- # Save the model
64
- model.save_model('model.json')
65
- print("Model saved to 'model.json'.")
66
 
67
- if __name__ == '__main__':
68
- train_and_save_model()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  # train_model.py
2
+ from xgboost import XGBRegressor
3
  import pandas as pd
4
  from sklearn.model_selection import train_test_split
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
+ # Example synthetic data
7
+ data = {
8
+ 'ad_spend': [1000, 2000, 3000, 4000, 5000],
9
+ 'impressions': [50000, 100000, 150000, 200000, 250000],
10
+ 'clicks': [2500, 5000, 7500, 10000, 12500],
11
+ 'conversions': [50, 100, 150, 200, 250],
12
+ 'roi': [1.5, 2.0, 2.5, 3.0, 3.5]
13
+ }
14
+ df = pd.DataFrame(data)
15
+
16
+ # Features and target
17
+ X = df[['ad_spend', 'impressions', 'clicks', 'conversions']]
18
+ y = df['roi']
19
+
20
+ # Train the model
21
+ model = XGBRegressor(n_estimators=100, max_depth=3, n_jobs=-1)
22
+ model.fit(X, y)
23
+
24
+ # Save the model
25
+ model.save_model('model.json')
26
+ print("Model saved to 'model.json'.")