Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +75 -17
src/streamlit_app.py
CHANGED
|
@@ -3,19 +3,29 @@ Streamlit Dashboard for DLRM Book Recommendation System
|
|
| 3 |
Simple interface for DLRM-based book recommendations
|
| 4 |
"""
|
| 5 |
|
|
|
|
|
|
|
| 6 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
import pandas as pd
|
| 8 |
import numpy as np
|
| 9 |
import torch
|
| 10 |
import pickle
|
| 11 |
-
import os
|
| 12 |
-
import sys
|
| 13 |
from typing import Dict, List, Tuple, Optional
|
| 14 |
import warnings
|
| 15 |
warnings.filterwarnings('ignore')
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
|
| 21 |
# Page configuration
|
|
@@ -26,6 +36,9 @@ st.set_page_config(
|
|
| 26 |
initial_sidebar_state="expanded"
|
| 27 |
)
|
| 28 |
|
|
|
|
|
|
|
|
|
|
| 29 |
# Custom CSS
|
| 30 |
st.markdown("""
|
| 31 |
<style>
|
|
@@ -55,6 +68,15 @@ st.markdown("""
|
|
| 55 |
border: 1px solid #e1e5eb;
|
| 56 |
margin-bottom: 1rem;
|
| 57 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
</style>
|
| 59 |
""", unsafe_allow_html=True)
|
| 60 |
|
|
@@ -136,8 +158,12 @@ def main():
|
|
| 136 |
# Header
|
| 137 |
st.markdown('<h1 class="main-header">π DLRM Book Recommendation System</h1>', unsafe_allow_html=True)
|
| 138 |
st.markdown("### Deep Learning Recommendation Model for Personalized Book Suggestions")
|
| 139 |
-
st.markdown("---")
|
| 140 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
|
| 142 |
|
| 143 |
# Load data
|
|
@@ -158,18 +184,50 @@ def main():
|
|
| 158 |
with st.spinner("Loading DLRM model..."):
|
| 159 |
recommender = load_dlrm_model()
|
| 160 |
|
| 161 |
-
if recommender is None or recommender.model is None:
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
|
| 172 |
-
|
|
|
|
|
|
|
|
|
|
| 173 |
|
| 174 |
# Model info
|
| 175 |
st.sidebar.markdown("---")
|
|
@@ -403,7 +461,7 @@ def main():
|
|
| 403 |
|
| 404 |
# Load training results if available
|
| 405 |
if os.path.exists('dlrm_book_training_results.pkl'):
|
| 406 |
-
with open('dlrm_book_training_results.pkl', 'rb') as f:
|
| 407 |
training_results = pickle.load(f)
|
| 408 |
|
| 409 |
st.subheader("π Training Results")
|
|
|
|
| 3 |
Simple interface for DLRM-based book recommendations
|
| 4 |
"""
|
| 5 |
|
| 6 |
+
import os
|
| 7 |
+
import sys
|
| 8 |
import streamlit as st
|
| 9 |
+
|
| 10 |
+
# Check if CPU_ONLY mode is enabled via command line argument
|
| 11 |
+
if len(sys.argv) > 1 and sys.argv[1] == '--cpu-only':
|
| 12 |
+
os.environ['CPU_ONLY'] = 'true'
|
| 13 |
+
print("π Running in CPU-only mode (CUDA disabled)")
|
| 14 |
+
|
| 15 |
import pandas as pd
|
| 16 |
import numpy as np
|
| 17 |
import torch
|
| 18 |
import pickle
|
|
|
|
|
|
|
| 19 |
from typing import Dict, List, Tuple, Optional
|
| 20 |
import warnings
|
| 21 |
warnings.filterwarnings('ignore')
|
| 22 |
|
| 23 |
+
# Import our DLRM recommender
|
| 24 |
+
try:
|
| 25 |
+
from dlrm_inference import DLRMBookRecommender, load_dlrm_recommender, TORCHREC_AVAILABLE
|
| 26 |
+
except ImportError as e:
|
| 27 |
+
print(f"β οΈ Error importing DLRM recommender: {e}")
|
| 28 |
+
TORCHREC_AVAILABLE = False
|
| 29 |
|
| 30 |
|
| 31 |
# Page configuration
|
|
|
|
| 36 |
initial_sidebar_state="expanded"
|
| 37 |
)
|
| 38 |
|
| 39 |
+
# Check if running in CPU-only mode
|
| 40 |
+
cpu_only_mode = os.environ.get('CPU_ONLY', 'false').lower() == 'true'
|
| 41 |
+
|
| 42 |
# Custom CSS
|
| 43 |
st.markdown("""
|
| 44 |
<style>
|
|
|
|
| 68 |
border: 1px solid #e1e5eb;
|
| 69 |
margin-bottom: 1rem;
|
| 70 |
}
|
| 71 |
+
.cpu-mode-banner {
|
| 72 |
+
background-color: #fff3cd;
|
| 73 |
+
color: #856404;
|
| 74 |
+
padding: 0.75rem;
|
| 75 |
+
border-radius: 0.5rem;
|
| 76 |
+
border-left: 4px solid #ffeeba;
|
| 77 |
+
margin: 1rem 0;
|
| 78 |
+
text-align: center;
|
| 79 |
+
}
|
| 80 |
</style>
|
| 81 |
""", unsafe_allow_html=True)
|
| 82 |
|
|
|
|
| 158 |
# Header
|
| 159 |
st.markdown('<h1 class="main-header">π DLRM Book Recommendation System</h1>', unsafe_allow_html=True)
|
| 160 |
st.markdown("### Deep Learning Recommendation Model for Personalized Book Suggestions")
|
|
|
|
| 161 |
|
| 162 |
+
# CPU Mode Banner (if enabled)
|
| 163 |
+
if cpu_only_mode:
|
| 164 |
+
st.markdown('<div class="cpu-mode-banner">βοΈ Running in CPU-only mode (NVIDIA drivers not required)</div>', unsafe_allow_html=True)
|
| 165 |
+
|
| 166 |
+
st.markdown("---")
|
| 167 |
|
| 168 |
|
| 169 |
# Load data
|
|
|
|
| 184 |
with st.spinner("Loading DLRM model..."):
|
| 185 |
recommender = load_dlrm_model()
|
| 186 |
|
| 187 |
+
if recommender is None or not hasattr(recommender, 'model') or recommender.model is None:
|
| 188 |
+
if cpu_only_mode:
|
| 189 |
+
st.warning("β οΈ DLRM model not available in CPU-only mode")
|
| 190 |
+
st.info("The app will continue with limited functionality")
|
| 191 |
+
|
| 192 |
+
# Show options for browsing books without recommendations
|
| 193 |
+
st.subheader("π Browse Books")
|
| 194 |
+
|
| 195 |
+
# Simple book browser
|
| 196 |
+
search_query = st.text_input("Search for books", placeholder="Enter title, author, or publisher")
|
| 197 |
+
if search_query:
|
| 198 |
+
mask = (
|
| 199 |
+
books_df['Book-Title'].str.contains(search_query, case=False, na=False) |
|
| 200 |
+
books_df['Book-Author'].str.contains(search_query, case=False, na=False) |
|
| 201 |
+
books_df['Publisher'].str.contains(search_query, case=False, na=False)
|
| 202 |
+
)
|
| 203 |
+
results = books_df[mask].head(20)
|
| 204 |
+
|
| 205 |
+
if len(results) > 0:
|
| 206 |
+
st.success(f"Found {len(results)} books matching '{search_query}'")
|
| 207 |
+
for _, book in results.iterrows():
|
| 208 |
+
st.markdown(f"**{book['Book-Title']}** by *{book['Book-Author']}*")
|
| 209 |
+
st.write(f"Published: {book.get('Year-Of-Publication', 'Unknown')} | ISBN: {book['ISBN']}")
|
| 210 |
+
st.markdown("---")
|
| 211 |
+
else:
|
| 212 |
+
st.info(f"No books found matching '{search_query}'")
|
| 213 |
+
|
| 214 |
+
return
|
| 215 |
+
else:
|
| 216 |
+
st.error("β DLRM model not available")
|
| 217 |
+
st.info("Please run the training script first: `python train_dlrm_books.py`")
|
| 218 |
+
|
| 219 |
+
st.markdown("### Available Options:")
|
| 220 |
+
st.markdown("1. **Train DLRM Model**: Run `python train_dlrm_books.py`")
|
| 221 |
+
st.markdown("2. **Prepare Data**: Run `python dlrm_book_recommender.py`")
|
| 222 |
+
st.markdown("3. **Check Files**: Ensure preprocessing files exist")
|
| 223 |
+
st.markdown("4. **Try CPU-only Mode**: Run `streamlit run streamlit_dlrm_app.py -- --cpu-only`")
|
| 224 |
+
|
| 225 |
+
return
|
| 226 |
|
| 227 |
+
if cpu_only_mode:
|
| 228 |
+
st.success("β
DLRM model loaded successfully in CPU-only mode!")
|
| 229 |
+
else:
|
| 230 |
+
st.success("β
DLRM model loaded successfully!")
|
| 231 |
|
| 232 |
# Model info
|
| 233 |
st.sidebar.markdown("---")
|
|
|
|
| 461 |
|
| 462 |
# Load training results if available
|
| 463 |
if os.path.exists('dlrm_book_training_results.pkl'):
|
| 464 |
+
with open('/home/mr-behdadi/PROJECT/ICE/dlrm_book_training_results.pkl', 'rb') as f:
|
| 465 |
training_results = pickle.load(f)
|
| 466 |
|
| 467 |
st.subheader("π Training Results")
|