Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,92 +1,80 @@
|
|
| 1 |
-
import pandas as pd
|
| 2 |
-
import numpy as np
|
| 3 |
-
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 4 |
-
from sklearn.metrics.pairwise import cosine_similarity
|
| 5 |
import gradio as gr
|
|
|
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
# Memuat data saat aplikasi pertama kali dijalankan
|
| 20 |
-
df_books = load_data()
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
def
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
sim_scores = sim_scores[1:6]
|
| 44 |
-
book_indices = [i[0] for i in sim_scores]
|
| 45 |
-
|
| 46 |
-
# 3. UI/UX: INJEKSI HTML & CSS UNTUK TAMPILAN GALERI
|
| 47 |
-
html_content = "<div style='display: flex; gap: 20px; flex-wrap: wrap; justify-content: center; margin-top: 20px;'>"
|
| 48 |
-
for i in book_indices:
|
| 49 |
-
book = df_books.iloc[i]
|
| 50 |
-
html_content += f"""
|
| 51 |
-
<div style='width: 160px; background: white; border: 1px solid #e0e0e0; border-radius: 12px; padding: 15px; text-align: center; box-shadow: 0 4px 6px rgba(0,0,0,0.05); transition: transform 0.2s;'>
|
| 52 |
-
<img src='{book['image_url']}' style='width: 120px; height: 170px; object-fit: cover; border-radius: 6px; margin-bottom: 12px; box-shadow: 0 2px 4px rgba(0,0,0,0.1);' onerror="this.src='https://via.placeholder.com/120x170?text=No+Cover'">
|
| 53 |
-
<h4 style='font-size: 14px; color: #333; margin: 0 0 5px 0; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; height: 38px;' title="{book['original_title']}">{book['original_title']}</h4>
|
| 54 |
-
<p style='font-size: 12px; color: #7f8c8d; margin: 0 0 8px 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;'>{book['authors']}</p>
|
| 55 |
-
<div style='background: #f1f8ff; color: #0366d6; padding: 4px 8px; border-radius: 20px; display: inline-block; font-size: 12px; font-weight: bold;'>
|
| 56 |
-
⭐ {book['average_rating']}
|
| 57 |
-
</div>
|
| 58 |
-
</div>
|
| 59 |
-
"""
|
| 60 |
-
html_content += "</div>"
|
| 61 |
-
return html_content
|
| 62 |
|
| 63 |
-
#
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
#
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
-
#
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
in_book = gr.Dropdown(choices=book_list, value=default_book,
|
| 78 |
-
label="🔍 Ketik atau Pilih Buku Favorit Anda", interactive=True)
|
| 79 |
-
with gr.Column(scale=1):
|
| 80 |
-
btn = gr.Button("✨ Berikan Rekomendasi", variant="primary")
|
| 81 |
-
|
| 82 |
-
gr.Markdown("### 🎯 Rekomendasi Teratas Untuk Anda:")
|
| 83 |
-
out_html = gr.HTML()
|
| 84 |
-
|
| 85 |
-
# Interaksi: Bisa dengan klik tombol, atau langsung ganti dropdown
|
| 86 |
-
btn.click(recommend_books, inputs=in_book, outputs=out_html)
|
| 87 |
-
in_book.change(recommend_books, inputs=in_book, outputs=out_html)
|
| 88 |
-
|
| 89 |
-
app.load(recommend_books, inputs=in_book, outputs=out_html)
|
| 90 |
|
| 91 |
-
#
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
|
| 4 |
+
# ==========================================
|
| 5 |
+
# 1. Dummy Data (Ganti dengan dataset asli)
|
| 6 |
+
# ==========================================
|
| 7 |
+
data = {
|
| 8 |
+
'Judul': ['Bumi Manusia', 'Laskar Pelangi', 'Dune', 'Sapiens', 'Atomic Habits', '1984', 'Filosofi Teras'],
|
| 9 |
+
'Genre': ['Fiksi Sejarah', 'Drama', 'Sci-Fi', 'Sejarah', 'Self-Improvement', 'Sci-Fi', 'Filsafat'],
|
| 10 |
+
'Penulis': ['Pramoedya A. Toer', 'Andrea Hirata', 'Frank Herbert', 'Yuval Noah Harari', 'James Clear', 'George Orwell', 'Henry Manampiring']
|
| 11 |
+
}
|
| 12 |
+
df = pd.DataFrame(data)
|
| 13 |
+
daftar_buku = df['Judul'].tolist()
|
| 14 |
+
daftar_genre = df['Genre'].unique().tolist()
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
# ==========================================
|
| 17 |
+
# 2. Fungsi Logika Sistem Rekomendasi
|
| 18 |
+
# ==========================================
|
| 19 |
+
def rekomen_tab1_nlp(buku_favorit):
|
| 20 |
+
if not buku_favorit:
|
| 21 |
+
return "⚠️ Silakan pilih satu buku terlebih dahulu."
|
| 22 |
+
# TODO: Masukkan model NLP/TF-IDF Anda di sini
|
| 23 |
+
return f"✅ Rekomendasi berbasis NLP untuk pembaca '{buku_favorit}':\n1. [Buku Mirip 1]\n2. [Buku Mirip 2]"
|
| 24 |
|
| 25 |
+
def rekomen_tab2_collab(buku_dibaca):
|
| 26 |
+
if not buku_dibaca or len(buku_dibaca) == 0:
|
| 27 |
+
return "⚠️ Silakan pilih minimal 1 buku."
|
| 28 |
+
# TODO: Masukkan model Collaborative Filtering Anda di sini
|
| 29 |
+
buku_terpilih_str = ", ".join(buku_dibaca)
|
| 30 |
+
return f"✅ Berdasarkan pola pembaca yang menyukai {buku_terpilih_str}:\n1. [Buku Rekomendasi Collab 1]\n2. [Buku Rekomendasi Collab 2]"
|
| 31 |
|
| 32 |
+
def rekomen_tab3_coldstart(genre):
|
| 33 |
+
# TODO: Ganti dengan logika sorting rating tertinggi per genre
|
| 34 |
+
if genre == "Semua Genre":
|
| 35 |
+
return "✅ Top 3 Buku Populer Semua Kategori:\n1. Bumi Manusia\n2. Sapiens\n3. Dune"
|
| 36 |
+
else:
|
| 37 |
+
buku_genre = df[df['Genre'] == genre]['Judul'].tolist()
|
| 38 |
+
hasil = "\n".join([f"{i+1}. {b}" for i, b in enumerate(buku_genre)])
|
| 39 |
+
return f"✅ Top Buku untuk Genre '{genre}':\n{hasil}"
|
| 40 |
|
| 41 |
+
# ==========================================
|
| 42 |
+
# 3. Membangun Antarmuka Gradio
|
| 43 |
+
# ==========================================
|
| 44 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 45 |
+
gr.Markdown("# 📚 Smart Recommender System")
|
| 46 |
+
gr.Markdown("Sistem rekomendasi buku hybrid dengan tiga pendekatan berbeda.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
# TAB 1: NLP / Content-Based
|
| 49 |
+
with gr.Tab("1. Content-Based (NLP)"):
|
| 50 |
+
gr.Markdown("**Pilih 1 buku favorit Anda, sistem akan mencari buku dengan tema/sinopsis serupa.**")
|
| 51 |
+
with gr.Row():
|
| 52 |
+
in_nlp = gr.Dropdown(choices=daftar_buku, label="Buku Favorit")
|
| 53 |
+
btn_nlp = gr.Button("Cari Kemiripan Tema", variant="primary")
|
| 54 |
+
out_nlp = gr.Textbox(label="Hasil Rekomendasi", lines=3)
|
| 55 |
+
btn_nlp.click(fn=rekomen_tab1_nlp, inputs=in_nlp, outputs=out_nlp)
|
| 56 |
|
| 57 |
+
# TAB 2: Collaborative Filtering
|
| 58 |
+
with gr.Tab("2. Collaborative Filtering"):
|
| 59 |
+
gr.Markdown("**Pilih beberapa buku yang pernah Anda baca untuk menemukan apa yang dibaca oleh orang dengan selera seperti Anda.**")
|
| 60 |
+
with gr.Row():
|
| 61 |
+
# multiselect=True memungkinkan pemilihan banyak buku sekaligus
|
| 62 |
+
in_collab = gr.Dropdown(choices=daftar_buku, multiselect=True, label="Buku yang Sudah Dibaca")
|
| 63 |
+
btn_collab = gr.Button("Analisis Pola Pembaca", variant="primary")
|
| 64 |
+
out_collab = gr.Textbox(label="Hasil Rekomendasi", lines=3)
|
| 65 |
+
btn_collab.click(fn=rekomen_tab2_collab, inputs=in_collab, outputs=out_collab)
|
| 66 |
|
| 67 |
+
# TAB 3: Cold Start / Genre
|
| 68 |
+
with gr.Tab("3. Pengguna Baru (Genre)"):
|
| 69 |
+
gr.Markdown("**Belum ada riwayat bacaan? Pilih genre favorit Anda untuk melihat buku paling populer.**")
|
| 70 |
+
with gr.Row():
|
| 71 |
+
in_coldstart = gr.Dropdown(choices=["Semua Genre"] + daftar_genre, value="Semua Genre", label="Genre Favorit")
|
| 72 |
+
btn_coldstart = gr.Button("Tampilkan Buku Populer", variant="primary")
|
| 73 |
+
out_coldstart = gr.Textbox(label="Hasil Rekomendasi", lines=3)
|
| 74 |
+
btn_coldstart.click(fn=rekomen_tab3_coldstart, inputs=in_coldstart, outputs=out_coldstart)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
+
# ==========================================
|
| 77 |
+
# 4. Jalankan Aplikasi
|
| 78 |
+
# ==========================================
|
| 79 |
+
if __name__ == "__main__":
|
| 80 |
+
demo.launch()
|