Skip to content

Commit

Permalink
New interface
Browse files Browse the repository at this point in the history
  • Loading branch information
loop-index committed Oct 17, 2023
1 parent bc9e1aa commit 466de66
Show file tree
Hide file tree
Showing 7 changed files with 287 additions and 38 deletions.
38 changes: 21 additions & 17 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import signal, time, json, re, requests
from flask import Flask, request, jsonify
from flask import Flask, request, jsonify, render_template
from flask_cors import CORS
from public.ipinyou import predictor, bidder
from public import utils, api, db_main
Expand All @@ -24,7 +24,7 @@ def stop_server():

@app.route('/')
def hello_world():
return 'Hello, World!'
return render_template('index.html')

@app.route('/users')
def get_users():
Expand All @@ -48,22 +48,26 @@ def ad_request():
data = request.args
user_tags = data['user_tags'].split(',')
floor_price = data['floor_price']
number = data['number']

advertiserResult = predictor.get_advertiser_for(user_tags, floor_price)
bidResult = bidder.get_bid(user_tags, floor_price, advertiserResult['advertiserID'], advertiserResult['ctr'])

db_main.update_ad_impression(advertiserResult['adID'])
return jsonify({
'advertiserID': advertiserResult['advertiserID'],
'advertiser': advertiserResult['advertiser'],
'campaignID': advertiserResult['campaignID'],
'campaign': advertiserResult['campaign'],
'adID': advertiserResult['adID'],
'adText': advertiserResult['adText'],
'bid': '{:.2f}$'.format(utils.convert_cost(bidResult['bid'])),
'ctr': '{:.2f}%'.format(advertiserResult['ctr']),
'time': '{:.2f}ms'.format((time.time() - start)*1000),
})
advertiserResult = predictor.get_advertiser_for(user_tags, floor_price, int(number))
result = []
for advertiser in advertiserResult:
advertiser['bid'] = bidder.get_bid(user_tags, floor_price, advertiser['advertiserID'], advertiser['ctr'])['bid']
db_main.update_ad_impression(advertiser['adID'])
result.append({
'advertiserID': advertiser['advertiserID'],
'advertiser': advertiser['advertiser'],
'campaignID': advertiser['campaignID'],
'campaign': advertiser['campaign'],
'adID': advertiser['adID'],
'adText': advertiser['adText'],
'bid': '{:.2f}$'.format(utils.convert_cost(advertiser['bid'])),
'ctr': '{:.2f}%'.format(advertiser['ctr']),
'time': '{:.2f}ms'.format((time.time() - start)*1000),
})

return jsonify(result)

# return (utils.ad_html(advertiserResult['advertiser'], advertiserResult['campaign'], advertiserResult['adText']))

Expand Down
Binary file modified public/__pycache__/db_main.cpython-310.pyc
Binary file not shown.
10 changes: 9 additions & 1 deletion public/db_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,18 @@ def get_advertisers_with_active_campaigns():
cursor.execute('SELECT advertiser_id FROM campaign WHERE start_date <= CURDATE() AND end_date >= CURDATE() AND status = \'Active\'')
return cursor.fetchall()

def get_campaign_from_advertiser(advertiser_id):
def get_all_advertisers():
cursor.execute('SELECT advertiser_id, name FROM advertiser')
return cursor.fetchall()

def get_campaign_from_advertiser_active(advertiser_id):
cursor.execute('SELECT campaign_id, name FROM campaign WHERE advertiser_id = %s AND start_date <= CURDATE() AND end_date >= CURDATE() AND status = \'Active\'', (advertiser_id,))
return cursor.fetchall()

def get_campaign_from_advertiser(advertiser_id):
cursor.execute('SELECT campaign_id, name FROM campaign WHERE advertiser_id = %s', (advertiser_id,))
return cursor.fetchall()

