Spaces:
Sleeping
Sleeping
Initial Commit
Browse files- Dockerfile +12 -0
- Rekomendasi User to Item (CBF)/rekomendasi_deploy.py +1 -1
- app.py +105 -0
- requirements.txt +5 -0
- similarity_matrix.pkl +3 -0
- tfidf_matrix.npz +3 -0
- tfidf_vectorizer.pkl +3 -0
Dockerfile
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.11-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
COPY requirements.txt requirements.txt
|
| 6 |
+
RUN pip install --upgrade pip && pip install -r requirements.txt
|
| 7 |
+
|
| 8 |
+
COPY . .
|
| 9 |
+
|
| 10 |
+
ENV FLASK_APP=app.py
|
| 11 |
+
|
| 12 |
+
CMD ["flask", "run", "--host=0.0.0.0", "--port=7860"]
|
Rekomendasi User to Item (CBF)/rekomendasi_deploy.py
CHANGED
|
@@ -55,4 +55,4 @@ def recommend():
|
|
| 55 |
|
| 56 |
# === Run Server ===
|
| 57 |
if __name__ == "__main__":
|
| 58 |
-
app.run(debug=True)
|
|
|
|
| 55 |
|
| 56 |
# === Run Server ===
|
| 57 |
if __name__ == "__main__":
|
| 58 |
+
app.run(debug=True, host='0.0.0.0', port=5000)
|
app.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import pickle
|
| 5 |
+
from scipy.sparse import load_npz
|
| 6 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
# === Inisialisasi Flask ===
|
| 10 |
+
app = Flask(__name__)
|
| 11 |
+
|
| 12 |
+
# === BASE PATH ===
|
| 13 |
+
base_dir = os.path.dirname(os.path.abspath(__file__))
|
| 14 |
+
|
| 15 |
+
# ======================================
|
| 16 |
+
# === Load Model & Data USER to ITEM ===
|
| 17 |
+
# ======================================
|
| 18 |
+
with open(os.path.join(base_dir, "tfidf_vectorizer.pkl"), "rb") as f:
|
| 19 |
+
tfidf = pickle.load(f)
|
| 20 |
+
|
| 21 |
+
tfidf_matrix = load_npz(os.path.join(base_dir, "tfidf_matrix.npz"))
|
| 22 |
+
df_user = pd.read_csv(os.path.join(base_dir, "Dataset", "data_Processed.csv"))
|
| 23 |
+
df_history = pd.read_csv(os.path.join(base_dir, "Dataset", "userHistory.csv"))
|
| 24 |
+
|
| 25 |
+
# === Fungsi rekomendasi USER to ITEM ===
|
| 26 |
+
def recommend_for_user(user_id, top_n=5):
|
| 27 |
+
user_history = df_history[df_history['userID'] == user_id]['namaWisata'].tolist()
|
| 28 |
+
visited_indices = df_user[df_user['title'].isin(user_history)].index
|
| 29 |
+
|
| 30 |
+
if len(visited_indices) == 0:
|
| 31 |
+
return []
|
| 32 |
+
|
| 33 |
+
user_profile_matrix = tfidf_matrix[visited_indices].mean(axis=0)
|
| 34 |
+
user_profile = np.asarray(user_profile_matrix).reshape(1, -1)
|
| 35 |
+
similarities = cosine_similarity(user_profile, tfidf_matrix).flatten()
|
| 36 |
+
|
| 37 |
+
df_temp = df_user.copy()
|
| 38 |
+
df_temp['similarity'] = similarities
|
| 39 |
+
rekomendasi = df_temp[~df_temp['title'].isin(user_history)]
|
| 40 |
+
rekomendasi = rekomendasi.sort_values(by='similarity', ascending=False)
|
| 41 |
+
|
| 42 |
+
return rekomendasi[['title', 'similarity']].head(top_n).to_dict(orient="records")
|
| 43 |
+
|
| 44 |
+
# === Endpoint USER to ITEM ===
|
| 45 |
+
@app.route("/recommenduti", methods=["GET"])
|
| 46 |
+
def recommend_user_to_item():
|
| 47 |
+
user_id = request.args.get("user_id", type=int)
|
| 48 |
+
|
| 49 |
+
if user_id is None:
|
| 50 |
+
return jsonify({"error": "Parameter user_id diperlukan."}), 400
|
| 51 |
+
|
| 52 |
+
hasil = recommend_for_user(user_id)
|
| 53 |
+
|
| 54 |
+
if not hasil:
|
| 55 |
+
return jsonify({"message": f"Tidak ada data history untuk user ID {user_id}."}), 404
|
| 56 |
+
|
| 57 |
+
return jsonify({"user_id": user_id, "rekomendasi": hasil})
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
# ======================================
|
| 61 |
+
# === Load Model & Data ITEM to ITEM ===
|
| 62 |
+
# ======================================
|
| 63 |
+
df_item = pd.read_csv(os.path.join(base_dir, "Dataset", "data_Processed.csv"))
|
| 64 |
+
|
| 65 |
+
with open(os.path.join(base_dir, "similarity_matrix.pkl"), "rb") as f:
|
| 66 |
+
similarity_matrix = pickle.load(f)
|
| 67 |
+
|
| 68 |
+
# === Fungsi rekomendasi ITEM to ITEM ===
|
| 69 |
+
def rekomendasi_tempat(tempat_id, top_n=5):
|
| 70 |
+
if tempat_id < 0 or tempat_id >= len(df_item):
|
| 71 |
+
return None
|
| 72 |
+
|
| 73 |
+
sim_scores = list(enumerate(similarity_matrix[tempat_id]))
|
| 74 |
+
sim_scores = sorted(sim_scores, key=lambda x: x[1], reverse=True)[1:top_n+1]
|
| 75 |
+
input_title = df_item.iloc[tempat_id]['title']
|
| 76 |
+
rekomendasi = []
|
| 77 |
+
for i, score in sim_scores:
|
| 78 |
+
rekomendasi.append({
|
| 79 |
+
'title': df_item.iloc[i]['title'],
|
| 80 |
+
'index': i,
|
| 81 |
+
'kategori': df_item.iloc[i]['kategori'],
|
| 82 |
+
'similarity': round(score, 3)
|
| 83 |
+
})
|
| 84 |
+
|
| 85 |
+
return {
|
| 86 |
+
"tempat_id": tempat_id,
|
| 87 |
+
"rekomendasi": rekomendasi,
|
| 88 |
+
"input_title": input_title
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
# === Endpoint ITEM to ITEM ===
|
| 92 |
+
@app.route("/recommenditi", methods=["GET"])
|
| 93 |
+
def rekomendasi_api():
|
| 94 |
+
tempat_id = request.args.get("tempat_id", type=int)
|
| 95 |
+
|
| 96 |
+
if tempat_id is None:
|
| 97 |
+
return jsonify({"error": "Parameter 'tempat_id' diperlukan."}), 400
|
| 98 |
+
|
| 99 |
+
hasil = rekomendasi_tempat(tempat_id)
|
| 100 |
+
|
| 101 |
+
if hasil is None:
|
| 102 |
+
return jsonify({"message": f"Tempat '{tempat_id}' tidak ditemukan."}), 404
|
| 103 |
+
|
| 104 |
+
return jsonify(hasil)
|
| 105 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
flask
|
| 2 |
+
pandas
|
| 3 |
+
numpy
|
| 4 |
+
scikit-learn
|
| 5 |
+
scipy
|
similarity_matrix.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ebc5a936cae12829abf36556df1e2b72e0f649a1dfd99eab7cd1f84023dc27bb
|
| 3 |
+
size 148130
|
tfidf_matrix.npz
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:faedb0b4226402f3a7ca92b0a889fd056870b9749bfd38104e5a48f387aa9a66
|
| 3 |
+
size 37767
|
tfidf_vectorizer.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ff55d00fd0f9f1db48e35f1b7f09d677f5c9174bbdb06da49440f4fee87ae947
|
| 3 |
+
size 18864
|