Spaces:
Runtime error
Runtime error
Update src/streamlit_app.py
Browse files22 unmodified lines23242526272864 unmodified lines9394959697989951 unmodified lines151152153154155156165 unmodified lines32232332432532632732832933033117 unmodified lines3493503513523533543553563572 unmodified lines36036136236336436536636736836937037137222 unmodified linesimport timeimport jsonimport randomimport numpy as npfrom PIL import Imageimport streamlit as st64 unmodified linesmetadata = load_metadata()# ==========================================================================# Preprocessing Logic# ==========================================================================def preprocess_image(pil_img):"""51 unmodified linesreturn results# ==========================================================================# Streamlit UI Configuration# ==========================================================================165 unmodified lineselse:st.markdown(f'<div class="glass-card"><div class="metric-value">N/A</div><div class="metric-label">Upload class unknown</div></div>', unsafe_allow_html=True)# Display Results Grid (Fixed to prevent layout shift)st.markdown("#### Retrieved Nearest Neighbors")# Create dynamic rows to prevent shaking/layout shiftingfor i in range(0, len(results), 3):cols = st.columns(3)for j in range(3):17 unmodified linesbg_color = "rgba(127, 29, 29, 0.1)"badge_html = f'<span style="background-color: #EF4444; padding: 2px 8px; border-radius: 4px; font-size: 0.8rem; font-weight: 600; color: white;">{res["class_name"]}</span>'# Custom card containercard_content = f"""<div style="background: {bg_color}; border: 1px solid {border_color}; border-radius: 8px; padding: 12px; margin-bottom: 12px; text-align: center;"><div style="color: #38BDF8; font-size: 1.1rem; font-weight: 700; margin-bottom: 8px;">Rank #{res["rank"]}</div><div style="margin-bottom: 10px;">{badge_html}</div><div style="color: #94A3B8; font-size: 0.9rem; margin-bottom: 4px;">Cosine Similarity:</div>2 unmodified lines</div>"""# Render matched image and custom cardif os.path.exists(res["image_path"]):matched_img = Image.open(res["image_path"])st.image(matched_img, use_container_width=True)else:st.error("Image file missing.")st.markdown(card_content, unsafe_allow_html=True)else:22 unmodified lines232425262728293064 unmodified lines959697989910010151 unmodified lines153154155156157158159160161162163164165166167165 unmodified lines33333433533633733833934034134217 unmodified lines3603613623633643653663673683693703713723732 unmodified lines37637737837938038122 unmodified linesimport timeimport jsonimport randomimport base64from io import BytesIOimport numpy as npfrom PIL import Imageimport streamlit as st64 unmodified linesmetadata = load_metadata()# ==========================================================================# Preprocessing & Helper Logic# ==========================================================================def preprocess_image(pil_img):"""51 unmodified linesreturn resultsdef get_image_base64(image_path):"""Converts a local image to base64 string for direct HTML rendering."""if os.path.exists(image_path):img = Image.open(image_path)buffered = BytesIO()img.save(buffered, format="PNG")return base64.b64encode(buffered.getvalue()).decode()return ""# ==========================================================================# Streamlit UI Configuration# ==========================================================================165 unmodified lineselse:st.markdown(f'<div class="glass-card"><div class="metric-value">N/A</div><div class="metric-label">Upload class unknown</div></div>', unsafe_allow_html=True)# Display Results Grid (Fixed completely with Base64 HTML rendering)st.markdown("#### Retrieved Nearest Neighbors")# Create dynamic rowsfor i in range(0, len(results), 3):cols = st.columns(3)for j in range(3):17 unmodified linesbg_color = "rgba(127, 29, 29, 0.1)"badge_html = f'<span style="background-color: #EF4444; padding: 2px 8px; border-radius: 4px; font-size: 0.8rem; font-weight: 600; color: white;">{res["class_name"]}</span>'# Convert local image to base64img_b64 = get_image_base64(res["image_path"])img_html = f'<img src="data:image/png;base64,{img_b64}" style="width: 100%; border-radius: 8px; margin-bottom: 12px; display: block;">' if img_b64 else '<div style="color: #EF4444; padding: 20px; border: 1px solid #EF4444; border-radius: 8px; margin-bottom: 12px;">Image missing</div>'# Custom card container integrating image and text as a single HTML blockcard_content = f"""<div style="background: {bg_color}; border: 1px solid {border_color}; border-radius: 8px; padding: 12px; margin-bottom: 12px; text-align: center; height: 100%;">{img_html}<div style="color: #38BDF8; font-size: 1.1rem; font-weight: 700; margin-bottom: 8px;">Rank #{res["rank"]}</div><div style="margin-bottom: 10px;">{badge_html}</div><div style="color: #94A3B8; font-size: 0.9rem; margin-bottom: 4px;">Cosine Similarity:</div>2 unmodified lines</div>"""st.markdown(card_content, unsafe_allow_html=True)else:
src/streamlit_app.py+21-12