123ahmed commited on
Commit
eb7e776
·
verified ·
1 Parent(s): 9ff6c1d

Update app.py

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