Spaces:
Sleeping
Sleeping
Commit
·
88f684a
1
Parent(s):
94b5199
Add Streamlit UI for Smile Changer (no API endpoints, Gradio logic reused, ready for Streamlit deployment)
Browse files- app_streamlit.py +63 -0
app_streamlit.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import io
|
| 4 |
+
|
| 5 |
+
from app import run_edit, ATTRIBUTE_MAP # reuse the logic from your existing code
|
| 6 |
+
|
| 7 |
+
st.title("Smile Changer – Streamlit Edition")
|
| 8 |
+
|
| 9 |
+
st.markdown(
|
| 10 |
+
"""
|
| 11 |
+
**AI-powered facial attribute editor**. Upload your photo, choose an attribute, and let the AI create a new version!
|
| 12 |
+
|
| 13 |
+
*Supported attributes*: Smile, Age, Female features, Beard, Mustache/Goatee, Glasses, Makeup, Curly hair, Afro, Orange/Blonde hair (text)
|
| 14 |
+
"""
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
uploaded_file = st.file_uploader("Upload face image", type=["png", "jpg", "jpeg"])
|
| 18 |
+
|
| 19 |
+
if uploaded_file:
|
| 20 |
+
image = Image.open(uploaded_file).convert("RGB")
|
| 21 |
+
st.image(image, caption="Input face")
|
| 22 |
+
|
| 23 |
+
col1, col2 = st.columns(2)
|
| 24 |
+
with col1:
|
| 25 |
+
attribute = st.selectbox(
|
| 26 |
+
"Edit Attribute",
|
| 27 |
+
list(ATTRIBUTE_MAP.keys()),
|
| 28 |
+
index=list(ATTRIBUTE_MAP.keys()).index("Smile")
|
| 29 |
+
)
|
| 30 |
+
with col2:
|
| 31 |
+
lo, hi = ATTRIBUTE_MAP[attribute][1]
|
| 32 |
+
strength = st.slider("Strength", float(lo), float(hi), float((lo+hi)/2), step=0.01)
|
| 33 |
+
|
| 34 |
+
align_face = st.checkbox("Align face before editing", value=True)
|
| 35 |
+
use_bg_mask = st.checkbox("Use background mask (reduce artifacts)", value=False)
|
| 36 |
+
|
| 37 |
+
custom_text_edit = ""
|
| 38 |
+
if attribute.endswith("(text)"):
|
| 39 |
+
custom_text_edit = st.text_input(
|
| 40 |
+
"Custom text edit (optional, for StyleCLIP Global Mapper)",
|
| 41 |
+
value=""
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
if st.button("Run Edit"):
|
| 45 |
+
try:
|
| 46 |
+
with st.spinner("Editing image..."):
|
| 47 |
+
result = run_edit(
|
| 48 |
+
image=image,
|
| 49 |
+
attribute=attribute,
|
| 50 |
+
strength=strength,
|
| 51 |
+
align_face=align_face,
|
| 52 |
+
use_bg_mask=use_bg_mask,
|
| 53 |
+
custom_text_edit=custom_text_edit,
|
| 54 |
+
)
|
| 55 |
+
st.image(result, caption=f"Output ({attribute})")
|
| 56 |
+
# Download button
|
| 57 |
+
buf = io.BytesIO()
|
| 58 |
+
result.save(buf, format="PNG")
|
| 59 |
+
st.download_button("Download result", buf.getvalue(), file_name="output.png", mime="image/png")
|
| 60 |
+
except Exception as e:
|
| 61 |
+
st.error(f"An error occurred: {e}")
|
| 62 |
+
else:
|
| 63 |
+
st.info("Please upload a face image to get started.")
|