def get_min_impression_campaign_from_advertiser(advertiser_id):
cursor.execute('SELECT campaign_id, name FROM campaign WHERE \
impression = (SELECT MIN(impression) FROM campaign) AND advertiser_id = %s AND start_date <= CURDATE() \
Expand Down
Binary file modified public/ipinyou/__pycache__/bidder.cpython-310.pyc
Binary file not shown.
Binary file modified public/ipinyou/__pycache__/predictor.cpython-310.pyc
Binary file not shown.
46 changes: 26 additions & 20 deletions public/ipinyou/predictor.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def load_models():
model, user_columns = load_models()


def get_advertiser_for(user_tags, floor_price):
def get_advertiser_for(user_tags, floor_price, number):
# Create a dataframe with the input data
input_data = pd.DataFrame({'AdvertiserID': 0}, index=[0], dtype='int64')
input_data = input_data.join(pd.DataFrame(
Expand All @@ -81,29 +81,35 @@ def get_advertiser_for(user_tags, floor_price):
# max_advertiser = "Random"

advertisers = [a[0]
for a in db_main.get_advertisers_with_active_campaigns()]
for a in db_main.get_all_advertisers()]
ctr_list = {}

for advertiser in advertisers:
input_data['AdvertiserID'] = advertiser
pred = model.predict(input_data)[0]
ctr_list.setdefault(advertiser, pred)

top_3 = sorted(ctr_list, key=ctr_list.get, reverse=True)[:3]
top_advertiser = random.choice(top_3)

campaigns = db_main.get_campaign_from_advertiser(top_advertiser)
top_campaign = random.choice(campaigns)

ads = db_main.get_ad_from_campaign(top_campaign[0])
ad = ads[0]

return {
'advertiserID': top_advertiser,
'ctr': ctr_list[top_advertiser],
'campaignID': top_campaign[0],
'campaign': top_campaign[1],
'adID': ad[0],
'adText': ad[1],
'advertiser': db_main.get_advertiser_name(top_advertiser),
}
top_n = sorted(ctr_list, key=ctr_list.get, reverse=True)[:number + 2]
top_advertisers = random.sample(top_n, number)

result = []
for top_advertiser in top_advertisers:
campaigns = db_main.get_campaign_from_advertiser(top_advertiser)
top_campaign = random.choice(campaigns)

print(top_advertiser, top_campaign)
ads = db_main.get_ad_from_campaign(top_campaign[0])
print(ads)
ad = ads[0]

result.append({
'advertiserID': top_advertiser,
'ctr': ctr_list[top_advertiser],
'campaignID': top_campaign[0],
'campaign': top_campaign[1],
'adID': ad[0],
'adText': ad[1],
'advertiser': db_main.get_advertiser_name(top_advertiser),
})

return result
231 changes: 231 additions & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>User Attributes Selection</title>

<!-- jQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<!-- bootstrap 5 -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>
</head>
<body>
<div class="container">
<div class="row m-0">
<div class="col-12 col-md-6 col-lg-8">
<h1>User Attributes Selection</h1>
<h3> Gender </h3>
<span>
<input type="checkbox" class="btn-check" id="10110" autocomplete="off" checked>
<label class="btn btn-outline-warning my-1" for="10110">Male</label>

<input type="checkbox" class="btn-check" id="10111" autocomplete="off">
<label class="btn btn-outline-warning my-1" for="10111">Female</label>
</span>

<h3>In-market Interests</h3>
<span>
<input type="checkbox" class="btn-check" id="10684" autocomplete="off" checked>
<label class="btn btn-outline-warning my-1" for="10684">3c Product</label>

<input type="checkbox" class="btn-check" id="11092" autocomplete="off">
<label class="btn btn-outline-warning my-1" for="11092">Appliances</label>

<input type="checkbox" class="btn-check" id="11278" autocomplete="off">
<label class="btn btn-outline-warning my-1" for="11278">Clothing, Shoes & Bags</label>

<input type="checkbox" class="btn-check" id="11379" autocomplete="off">
<label class="btn btn-outline-warning my-1" for="11379">Beauty & Personal Care</label>

<input type="checkbox" class="btn-check" id="11423" autocomplete="off">
<label class="btn btn-outline-warning my-1" for="11423">Household & Home Improvement</label>

<input type="checkbox" class="btn-check" id="11512" autocomplete="off">
<label class="btn btn-outline-warning my-1" for="11512">Infant & Mom Products</label>

<input type="checkbox" class="btn-check" id="11576" autocomplete="off">
<label class="btn btn-outline-warning my-1" for="11576">Sports Item</label>

<input type="checkbox" class="btn-check" id="11632" autocomplete="off">
<label class="btn btn-outline-warning my-1" for="11632">Outdoor</label>

<input type="checkbox" class="btn-check" id="11680" autocomplete="off">
<label class="btn btn-outline-warning my-1" for="11680">Health Care Products</label>

<input type="checkbox" class="btn-check" id="11724" autocomplete="off">
<label class="btn btn-outline-warning my-1" for="11724">Luxury</label>

<input type="checkbox" class="btn-check" id="11944" autocomplete="off">
<label class="btn btn-outline-warning my-1" for="11944">Real Estate</label>

<input type="checkbox" class="btn-check" id="13042" autocomplete="off">
<label class="btn btn-outline-warning my-1" for="13042">Automobile</label>

<input type="checkbox" class="btn-check" id="13403" autocomplete="off">
<label class="btn btn-outline-warning my-1" for="13403">Finance</label>

<input type="checkbox" class="btn-check" id="13496" autocomplete="off">
<label class="btn btn-outline-warning my-1" for="13496">Travel</label>

<input type="checkbox" class="btn-check" id="13678" autocomplete="off">
<label class="btn btn-outline-warning my-1" for="13678">Education</label>

<input type="checkbox" class="btn-check" id="13776" autocomplete="off">
<label class="btn btn-outline-warning my-1" for="13776">Service</label>

<input type="checkbox" class="btn-check" id="13874" autocomplete="off">
<label class="btn btn-outline-warning my-1" for="13874">Electronic Game</label>

<input type="checkbox" class="btn-check" id="16593" autocomplete="off">
<label class="btn btn-outline-warning my-1" for="16593">Book</label>

<input type="checkbox" class="btn-check" id="16617" autocomplete="off">
<label class="btn btn-outline-warning my-1" for="16617">Medicine</label>

<input type="checkbox" class="btn-check" id="16661" autocomplete="off">
<label class="btn btn-outline-warning my-1" for="16661">Food & Drink</label>
</span>

<h3>Long-term Interests</h3>
<span>
<input type="checkbox" class="btn-check" id="10006" autocomplete="off" checked>
<label class="btn btn-outline-warning my-1" for="10006">News</label>

<input type="checkbox" class="btn-check" id="10024" autocomplete="off">
<label class="btn btn-outline-warning my-1" for="10024">Education</label>

<input type="checkbox" class="btn-check" id="10031" autocomplete="off">
<label class="btn btn-outline-warning my-1" for="10031">Automobile</label>

<input type="checkbox" class="btn-check" id="10048" autocomplete="off">
<label class="btn btn-outline-warning my-1" for="10048">Real Estate</label>

<input type="checkbox" class="btn-check" id="10052" autocomplete="off">
<label class="btn btn-outline-warning my-1" for="10052">IT</label>

<input type="checkbox" class="btn-check" id="10057" autocomplete="off">
<label class="btn btn-outline-warning my-1" for="10057">Electronic Game</label>

<input type="checkbox" class="btn-check" id="10059" autocomplete="off">
<label class="btn btn-outline-warning my-1" for="10059">Fashion</label>

<input type="checkbox" class="btn-check" id="10063" autocomplete="off">
<label class="btn btn-outline-warning my-1" for="10063">Entertainment</label>

<input type="checkbox" class="btn-check" id="10067" autocomplete="off">
<label class="btn btn-outline-warning my-1" for="10067">Luxury</label>

<input type="checkbox" class="btn-check" id="10074" autocomplete="off">
<label class="btn btn-outline-warning my-1" for="10074">Home and Lifestyle</label>

<input type="checkbox" class="btn-check" id="10075" autocomplete="off">
<label class="btn btn-outline-warning my-1" for="10075">Health</label>

<input type="checkbox" class="btn-check" id="10076" autocomplete="off">
<label class="btn btn-outline-warning my-1" for="10076">Food</label>

<input type="checkbox" class="btn-check" id="10077" autocomplete="off">
<label class="btn btn-outline-warning my-1" for="10077">Divine</label>

<input type="checkbox" class="btn-check" id="10079" autocomplete="off">
<label class="btn btn-outline-warning my-1" for="10079">Motherhood & Parenting</label>

<input type="checkbox" class="btn-check" id="10083" autocomplete="off">
<label class="btn btn-outline-warning my-1" for="10083">Sports</label>

<input type="checkbox" class="btn-check" id="10093" autocomplete="off">
<label class="btn btn-outline-warning my-1" for="10093">Travel & Outdoors</label>

<input type="checkbox" class="btn-check" id="10102" autocomplete="off">
<label class="btn btn-outline-warning my-1" for="10102">Social</label>

<input type="checkbox" class="btn-check" id="13800" autocomplete="off">
<label class="btn btn-outline-warning my-1" for="13800">Art & Photography & Design</label>

<input type="checkbox" class="btn-check" id="13866" autocomplete="off">
<label class="btn btn-outline-warning my-1" for="13866">Online Literature</label>

<input type="checkbox" class="btn-check" id="14273" autocomplete="off">
<label class="btn btn-outline-warning my-1" for="14273">3c</label>

<input type="checkbox" class="btn-check" id="16706" autocomplete="off">
<label class="btn btn-outline-warning my-1" for="16706">Culture</label>

<input type="checkbox" class="btn-check" id="16751" autocomplete="off">
<label class="btn btn-outline-warning my-1" for="16751">Sex</label>
</span>
</div>
<style>
.overlay {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-image: linear-gradient(to bottom, rgba(255, 255, 255, 0), rgba(0, 0, 0, 0.8) 90%);
z-index: 1;
}

.overlay.card-title {
font-size: 4vw;
line-height: 2.5vw;
}

.overlay.card-text {
font-size: 2vw;
line-height: 2.5vw;
}
</style>
<div class="col-12 col-md-6 col-lg-4">
<div class="ad p-4"></div>
<div class="ad p-4"></div>
</div>
</div>

</div>

<script>

// Update ads
function updateAds() {
let userTags = [];
$(".btn-check:checked").each(function () {
userTags.push($(this).attr("id"));
});

userTags = userTags.join("%2C");
fetch(`/ad_request?user_tags=${userTags}&floor_price=10&number=${$('.ad').length}`,
{
method: 'GET',
headers: {
'Accept': 'application/json',
},
})
.then(response => response.json())
.then(data => {
for (let i = 0; i < data.length; i++){
$('.ad').eq(i).html(`
<div class="card p-0 m-0" style="width: 100%; aspect-ratio: 1/1; overflow: hidden">
<img src="https://cataas.com/cat" class="card-img-top" alt="..." style="width: 100%; height: 100%; object-fit: cover;">
<div class="overlay">
<div class="card-img-overlay d-flex flex-column justify-content-end text-light">
<h1 class="card-title">${data[i].advertiser}</h1>
<p class="card-text">${data[i].adText}</p>
</div>
</div>
</div>`);
}
});
}

// Update ads when page loaded
updateAds();
$(".btn-check").change(function () {
updateAds();
});
</script>

</body>
</html>

0 comments on commit 466de66

Please sign in to comment.