File size: 2,227 Bytes
88f684a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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.")