from utils.layout import render_layout
import streamlit as st
import time
from model.search_script import search_for_recipes
import streamlit.components.v1 as components
def recipe_search_page():
st.markdown("""
## Advanced Recipe Recommendation
This module uses a custom-trained BERT model to semantically search recipes
based on your query of ingredients and tags.
""", unsafe_allow_html=True)
if 'search_system' not in st.session_state:
with st.spinner("Initializing recipe search system"):
st.session_state.search_system = search_for_recipes()
search_system = st.session_state.search_system
if not search_system.is_ready:
st.error("System not ready. Please check data files and try again.")
return
query = st.text_input(
"Search for recipes:",
placeholder="e.g., 'chicken pasta italian', 'vegetarian salad', 'chocolate dessert', 'quick easy' "
)
col1, col2 = st.columns(2)
with col1:
num_results = st.slider("Number of results", 1, 15, 5)
with col2:
min_rating = st.slider("Minimum rating", 1.0, 5.0, 3.0, 0.1)
if st.button("🔍 Search Recipes") and query:
with st.spinner(f"Searching for '{query}'..."):
start = time.time()
print(query, num_results, min_rating)
results = search_system.search_recipes(query, num_results, min_rating)
elapsed = time.time() - start
if results:
st.markdown(f"### Top {len(results)} recipe recommendations for: *'{query}'*")
st.markdown("
{i}. {recipe['name']}
{recipe['minutes']} min | {recipe['n_steps']} steps | {recipe['avg_rating']:.1f}/5.0
({recipe['num_ratings']} ratings)
Match Score: {recipe['similarity_score']:.1%}
(query match)
Tags:
{" ".join([f"{tag}" for tag in recipe['tags']])}
Ingredients:
{', '.join(recipe['ingredients'][:8])}
{'...' if len(recipe['ingredients']) > 8 else ''}
{"
Description:
" + description + "
" if description else ""}
{"
" if steps_html else ""}
"""
with columns[(i-1) % 2]:
components.html(html_code, height=340, scrolling=True)
else:
st.warning(f"No recipes found for '{query}' with a minimum rating of {min_rating}/5.0.")
render_layout(recipe_search_page)