-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
37 lines (28 loc) · 1.18 KB
/
app.py
File metadata and controls
37 lines (28 loc) · 1.18 KB
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
from flask import Flask, request, jsonify
import pickle
app = Flask(__name__)
# Load model Apriori dari file pickle
with open('apriori_market_basket_model.pkl', 'rb') as f:
model_data = pickle.load(f)
association_rules = model_data['association_rules']
@app.route('/predict', methods=['POST'])
def predict():
data = request.get_json()
input_items = data.get('keranjang', [])
rekomendasi = []
for _, rule in association_rules.iterrows():
antecedents = set(rule['antecedents']) # barang yang harus ada di keranjang
consequents = set(rule['consequents']) # barang yang direkomendasikan
# Jika semua antecedents ada di input user
if antecedents.issubset(set(input_items)):
for item in consequents:
# Hindari merekomendasikan barang yang sudah ada di keranjang
if item not in input_items:
rekomendasi.append({
'Nama Barang': item,
'Kuantitas': 1,
'Harga': 0 # Ganti kalau kamu punya info harga
})
return jsonify({'rekomendasi': rekomendasi})
if __name__ == '__main__':
app.run(debug=True)