import gradio as gr import numpy as np import pandas as pd import matplotlib.pyplot as plt from pycaret.regression import * cvr_saved = load_model('pred_cvr') def predict_cvr(xyz_campaign_id, gender, age, Impressions, Clicks, Total_Conversion, interest): #สร้าง function predict_cvr โดยภายใน function คือ ส่วนของ input data path = "/content/drive/MyDrive/KAG_conversion_data.csv" #Import development ไฟล์ที่เป็น .csv df = pd.read_csv(path) #อ่านไฟล์ csv df.drop(["ad_id", "fb_campaign_id", "Spent","Approved_Conversion"],axis=1, inplace = True) #drop columns ทิ้ง df = pd.DataFrame.from_dict({'xyz_campaign_id': [xyz_campaign_id], 'gender': [gender], 'age': [age], 'Impressions': [Impressions], 'Clicks': [Clicks], 'Total_Conversion': [Total_Conversion], 'interest': [interest]}) #แปลงเป็น dataframe df["xyz_campaign_id"].replace({916:"campaign_a",936:"campaign_b",1178:"campaign_c"}, inplace=True) #แทนที่ด้วยชื่อ campaign pred = cvr_saved.predict(df).tolist()[0] #เมื่่อถูกทำนายแล้ว มันจะส่งกลับคืนค่าเข้าไปใน pred return 'Conversion Rate : '+str(pred) #function predict_cvr จะส่ง output ออกมาเป็น "Conversion Rate : pred" xyz_campaign_id = gr.inputs.Dropdown(['campaign_a', 'campaign_b', 'campaign_c'], label="xyz_campaign_id") gender = gr.inputs.Dropdown(['M', 'F'], label = "gender") age = gr.inputs.Dropdown(['30-34', '35-39', '40-44', '45-49'], label = "age") Impressions = gr.inputs.Slider(minimum=100,maximum=1000000,step=100,label = "Impressions") Clicks = gr.inputs.Slider(minimum=1,maximum=500,step=1, label = "Clicks") Total_Conversion = gr.inputs.Slider(minimum=1,maximum=100,step= 1, label = "Total_Conversion") interest = gr.inputs.Slider(minimum=1,maximum=114,step= 1, label = "interest") gr.Interface(predict_cvr, inputs =[xyz_campaign_id, gender, age, Impressions, Clicks, Total_Conversion, interest], outputs="label", title = "Facebook Ads Conversions Prediction Web App", theme = "dark-peach", capture_session=True).launch(debug=True);