File size: 2,320 Bytes
eb7e776 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | import pandas as pd
import gradio as gr
import joblib
# تحميل الأكواد المحفوظة
le = joblib.load('le_col.pkl')
std = joblib.load('std_col.pkl')
lg = joblib.load('lg.pkl')
le_col = ['gender','education','region','loyalty_status','purchase_frequency','product_category']
std_col = ['age','income','purchase_amount','promotion_usage','satisfaction_score']
def Prediction_will_purchase_again_Model(a,g,i,e,r,l,p,pp,c,u,s):
try:
input_data = pd.DataFrame({
'age':[a],
'gender':[g],
'income':[i],
'education':[e],
'region':[r],
'loyalty_status':[l],
'purchase_frequency':[p],
'purchase_amount':[pp],
'product_category':[c],
'promotion_usage':[u],
'satisfaction_score':[s]
})
# Label Encoding
for col in le_col:
input_data[col] = le[col].transform(input_data[col])
# Standardization
input_data[std_col] = std.transform(input_data[std_col])
# Predict using probability + threshold
prob = lg.predict_proba(input_data)[:,1][0]
threshold = 0.4 # غيره حسب النتيجة اللي عايزها
if prob >= threshold:
return f'Yes (Prob={prob:.2f})'
else:
return f'No (Prob={prob:.2f})'
except Exception as e:
return str(e)
# Gradio Interface
gr.Interface(
fn=Prediction_will_purchase_again_Model,
inputs=[
gr.Number(label='age'),
gr.Dropdown(['Male','Female'],label='gender'),
gr.Number(label='income'),
gr.Dropdown(['Bachelor', 'Masters', 'HighSchool', 'College'],label='education'),
gr.Dropdown(['East', 'West', 'South', 'North'],label='region'),
gr.Dropdown(['Gold', 'Regular', 'Silver'],label='loyalty_status'),
gr.Dropdown(['frequent', 'rare', 'occasional'],label='purchase_frequency'),
gr.Number(label='purchase_amount'),
gr.Dropdown(['Books', 'Clothing', 'Food', 'Electronics', 'Home', 'Beauty','Health'],label='product_category'),
gr.Number(label='promotion_usage'),
gr.Number(label='satisfaction_score')
],
title='Prediction_will_purchase_again_Model',
outputs=gr.Textbox(label='Prediction')
).launch()
|