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()