Update app.py
Browse files10 unmodified lines11121314151617114 unmodified lines13213313413513613713821 unmodified lines160161162163164165166167112 unmodified lines2802812822832842852862872882892901 unmodified line29229329429529629729820 unmodified lines3193203213223233243252 unmodified lines32832933033133233333410 unmodified lines# ---------------------------------------------------------------------------st.set_page_config(layout="wide", page_title="Garment Extractor Pro", page_icon="✂️", initial_sidebar_state="expanded")# Initialize state at the very top to prevent runtime routing crashesif "fringe_val" not in st.session_state:st.session_state.fringe_val = 0114 unmodified linesMAX_MASK_EDGE = 1024# MEMORY FIX: max_entries=1 drops the old neural network before loading a new one.@st.cache_resource(show_spinner=False, max_entries=1)def load_ai_session(model_name: str):import onnxruntime as ort21 unmodified linesif model_name == "u2net_cloth_seg":rgb_mask = raw_mask.convert("RGB")r, g, b = rgb_mask.split()# Red = Upper Clothes, Green = Lower Clothes. Drops the Blue skin channel.clothes_only = np.maximum(np.array(r), np.array(g))mask = Image.fromarray(clothes_only).convert("L")else:112 unmodified linesst.markdown("<p style='font-size: 0.9rem; font-weight: 500; margin-bottom: 0;'>Fringe Eraser (px)</p>", unsafe_allow_html=True)col_min, col_slide, col_plus = st.columns([1, 4, 1])with col_min:st.button("−", on_click=step_fringe, args=(-1,), use_container_width=True, key="btn_minus")with col_slide:grow_px = st.slider("Fringe", 0, 5, key="fringe_val", label_visibility="collapsed")with col_plus:st.button("+", on_click=step_fringe, args=(1,), use_container_width=True, key="btn_plus")# 3. Process & Render Result1 unmodified linewith col1:st.markdown('<div class="image-card-title">Source Image</div>', unsafe_allow_html=True)st.image(original_image, use_container_width=True)with col2:st.markdown('<div class="image-card-title">Extraction Result</div>', unsafe_allow_html=True)20 unmodified linesextracted_image = chroma_img.copy()extracted_image.putalpha(Image.fromarray(combined_alpha))st.image(extracted_image, use_container_width=True)buf = io.BytesIO()extracted_image.save(buf, format="PNG")2 unmodified linesdata=buf.getvalue(),file_name="product_asset.png",mime="image/png",use_container_width=True)except ModuleNotFoundError as exc:10 unmodified lines111213141516114 unmodified lines13113213313413513621 unmodified lines158159160161162163112 unmodified lines2762772782792802812822832842852861 unmodified line28828929029129229329420 unmodified lines3153163173183193203212 unmodified lines32432532632732832933010 unmodified lines# ---------------------------------------------------------------------------st.set_page_config(layout="wide", page_title="Garment Extractor Pro", page_icon="✂️", initial_sidebar_state="expanded")if "fringe_val" not in st.session_state:st.session_state.fringe_val = 0114 unmodified linesMAX_MASK_EDGE = 1024@st.cache_resource(show_spinner=False, max_entries=1)def load_ai_session(model_name: str):import onnxruntime as ort21 unmodified linesif model_name == "u2net_cloth_seg":rgb_mask = raw_mask.convert("RGB")r, g, b = rgb_mask.split()clothes_only = np.maximum(np.array(r), np.array(g))mask = Image.fromarray(clothes_only).convert("L")else:112 unmodified linesst.markdown("<p style='font-size: 0.9rem; font-weight: 500; margin-bottom: 0;'>Fringe Eraser (px)</p>", unsafe_allow_html=True)col_min, col_slide, col_plus = st.columns([1, 4, 1])with col_min:st.button("−", on_click=step_fringe, args=(-1,), width="stretch", key="btn_minus")with col_slide:grow_px = st.slider("Fringe", 0, 5, key="fringe_val", label_visibility="collapsed")with col_plus:st.button("+", on_click=step_fringe, args=(1,), width="stretch", key="btn_plus")# 3. Process & Render Result1 unmodified linewith col1:st.markdown('<div class="image-card-title">Source Image</div>', unsafe_allow_html=True)st.image(original_image, width="stretch")with col2:st.markdown('<div class="image-card-title">Extraction Result</div>', unsafe_allow_html=True)20 unmodified linesextracted_image = chroma_img.copy()extracted_image.putalpha(Image.fromarray(combined_alpha))st.image(extracted_image, width="stretch")buf = io.BytesIO()extracted_image.save(buf, format="PNG")2 unmodified linesdata=buf.getvalue(),file_name="product_asset.png",mime="image/png",width="stretch")except ModuleNotFoundError as exc:
app.py+5-9