import streamlit as st from PIL import Image import io from app import run_edit, ATTRIBUTE_MAP # reuse the logic from your existing code st.title("Smile Changer – Streamlit Edition") st.markdown( """ **AI-powered facial attribute editor**. Upload your photo, choose an attribute, and let the AI create a new version! *Supported attributes*: Smile, Age, Female features, Beard, Mustache/Goatee, Glasses, Makeup, Curly hair, Afro, Orange/Blonde hair (text) """ ) uploaded_file = st.file_uploader("Upload face image", type=["png", "jpg", "jpeg"]) if uploaded_file: image = Image.open(uploaded_file).convert("RGB") st.image(image, caption="Input face") col1, col2 = st.columns(2) with col1: attribute = st.selectbox( "Edit Attribute", list(ATTRIBUTE_MAP.keys()), index=list(ATTRIBUTE_MAP.keys()).index("Smile") ) with col2: lo, hi = ATTRIBUTE_MAP[attribute][1] strength = st.slider("Strength", float(lo), float(hi), float((lo+hi)/2), step=0.01) align_face = st.checkbox("Align face before editing", value=True) use_bg_mask = st.checkbox("Use background mask (reduce artifacts)", value=False) custom_text_edit = "" if attribute.endswith("(text)"): custom_text_edit = st.text_input( "Custom text edit (optional, for StyleCLIP Global Mapper)", value="" ) if st.button("Run Edit"): try: with st.spinner("Editing image..."): result = run_edit( image=image, attribute=attribute, strength=strength, align_face=align_face, use_bg_mask=use_bg_mask, custom_text_edit=custom_text_edit, ) st.image(result, caption=f"Output ({attribute})") # Download button buf = io.BytesIO() result.save(buf, format="PNG") st.download_button("Download result", buf.getvalue(), file_name="output.png", mime="image/png") except Exception as e: st.error(f"An error occurred: {e}") else: st.info("Please upload a face image to get started.")