Spaces:
Sleeping
Sleeping
File size: 37,427 Bytes
cdb73a8 | 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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 | import json
import logging
import os
import uuid
import pickle
from datetime import datetime
from typing import Optional
from contextlib import contextmanager
from pathlib import Path
import numpy as np
import pandas as pd
import streamlit as st
import sys
from sentence_transformers import SentenceTransformer
from huggingface_hub import snapshot_download
# Add the project root to the Python path
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../..")))
import src.book_recommender.core.config as config
from src.book_recommender.core.exceptions import (
DataNotFoundError,
)
from src.book_recommender.core.logging_config import (
configure_logging,
)
from src.book_recommender.data.processor import clean_and_prepare_data
from src.book_recommender.ml.clustering import (
cluster_books,
get_cluster_names,
)
from src.book_recommender.ml.embedder import generate_embedding_for_query
from src.book_recommender.ml.explainability import explain_recommendation
from src.book_recommender.ml.feedback import save_feedback
from src.book_recommender.ml.recommender import BookRecommender
from src.book_recommender.utils import get_cover_url_multi_source, load_book_covers_batch
configure_logging(log_file="app.log", log_level=os.getenv("LOG_LEVEL", "INFO"))
logger = logging.getLogger(__name__)
st.set_page_config(
page_title="DeepShelf Legacy Demo", page_icon="📚", layout="wide", initial_sidebar_state="collapsed"
)
@st.cache_resource(show_spinner=False)
def ensure_data_available():
"""
Checks if data exists. If not, downloads from Hugging Face.
Uses st.secrets or env vars for configuration.
"""
if (
config.PROCESSED_DATA_PATH.exists()
and config.EMBEDDINGS_PATH.exists()
and config.CLUSTERS_CACHE_PATH.exists()
):
return
# Try getting ID from env var or Streamlit secrets
dataset_id = os.getenv("HF_DATASET_ID")
if not dataset_id and "HF_DATASET_ID" in st.secrets:
dataset_id = st.secrets["HF_DATASET_ID"]
if not dataset_id:
st.warning("HF_DATASET_ID not found. Using local data generation (slow). Set HF_DATASET_ID to download pre-computed data.")
return
try:
with st.spinner(f"Downloading 100MB+ dataset from Hugging Face ({dataset_id})... this happens only once!"):
config.PROCESSED_DATA_DIR.mkdir(parents=True, exist_ok=True)
snapshot_download(
repo_id=dataset_id,
repo_type="dataset",
local_dir=config.PROCESSED_DATA_DIR,
local_dir_use_symlinks=False,
allow_patterns=["*.parquet", "*.npy", "*.pkl", "*.json"],
token=os.getenv("HF_TOKEN") # Optional for public datasets
)
st.success("Data downloaded successfully!")
except Exception as e:
st.error(f"Failed to download data: {e}")
logger.error(f"Download failed: {e}")
@st.cache_resource(show_spinner=False)
def load_embedding_model() -> SentenceTransformer:
"""
Load and cache the sentence-transformer model.
This ensures the model is loaded only once per session/process.
"""
logger.info(f"Loading embedding model: {config.EMBEDDING_MODEL}")
return SentenceTransformer(config.EMBEDDING_MODEL)
@contextmanager
def custom_spinner(text="Loading..."):
"""
A custom spinner that uses CSS/HTML for a better loading animation.
"""
placeholder = st.empty()
placeholder.markdown(
f"""
<div style="
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
margin: 2rem auto; /* Centered with consistent spacing */
">
<div class="book-loader-small">
<div class="inner">
<div class="left"></div>
<div class="middle"></div>
<div class="right"></div>
</div>
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</div>
<p style="
margin-top: 0.8rem;
font-size: 0.85rem;
color: #6b7280;
font-weight: 500;
font-family: 'Inter', sans-serif;
letter-spacing: 0.3px;
opacity: 0.8;
animation: text-pulse 2s infinite ease-in-out;
">{text}</p>
</div>
<style>
.book-loader-small {{
--color: #667eea;
--duration: 6.8s;
width: 24px; /* Reduced from 32px */
height: 9px; /* Reduced from 12px */
position: relative;
margin: 0;
}}
.book-loader-small .inner {{
width: 24px;
height: 9px;
position: relative;
transform-origin: 1.5px 1.5px; /* Adjusted origin */
transform: rotateZ(-90deg);
animation: book var(--duration) ease infinite;
}}
.book-loader-small .inner .left,
.book-loader-small .inner .right {{
width: 45px; /* Reduced from 60px */
height: 3px; /* Reduced from 4px */
top: 0;
border-radius: 1.5px;
background: var(--color);
position: absolute;
}}
.book-loader-small .inner .left {{
right: 21px; /* Adjusted */
transform-origin: 43.5px 1.5px; /* Adjusted */
transform: rotateZ(90deg);
animation: left var(--duration) ease infinite;
}}
.book-loader-small .inner .right {{
left: 21px; /* Adjusted */
transform-origin: 1.5px 1.5px; /* Adjusted */
transform: rotateZ(-90deg);
animation: right var(--duration) ease infinite;
}}
.book-loader-small .inner .middle {{
width: 24px;
height: 9px;
border: 3px solid var(--color); /* Reduced border */
border-top: 0;
border-radius: 0 0 7px 7px;
transform: translateY(1.5px);
}}
.book-loader-small ul {{
margin: 0;
padding: 0;
list-style: none;
position: absolute;
left: 50%;
top: 0;
}}
.book-loader-small ul li {{
height: 3px; /* Reduced from 4px */
border-radius: 1.5px;
transform-origin: 100% 1.5px; /* Adjusted */
width: 36px; /* Reduced from 48px */
right: 0;
top: -7.5px; /* Adjusted */
position: absolute;
background: var(--color);
transform: rotateZ(0deg) translateX(-13.5px); /* Adjusted */
animation-duration: var(--duration);
animation-timing-function: ease;
animation-iteration-count: infinite;
}}
.book-loader-small ul li:nth-child(1) {{ animation-name: page-1; }}
.book-loader-small ul li:nth-child(2) {{ animation-name: page-2; }}
.book-loader-small ul li:nth-child(3) {{ animation-name: page-3; }}
.book-loader-small ul li:nth-child(4) {{ animation-name: page-4; }}
.book-loader-small ul li:nth-child(5) {{ animation-name: page-5; }}
.book-loader-small ul li:nth-child(6) {{ animation-name: page-6; }}
.book-loader-small ul li:nth-child(7) {{ animation-name: page-7; }}
.book-loader-small ul li:nth-child(8) {{ animation-name: page-8; }}
.book-loader-small ul li:nth-child(9) {{ animation-name: page-9; }}
.book-loader-small ul li:nth-child(10) {{ animation-name: page-10; }}
.book-loader-small ul li:nth-child(11) {{ animation-name: page-11; }}
.book-loader-small ul li:nth-child(12) {{ animation-name: page-12; }}
@keyframes page-1 {{ 4% {{ transform: rotateZ(0deg) translateX(-13.5px); }} 13%, 54% {{ transform: rotateZ(180deg) translateX(-13.5px); }} 63% {{ transform: rotateZ(0deg) translateX(-13.5px); }} }}
@keyframes page-2 {{ 9% {{ transform: rotateZ(0deg) translateX(-13.5px); }} 18%, 59% {{ transform: rotateZ(180deg) translateX(-13.5px); }} 68% {{ transform: rotateZ(0deg) translateX(-13.5px); }} }}
@keyframes page-3 {{ 14% {{ transform: rotateZ(0deg) translateX(-13.5px); }} 23%, 64% {{ transform: rotateZ(180deg) translateX(-13.5px); }} 73% {{ transform: rotateZ(0deg) translateX(-13.5px); }} }}
@keyframes page-4 {{ 19% {{ transform: rotateZ(0deg) translateX(-13.5px); }} 28%, 69% {{ transform: rotateZ(180deg) translateX(-13.5px); }} 78% {{ transform: rotateZ(0deg) translateX(-13.5px); }} }}
@keyframes page-5 {{ 24% {{ transform: rotateZ(0deg) translateX(-13.5px); }} 33%, 74% {{ transform: rotateZ(180deg) translateX(-13.5px); }} 83% {{ transform: rotateZ(0deg) translateX(-13.5px); }} }}
@keyframes page-6 {{ 29% {{ transform: rotateZ(0deg) translateX(-13.5px); }} 38%, 79% {{ transform: rotateZ(180deg) translateX(-13.5px); }} 88% {{ transform: rotateZ(0deg) translateX(-13.5px); }} }}
@keyframes page-7 {{ 34% {{ transform: rotateZ(0deg) translateX(-13.5px); }} 43%, 84% {{ transform: rotateZ(180deg) translateX(-13.5px); }} 93% {{ transform: rotateZ(0deg) translateX(-13.5px); }} }}
@keyframes page-8 {{ 39% {{ transform: rotateZ(0deg) translateX(-13.5px); }} 48%, 89% {{ transform: rotateZ(180deg) translateX(-13.5px); }} 98% {{ transform: rotateZ(0deg) translateX(-13.5px); }} }}
@keyframes page-9 {{ 44% {{ transform: rotateZ(0deg) translateX(-13.5px); }} 53%, 94% {{ transform: rotateZ(180deg) translateX(-13.5px); }} 100% {{ transform: rotateZ(0deg) translateX(-13.5px); }} }}
@keyframes page-10 {{ 49% {{ transform: rotateZ(0deg) translateX(-13.5px); }} 58%, 99% {{ transform: rotateZ(180deg) translateX(-13.5px); }} 100% {{ transform: rotateZ(0deg) translateX(-13.5px); }} }}
@keyframes page-11 {{ 54% {{ transform: rotateZ(0deg) translateX(-13.5px); }} 63% {{ transform: rotateZ(180deg) translateX(-13.5px); }} 100% {{ transform: rotateZ(0deg) translateX(-13.5px); }} }}
@keyframes page-12 {{ 59% {{ transform: rotateZ(0deg) translateX(-13.5px); }} 68% {{ transform: rotateZ(180deg) translateX(-13.5px); }} 100% {{ transform: rotateZ(0deg) translateX(-13.5px); }} }}
@keyframes left {{ 4% {{ transform: rotateZ(90deg); }} 10%, 40% {{ transform: rotateZ(0deg); }} 46%, 54% {{ transform: rotateZ(90deg); }} 60%, 90% {{ transform: rotateZ(0deg); }} 96% {{ transform: rotateZ(90deg); }} }}
@keyframes right {{ 4% {{ transform: rotateZ(-90deg); }} 10%, 40% {{ transform: rotateZ(0deg); }} 46%, 54% {{ transform: rotateZ(-90deg); }} 60%, 90% {{ transform: rotateZ(0deg); }} 96% {{ transform: rotateZ(-90deg); }} }}
@keyframes book {{ 4% {{ transform: rotateZ(-90deg); }} 10%, 40% {{ transform: rotateZ(0deg); transform-origin: 1.5px 1.5px; }} 40.01%, 59.99% {{ transform-origin: 22.5px 1.5px; }} 46%, 54% {{ transform: rotateZ(90deg); }} 60%, 90% {{ transform: rotateZ(0deg); transform-origin: 1.5px 1.5px; }} 96% {{ transform: rotateZ(-90deg); }} }}
@keyframes text-pulse {{ 0%, 100% {{ opacity: 0.5; }} 50% {{ opacity: 1; }} }}
</style>
""",
unsafe_allow_html=True,
)
try:
yield
finally:
placeholder.empty()
st.markdown(
"""
<style>
/* Import Google Fonts */
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');
/* Global Styles */
* {
font-family: 'Inter', sans-serif;
}
/* Main container */
.main {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
padding: 0;
}
/* Content wrapper */
.stApp {
background: transparent;
}
/* Enforce flexbox for Streamlit columns */
[data-testid="column"] {
display: flex !important;
flex-direction: column !important;
}
[data-testid="column"] > div {
flex: 1;
display: flex;
flex-direction: column;
}
/* Header section */
.header-section {
background: rgba(255, 255, 255, 0.95);
backdrop-filter: blur(10px);
padding: 2rem 3rem;
border-radius: 0 0 30px 30px;
box-shadow: 0 10px 40px rgba(0,0,0,0.1);
margin-bottom: 2rem;
}
.main-title {
font-size: 3.5rem;
font-weight: 700;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
margin: 0;
padding: 0;
}
.subtitle {
font-size: 1.3rem;
color: #6b7280;
margin-top: 0.5rem;
}
/* Search section */
.search-container {
background: white;
padding: 2.5rem;
border-radius: 20px;
box-shadow: 0 20px 60px rgba(0,0,0,0.15);
margin: 2rem auto;
max-width: 900px;
}
/* Book Card wrapper and structure */
.book-card-container {
background: white;
border-radius: 15px;
padding: 1.5rem;
box-shadow: 0 4px 20px rgba(0,0,0,0.08);
transition: all 0.3s ease;
display: flex;
flex-direction: column;
height: 100%;
position: relative;
overflow: hidden;
}
.book-card-container:hover {
transform: translateY(-8px);
box-shadow: 0 12px 40px rgba(0,0,0,0.15);
}
/* Book cover with fixed aspect ratio */
.book-cover-wrapper {
position: relative;
width: 100%;
padding-top: 150%; /* 2:3 aspect ratio standard for books */
overflow: hidden;
border-radius: 8px;
margin-bottom: 1rem;
box-shadow: 0 4px 15px rgba(0,0,0,0.1);
background-color: #f3f4f6; /* Placeholder background */
}
.book-cover-wrapper img {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
object-fit: cover; /* Ensures image covers the area without distortion */
object-position: center;
transition: transform 0.3s ease;
}
.book-card-container:hover .book-cover-wrapper img {
transform: scale(1.05);
}
/* Info section */
.book-info {
flex: 1;
display: flex;
flex-direction: column;
}
.similarity-badge {
display: inline-block;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 0.4rem 1rem;
border-radius: 20px;
font-size: 0.75rem;
font-weight: 600;
margin-bottom: 0.8rem;
width: fit-content;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
/* Typography */
.book-title {
font-size: 1.1rem;
font-weight: 700;
color: #1f2937;
margin-bottom: 0.4rem;
line-height: 1.3;
/* Clamp to 2 lines */
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
overflow: hidden;
height: 2.8em;
}
.book-author {
font-size: 0.9rem;
color: #6b7280;
margin-bottom: 0.8rem;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.rating-stars {
color: #fbbf24;
font-size: 0.9rem;
margin-bottom: 0.8rem;
display: flex;
align-items: center;
gap: 0.5rem;
}
.rating-stars strong {
color: #4b5563;
font-weight: 600;
}
.genre-container {
display: flex;
flex-wrap: wrap;
gap: 0.4rem;
margin-bottom: 1rem;
height: 2.2rem; /* Fixed height for consistency */
overflow: hidden;
}
.genre-pill {
background: #f3f4f6;
color: #4b5563;
padding: 0.25rem 0.7rem;
border-radius: 12px;
font-size: 0.7rem;
white-space: nowrap;
}
/* Actions area */
.card-actions {
margin-top: auto;
padding-top: 0.5rem;
border-top: 1px solid #f3f4f6;
display: flex;
flex-direction: column;
gap: 0.5rem;
}
/* Actions pushed to bottom */
.card-actions {
margin-top: auto;
padding-top: 1rem;
display: flex;
flex-direction: column;
gap: 0.5rem;
}
/* Buttons */
.stButton > button {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border: none;
padding: 0.7rem 1.5rem;
border-radius: 25px;
font-size: 1rem;
font-weight: 600;
transition: all 0.3s ease;
box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4);
}
.stButton > button:hover {
transform: translateY(-2px);
box-shadow: 0 6px 20px rgba(102, 126, 234, 0.5);
}
/* Text input */
.stTextArea > div > div > textarea {
border-radius: 15px;
border: 2px solid #e5e7eb;
padding: 1rem;
font-size: 1rem;
transition: border-color 0.3s ease;
}
.stTextArea > div > div > textarea:focus {
border-color: #667eea;
box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}
/* Results section */
.results-header {
text-align: center;
font-size: 2rem;
font-weight: 600;
color: white;
margin: 3rem 0 2rem 0;
text-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
/* Loading states */
.stSpinner > div {
border-color: #667eea !important;
}
/* Info/Warning boxes */
.stAlert {
border-radius: 15px;
border-left: 4px solid #667eea;
}
/* Mobile Responsive */
@media (max-width: 768px) {
.main-title {
font-size: 2rem;
}
.subtitle {
font-size: 1rem;
}
.search-container {
padding: 1.5rem;
}
.book-card {
padding: 1rem;
}
.book-cover-container {
padding-top: 140%;
}
}
</style>
""",
unsafe_allow_html=True,
)
@st.cache_resource(show_spinner=False)
def load_recommender() -> BookRecommender:
"""Load recommender without cluttering UI with status messages."""
files_exist = (
os.path.exists(config.PROCESSED_DATA_PATH)
and os.path.exists(config.EMBEDDINGS_PATH)
and os.path.exists(config.EMBEDDING_METADATA_PATH)
)
model_changed = False
if files_exist:
with open(config.EMBEDDING_METADATA_PATH, "r", encoding="utf-8") as f:
metadata = json.load(f)
if metadata.get("model_name") != config.EMBEDDING_MODEL:
model_changed = True
if files_exist and not model_changed:
book_data = pd.read_parquet(config.PROCESSED_DATA_PATH)
embeddings = np.load(config.EMBEDDINGS_PATH)
else:
from src.book_recommender.ml.embedder import generate_embeddings
if not os.path.exists(config.RAW_DATA_PATH):
raise DataNotFoundError(f"Raw data file not found at: {config.RAW_DATA_PATH}")
book_data = clean_and_prepare_data(str(config.RAW_DATA_PATH), str(config.PROCESSED_DATA_PATH))
embeddings = generate_embeddings(book_data, model_name=config.EMBEDDING_MODEL, show_progress_bar=False)
np.save(config.EMBEDDINGS_PATH, embeddings)
metadata = {"model_name": config.EMBEDDING_MODEL}
with open(config.EMBEDDING_METADATA_PATH, "w", encoding="utf-8") as f:
json.dump(metadata, f)
recommender = BookRecommender(book_data=book_data, embeddings=embeddings)
return recommender
@st.cache_resource(show_spinner=False)
def load_cluster_data() -> tuple[np.ndarray, dict, pd.DataFrame]:
"""
Generates and returns cached book clusters, cluster names, and book data with cluster IDs.
Tries to load from disk first, then falls back to computation and saves to disk.
"""
logger.info("Generating/Loading cluster data for Streamlit app...")
recommender = load_recommender()
book_data_df = recommender.book_data.copy()
# Try loading from disk
if os.path.exists(config.CLUSTERS_CACHE_PATH):
try:
logger.info(f"Loading cached clusters from {config.CLUSTERS_CACHE_PATH}")
with open(config.CLUSTERS_CACHE_PATH, "rb") as f:
cached_data = pickle.load(f)
clusters_arr = cached_data["clusters_arr"]
names = cached_data["names"]
# Verify consistency
if len(clusters_arr) == len(book_data_df):
book_data_df["cluster_id"] = clusters_arr
logger.info("Cluster data loaded from disk cache.")
return clusters_arr, names, book_data_df
else:
logger.warning("Cached cluster data size mismatch. Recomputing...")
except Exception as e:
logger.error(f"Failed to load cluster cache: {e}. Recomputing...")
# Recompute if not found or failed
embeddings_arr = recommender.embeddings
clusters_arr, _ = cluster_books(embeddings_arr, n_clusters=config.NUM_CLUSTERS)
book_data_df["cluster_id"] = clusters_arr
names = get_cluster_names(book_data_df, clusters_arr)
# Save to disk
try:
with open(config.CLUSTERS_CACHE_PATH, "wb") as f:
pickle.dump({"clusters_arr": clusters_arr, "names": names}, f)
logger.info(f"Cluster data saved to {config.CLUSTERS_CACHE_PATH}")
except Exception as e:
logger.error(f"Failed to save cluster cache: {e}")
logger.info("Cluster data generated/loaded and cached for Streamlit app.")
return clusters_arr, names, book_data_df
def render_header():
"""Modern header with gradient background."""
st.markdown(
"""
<div class="header-section">
<h1 class="main-title">📚 DeepShelf Legacy Demo</h1>
<p class="subtitle">Explore the power of semantic search</p>
</div>
""",
unsafe_allow_html=True,
)
def render_search_section():
"""Enhanced search interface with examples."""
st.markdown('<div class="search-container">', unsafe_allow_html=True)
st.markdown("### What kind of book are you looking for?")
col1, col2, col3, col4, col5 = st.columns([1, 2, 2, 2, 1])
with col2:
if st.button("Fantasy", key="example1", width="stretch"):
st.session_state.query = "A fantasy adventure with magic and dragons"
with col3:
if st.button("Mystery", key="example2", width="stretch"):
st.session_state.query = "A psychological thriller with unexpected twists"
with col4:
if st.button("Romance", key="example3", width="stretch"):
st.session_state.query = "A heartwarming romance set in a small town"
# Main search input
query = st.text_area(
"Describe the book you want",
value=st.session_state.get("query", ""),
height=100,
placeholder=(
"Example: A science fiction novel about time travel with " "complex characters and philosophical themes..."
),
label_visibility="collapsed",
)
col1, col2, col3 = st.columns([1, 2, 1])
with col2:
search_button = st.button("Find My Perfect Books", type="primary", width="stretch")
st.markdown("</div>", unsafe_allow_html=True)
return query, search_button
def render_book_card(rec, index, cover_url, query_text: Optional[str] = None):
"""Render a beautiful book card with all details."""
# Generate stars string
rating_html = '<div class="rating-stars"> </div>'
if rec.get("rating") and rec.get("rating") != "N/A" and pd.notna(rec["rating"]):
try:
rating = float(rec["rating"])
stars = "⭐" * int(rating)
rating_html = f'<div class="rating-stars">{stars} <strong>{rating:.1f}/5</strong></div>'
except Exception:
pass
# Generate genres HTML
genres_html = '<div class="genre-container"> </div>'
if rec.get("genres") and isinstance(rec.get("genres"), str):
genres = [g.strip() for g in rec["genres"].split(",")[:3]]
pills = "".join([f'<span class="genre-pill">{g.title()}</span>' for g in genres])
genres_html = f'<div class="genre-container">{pills}</div>'
# Generate match badge
match_html = ""
if "similarity" in rec:
similarity_percent = rec["similarity"] * 100
match_html = f'<div class="similarity-badge">{similarity_percent:.0f}% Match</div>'
# Single HTML block for the card content
card_html = f"""
<div class="book-card-container">
<div class="book-cover-wrapper">
<img src="{cover_url}" alt="{rec['title']}">
</div>
<div class="book-info">
{match_html}
<div class="book-title" title="{rec['title']}">{rec['title']}</div>
<div class="book-author">by {rec.get("authors", "Unknown Author")}</div>
{rating_html}
{genres_html}
</div>
</div>
"""
st.markdown(card_html, unsafe_allow_html=True)
# Interactive elements (outside the HTML block)
with st.container():
if rec.get("description"):
with st.expander("Read More"):
desc = rec["description"][:250] + "..." if len(rec["description"]) > 250 else rec["description"]
st.write(desc)
if query_text and "similarity" in rec:
with st.expander("Why this recommendation?"):
from src.book_recommender.ml.explainability import (
explain_recommendation,
)
explanation = explain_recommendation(
query_text=query_text, recommended_book=rec, similarity_score=rec["similarity"]
)
st.info(explanation["summary"])
if explanation.get("matching_features"):
st.markdown("**Matching features:**")
for feature in explanation["matching_features"]:
st.markdown(f"• {feature}")
st.markdown('<div class="card-actions">', unsafe_allow_html=True)
if query_text:
col_a, col_b, col_c = st.columns([1, 1, 3])
with col_a:
if st.button("👍", key=f"like_{index}", help="Good recommendation", use_container_width=True):
save_feedback(query_text, rec, "positive", st.session_state.session_id)
st.toast(f"Feedback saved!", icon="👍")
with col_b:
if st.button("👎", key=f"dislike_{index}", help="Not relevant", use_container_width=True):
save_feedback(query_text, rec, "negative", st.session_state.session_id)
st.toast(f"Feedback saved!", icon="👎")
with col_c:
if st.button("View Details", key=f"details_{index}", use_container_width=True):
show_book_details(rec, query_text=query_text)
else:
if st.button("View Details", key=f"details_{index}", use_container_width=True):
show_book_details(rec, query_text=query_text)
st.markdown('</div>', unsafe_allow_html=True)
@st.dialog("Book Details")
def show_book_details(book, query_text: Optional[str] = None):
col1, col2 = st.columns([1, 2])
with col1:
st.image(get_cover_url_multi_source(book["title"], book.get("authors", "")))
with col2:
st.markdown(f"## {book['title']}")
st.markdown(f"**by {book.get('authors', 'Unknown Author')}**")
if query_text and "similarity" in book:
explanation = explain_recommendation(
query_text=query_text, recommended_book=book, similarity_score=book["similarity"]
)
st.markdown(f"**Match Score:** {explanation['match_score']}% (Confidence: {explanation['confidence']})")
st.write(explanation["summary"])
st.divider()
similarity_percent = book["similarity"] * 100
st.markdown(f"**{similarity_percent:.0f}% Match**")
if book.get("rating") and pd.notna(book["rating"]):
try:
rating = float(book["rating"])
stars = "⭐" * int(rating)
st.markdown(f"{stars} **{rating:.1f}/5**")
except Exception:
pass
if book.get("genres"):
st.markdown(f"**Genres:** {book['genres']}")
st.divider()
if book.get("description"):
st.markdown("### Description")
st.write(book["description"])
st.divider()
col_a, col_b = st.columns(2)
with col_a:
st.link_button(
"Search on Google", f"https://www.google.com/search?q={book['title']}+{book.get('authors', '')}"
)
with col_b:
st.link_button("View on Goodreads", f"https://www.goodreads.com/search?q={book['title']}")
def render_sidebar(recommender):
"""Renders the sidebar with filters and search history."""
with st.sidebar:
st.markdown("## Recent Searches")
if not st.session_state.search_history:
st.info("Your recent searches will appear here.")
else:
for item in reversed(st.session_state.search_history):
if st.button(f"'{item['query'][:20]}...' ({item['results_count']} found)", key=item["timestamp"]):
st.session_state.query = item["query"]
st.rerun()
st.markdown("---")
st.markdown("## Filters")
if "recommendations" in st.session_state and st.session_state.recommendations:
st.session_state.min_rating = st.slider(
"Minimum Rating", 0.0, 5.0, st.session_state.get("min_rating", 0.0), 0.5
)
all_genres = set()
for rec in st.session_state.recommendations:
if rec.get("genres"):
genres = [g.strip() for g in rec["genres"].split(",")]
all_genres.update(genres)
st.session_state.selected_genres = st.multiselect(
"Genres", sorted(list(all_genres)), default=st.session_state.get("selected_genres", [])
)
else:
st.info("Perform a search to see filters.")
def main():
"""Main application with UI."""
# Ensure data is present before doing anything else
ensure_data_available()
render_header()
if "query" not in st.session_state:
st.session_state.query = ""
if "search_history" not in st.session_state:
st.session_state.search_history = []
if "recommendations" not in st.session_state:
st.session_state.recommendations = []
if "session_id" not in st.session_state:
st.session_state.session_id = str(uuid.uuid4())
try:
recommender = load_recommender()
render_sidebar(recommender)
tab1, tab2 = st.tabs(["Search", "Browse Collections"])
with tab1:
query, search_button = render_search_section()
if (search_button and query.strip()) or (query and query != st.session_state.get("last_query", "")):
st.session_state.last_query = query
with custom_spinner("Finding the perfect books for you..."):
# Use the cached model instance
embedding_model = load_embedding_model()
query_embedding = generate_embedding_for_query(query, model=embedding_model)
st.session_state.recommendations = recommender.get_recommendations_from_vector(
query_embedding,
top_k=10,
similarity_threshold=0.25,
)
if st.session_state.recommendations:
st.session_state.search_history.append(
{
"query": query,
"timestamp": datetime.now(),
"results_count": len(st.session_state.recommendations),
}
)
st.session_state.search_history = st.session_state.search_history[-5:]
st.rerun()
else:
st.warning(
"No books found matching your description. Try being more specific or use different keywords!"
)
st.session_state.recommendations = []
elif search_button:
st.warning("Please describe what kind of book you're looking for!")
if st.session_state.recommendations:
filtered = st.session_state.recommendations
min_rating = st.session_state.get("min_rating", 0.0)
selected_genres = st.session_state.get("selected_genres", [])
if min_rating > 0.0:
filtered = [
rec
for rec in filtered
if rec.get("rating") and pd.notna(rec["rating"]) and float(rec.get("rating", 0)) >= min_rating
]
if selected_genres:
filtered = [
rec
for rec in filtered
if rec.get("genres") and any(g in rec.get("genres", "") for g in selected_genres)
]
st.markdown(
f'<div class="results-header">Found {len(filtered)} Perfect Books For You</div>',
unsafe_allow_html=True,
)
if not filtered:
st.info("No books match your current filters. Try adjusting them!")
else:
visible_recs = filtered[:12]
with custom_spinner("Loading book covers..."):
covers_dict = load_book_covers_batch(visible_recs)
for row_idx in range(0, min(len(filtered), 12), 4):
cols = st.columns(4, gap="medium")
for col_idx, rec in enumerate(filtered[row_idx : row_idx + 4]):
with cols[col_idx]:
unique_idx = row_idx + col_idx
cover_url = covers_dict.get(rec["title"], config.FALLBACK_COVER_URL)
render_book_card(rec, unique_idx, cover_url, query_text=query)
with tab2:
st.markdown('<div class="search-container">', unsafe_allow_html=True)
st.markdown("### Browse Books by Collection")
clusters_arr, cluster_names, book_data_with_clusters = load_cluster_data()
cluster_options = [
f"{name} ({np.sum(clusters_arr == cluster_id)} books)" for cluster_id, name in cluster_names.items()
]
selected_cluster_option = st.selectbox("Select a Collection", options=cluster_options, index=0)
selected_cluster_id = int(list(cluster_names.keys())[cluster_options.index(selected_cluster_option)])
cluster_books_df = book_data_with_clusters[book_data_with_clusters["cluster_id"] == selected_cluster_id]
st.markdown(
f'<div class="results-header">Books in {cluster_names[selected_cluster_id]}</div>',
unsafe_allow_html=True,
)
if not cluster_books_df.empty:
cluster_recs = cluster_books_df.to_dict(orient="records")
visible_recs = cluster_recs[:12]
with custom_spinner("Loading book covers..."):
covers_dict = load_book_covers_batch(visible_recs)
for row_idx in range(0, min(len(cluster_recs), 12), 4):
cols = st.columns(4, gap="medium")
for col_idx, rec in enumerate(cluster_recs[row_idx : row_idx + 4]):
with cols[col_idx]:
unique_idx = f"cluster_{selected_cluster_id}_{row_idx + col_idx}"
cover_url = covers_dict.get(rec["title"], config.FALLBACK_COVER_URL)
render_book_card({**rec, "similarity": 1.0}, unique_idx, cover_url)
else:
st.info("No books found in this collection.")
st.markdown("</div>", unsafe_allow_html=True)
except Exception as e:
st.error(f"Something went wrong: {str(e)}")
logger.error(f"Application error: {e}", exc_info=True)
if __name__ == "__main__":
main()
|