Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,85 @@
|
|
| 1 |
-
#
|
|
|
|
| 2 |
from xgboost import XGBRegressor
|
| 3 |
import pandas as pd
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
from flask import Flask, request, jsonify
|
| 3 |
from xgboost import XGBRegressor
|
| 4 |
import pandas as pd
|
| 5 |
+
import requests
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
app = Flask(__name__)
|
| 9 |
+
|
| 10 |
+
# Load the pre-trained model
|
| 11 |
+
model = XGBRegressor()
|
| 12 |
+
model.load_model('model.json')
|
| 13 |
+
|
| 14 |
+
# Fetch data from Facebook API
|
| 15 |
+
def fetch_data_from_api(query, geo_locations):
|
| 16 |
+
url = f"https://graph.facebook.com/v17.0/act_597540533213624/targetingsearch"
|
| 17 |
+
params = {
|
| 18 |
+
"q": query,
|
| 19 |
+
"geo_locations[countries]": geo_locations,
|
| 20 |
+
"access_token": os.getenv('ACCESS_TOKEN')
|
| 21 |
+
}
|
| 22 |
+
response = requests.get(url, params=params)
|
| 23 |
+
if response.status_code == 200:
|
| 24 |
+
return response.json()
|
| 25 |
+
else:
|
| 26 |
+
raise Exception(f"Failed to fetch data from API. Status code: {response.status_code}")
|
| 27 |
+
|
| 28 |
+
# Generate synthetic metrics
|
| 29 |
+
def generate_synthetic_metrics(data):
|
| 30 |
+
IMPRESSION_RATE = 0.10 # 10% of audience sees the ad
|
| 31 |
+
CTR = 0.05 # 5% of impressions result in clicks
|
| 32 |
+
CONVERSION_RATE = 0.02 # 2% of clicks result in conversions
|
| 33 |
+
CPM = 5 # $5 per 1000 impressions
|
| 34 |
+
REVENUE_PER_CONVERSION = 50 # $50 per conversion
|
| 35 |
+
|
| 36 |
+
data['impressions'] = data['audience_size_lower_bound'] * IMPRESSION_RATE
|
| 37 |
+
data['clicks'] = data['impressions'] * CTR
|
| 38 |
+
data['conversions'] = data['clicks'] * CONVERSION_RATE
|
| 39 |
+
data['ad_spend'] = (data['impressions'] / 1000) * CPM
|
| 40 |
+
data['revenue'] = data['conversions'] * REVENUE_PER_CONVERSION
|
| 41 |
+
data['roi'] = (data['revenue'] - data['ad_spend']) / data['ad_spend']
|
| 42 |
+
|
| 43 |
+
return data
|
| 44 |
+
|
| 45 |
+
@app.route('/predict', methods=['GET'])
|
| 46 |
+
def predict():
|
| 47 |
+
try:
|
| 48 |
+
# Get user input from query parameters
|
| 49 |
+
query = request.args.get('q', default='Fitness') # Default query is 'Fitness'
|
| 50 |
+
geo_locations = request.args.get('geo_locations', default='NG') # Default country is 'NG'
|
| 51 |
+
|
| 52 |
+
# Fetch data from Facebook API
|
| 53 |
+
response_data = fetch_data_from_api(query, geo_locations)
|
| 54 |
+
|
| 55 |
+
# Extract the list of dictionaries from the "data" key
|
| 56 |
+
if "data" in response_data and isinstance(response_data["data"], list):
|
| 57 |
+
data = pd.DataFrame(response_data["data"])
|
| 58 |
+
|
| 59 |
+
# Generate synthetic metrics
|
| 60 |
+
data = generate_synthetic_metrics(data)
|
| 61 |
+
|
| 62 |
+
# Use the first row of the data for prediction
|
| 63 |
+
input_data = data.iloc[0][['ad_spend', 'impressions', 'clicks', 'conversions']]
|
| 64 |
+
|
| 65 |
+
# Predict ROI
|
| 66 |
+
predicted_roi = model.predict([input_data])
|
| 67 |
+
|
| 68 |
+
# Return the prediction
|
| 69 |
+
return jsonify({
|
| 70 |
+
"ad_spend": input_data['ad_spend'],
|
| 71 |
+
"impressions": input_data['impressions'],
|
| 72 |
+
"clicks": input_data['clicks'],
|
| 73 |
+
"conversions": input_data['conversions'],
|
| 74 |
+
"predicted_roi": float(predicted_roi[0]),
|
| 75 |
+
"note": "These are recommendations based on real-world data. Actual results may vary."
|
| 76 |
+
})
|
| 77 |
+
|
| 78 |
+
else:
|
| 79 |
+
return jsonify({"error": "The 'data' key is missing or not a list in the API response."}), 400
|
| 80 |
+
|
| 81 |
+
except Exception as e:
|
| 82 |
+
return jsonify({"error": str(e)}), 500
|
| 83 |
+
|
| 84 |
+
if __name__ == '__main__':
|
| 85 |
+
app.run(host='0.0.0.0', port=7860)
|