From 466de6677e06688853368b53092c6a658930e6d3 Mon Sep 17 00:00:00 2001 From: duong Date: Tue, 17 Oct 2023 02:56:32 -0400 Subject: [PATCH] New interface --- main.py | 38 +-- public/__pycache__/db_main.cpython-310.pyc | Bin 3939 -> 4280 bytes public/db_main.py | 10 +- .../__pycache__/bidder.cpython-310.pyc | Bin 2664 -> 2664 bytes .../__pycache__/predictor.cpython-310.pyc | Bin 2775 -> 2871 bytes public/ipinyou/predictor.py | 46 ++-- templates/index.html | 231 ++++++++++++++++++ 7 files changed, 287 insertions(+), 38 deletions(-) create mode 100644 templates/index.html diff --git a/main.py b/main.py index 7f1c80c..463c9bf 100644 --- a/main.py +++ b/main.py @@ -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 @@ -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(): @@ -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'])) diff --git a/public/__pycache__/db_main.cpython-310.pyc b/public/__pycache__/db_main.cpython-310.pyc index 72257c440bfee106e0153ad74116cd339f6b71bb..1d3228258a15f8082372ff5d0e29fe56a2750138 100644 GIT binary patch delta 413 zcmaDXw?mOHpO=@50SJ;b^isF*P2`hd+%Qplfv9SVS_?yzXo`A@29Op@VGL%_)ZBRC zJ?rFBW{JuDT%3$rlh{PWbQJOub5j-Eg8Y3I5>v`ji%K$!Q;Q}Sut~`ar>B<0C+6hD zBgBhMfEE?;g9xR`@0n#L+i`MEe!#YgMH#4evOkxkKzw3yNoHB91xSe;keIB%sm^FW z`5~J9RV0dg51BOfCVW05wHsVP2rB43J%5=gxYh)@L)Y9K-bL}-EtEfAqS*^yat ZvIajdquyjQel>0u4i*kJAY|oX0|50uT|)o> delta 257 zcmdm?_*jlFpO=@50SJ`*7pIi)PUMqeTrg340V`uLgQn`n2k%)YvvMjTksOFn-pt4;%E+iR`5=$*WGyZoVGE$>XAU62#>m3N@{fbLNM$l3r|RTBu4hnH z9o*{Nb|Af~AVO_&BeTrrYusFn{4PL#F$<93V&q^dQU_{N*(}Uk#>njnSFHgQl-az2 zPl!>>AEcTMNboW8G4e1LX#$y=qLXj%r${M-)T@99H4vc=A~ZmR=Hx(T$;p)hyo_3t P+Xd9v**MrZSb5k0X|ys! diff --git a/public/db_main.py b/public/db_main.py index 24a2843..06aa50f 100644 --- a/public/db_main.py +++ b/public/db_main.py @@ -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() \ diff --git a/public/ipinyou/__pycache__/bidder.cpython-310.pyc b/public/ipinyou/__pycache__/bidder.cpython-310.pyc index 0b73ce13c171a80481391e43de773d3578a8d4ab..9a418fc680f1b4c130afd4cc822b8acaefa73470 100644 GIT binary patch delta 27 hcmaDM@l2Gsxn delta 27 hcmaDM@fgc5Z(31`ZIPynxKkO4qTcFkSKx!3O(>4fz(rvmcVkeZt5zrowZvDX{;Oq z$DVuv!6E($ocI&mk#gZ*AaP^1fx?bvc0BLRd$#<&_qFe3t(Hyje7Jir?)zW78|6Utqd?g<#b3&YiWbpp!O!IOkhg~5wlS!nLR39V8BjW z+~W3y$_XgkxlZ0ord4`NAFMejjZ)cTQaNCw@Z-uw!#21Lty)OwKnpvsyaq0K3lv4P z3!XH?xsP*yK`8T&#Lr_3_r(8=Yjy zC>GoJ4}y(LCB7#8U0Jax&*PK_Yo9BRFpZ}1pd;?!=E^yqWSIzafm$n;o=uNpD(c7@ z><%TYzA}-D*W&Ozt=*#-2b^FDZ;!;g9sVzhCsEMH|3T0P; v*e}f%2dF-12P}_L9u?xI{0bfI^S9zjeQ59Xp5jE@mt{wmfh-60M|=M_2R^J2 delta 549 zcmY*WJ#Q2-5cS7-*Y@5n2}ckSf(W5VIYg5nkdTgC0nPCPC{}{iYQ4E^#NBRu&LJeP zkoYK5xlM5=Y6@uh3;Y8l8fs{$(=ko}iLvJS&CHuu*q<+dT}$dXmI&g@@Vy!)ACs%W zUE=FMk-#k=A41L&jF3SGSYbsf$T3z_b&hFEEOF0*jaS5?N9d_+silU_k=|0w5=#f@ z@VW{We~V1G;7icF!EL8PEwdfa_u9Y)mMA|I<^AL#5y{k05agQ zp8*1gt&84Cbx%U~I{Z$4P*?B~?zs>A*5>B8G+D0Zr8d>9)U)hGW%jeYFx9-wihR=K z)p+WD@*P&}*Hz(eh>c-->3k{M)Ai(^DDC}kl}+bSI*} z`oMQPjV@KnOxi8k8xU c9!;8;de?oDJ8S-b^|psC4}A|KcPe*(1FbxXO#lD@ diff --git a/public/ipinyou/predictor.py b/public/ipinyou/predictor.py index 0303a75..37f7f9b 100644 --- a/public/ipinyou/predictor.py +++ b/public/ipinyou/predictor.py @@ -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( @@ -81,7 +81,7 @@ 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: @@ -89,21 +89,27 @@ def get_advertiser_for(user_tags, floor_price): 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 diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..ed79af9 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,231 @@ + + + + + + User Attributes Selection + + + + + + + + +
+
+
+

User Attributes Selection

+

Gender

+ + + + + + + + +

In-market Interests

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Long-term Interests

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+
+
+
+
+ +
+ + + + +