Spaces:
Build error
Build error
Upload 2 files
Browse files- app.py +138 -0
- requirements.txt +139 -0
app.py
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import torch
|
| 3 |
+
from torch import nn
|
| 4 |
+
from diffusers import DDPMScheduler, UNet2DModel
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
from tqdm.auto import tqdm
|
| 7 |
+
|
| 8 |
+
# Reuse your existing model code
|
| 9 |
+
class ClassConditionedUnet(nn.Module):
|
| 10 |
+
def __init__(self, num_classes=3, class_emb_size=12):
|
| 11 |
+
super().__init__()
|
| 12 |
+
self.class_emb = nn.Embedding(num_classes, class_emb_size)
|
| 13 |
+
self.model = UNet2DModel(
|
| 14 |
+
sample_size=64,
|
| 15 |
+
in_channels=3 + class_emb_size,
|
| 16 |
+
out_channels=3,
|
| 17 |
+
layers_per_block=2,
|
| 18 |
+
block_out_channels=(64, 128, 256, 512),
|
| 19 |
+
down_block_types=(
|
| 20 |
+
"DownBlock2D",
|
| 21 |
+
"DownBlock2D",
|
| 22 |
+
"AttnDownBlock2D",
|
| 23 |
+
"AttnDownBlock2D",
|
| 24 |
+
),
|
| 25 |
+
up_block_types=(
|
| 26 |
+
"AttnUpBlock2D",
|
| 27 |
+
"AttnUpBlock2D",
|
| 28 |
+
"UpBlock2D",
|
| 29 |
+
"UpBlock2D",
|
| 30 |
+
),
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
def forward(self, x, t, class_labels):
|
| 34 |
+
bs, ch, w, h = x.shape
|
| 35 |
+
class_cond = self.class_emb(class_labels)
|
| 36 |
+
class_cond = class_cond.view(bs, class_cond.shape[1], 1, 1).expand(bs, class_cond.shape[1], w, h)
|
| 37 |
+
net_input = torch.cat((x, class_cond), 1)
|
| 38 |
+
return self.model(net_input, t).sample
|
| 39 |
+
|
| 40 |
+
@st.cache_resource
|
| 41 |
+
def load_model(model_path):
|
| 42 |
+
"""Load the model with caching to avoid reloading"""
|
| 43 |
+
device = 'cpu' # For deployment, we'll use CPU
|
| 44 |
+
net = ClassConditionedUnet().to(device)
|
| 45 |
+
noise_scheduler = DDPMScheduler(num_train_timesteps=1000, beta_schedule='squaredcos_cap_v2')
|
| 46 |
+
checkpoint = torch.load(model_path, map_location='cpu')
|
| 47 |
+
net.load_state_dict(checkpoint['model_state_dict'])
|
| 48 |
+
return net, noise_scheduler
|
| 49 |
+
|
| 50 |
+
def generate_mixed_faces(net, noise_scheduler, mix_weights, num_images=1):
|
| 51 |
+
"""Generate faces with mixed ethnic features"""
|
| 52 |
+
device = next(net.parameters()).device
|
| 53 |
+
net.eval()
|
| 54 |
+
with torch.no_grad():
|
| 55 |
+
x = torch.randn(num_images, 3, 64, 64).to(device)
|
| 56 |
+
|
| 57 |
+
# Get embeddings for all classes
|
| 58 |
+
emb_asian = net.class_emb(torch.zeros(num_images).long().to(device))
|
| 59 |
+
emb_indian = net.class_emb(torch.ones(num_images).long().to(device))
|
| 60 |
+
emb_european = net.class_emb(torch.full((num_images,), 2).to(device))
|
| 61 |
+
|
| 62 |
+
progress_bar = st.progress(0)
|
| 63 |
+
for idx, t in enumerate(noise_scheduler.timesteps):
|
| 64 |
+
# Update progress bar
|
| 65 |
+
progress_bar.progress(idx / len(noise_scheduler.timesteps))
|
| 66 |
+
|
| 67 |
+
# Mix embeddings according to weights
|
| 68 |
+
mixed_emb = (
|
| 69 |
+
mix_weights[0] * emb_asian +
|
| 70 |
+
mix_weights[1] * emb_indian +
|
| 71 |
+
mix_weights[2] * emb_european
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
# Override embedding layer temporarily
|
| 75 |
+
original_forward = net.class_emb.forward
|
| 76 |
+
net.class_emb.forward = lambda _: mixed_emb
|
| 77 |
+
|
| 78 |
+
residual = net(x, t, torch.zeros(num_images).long().to(device))
|
| 79 |
+
x = noise_scheduler.step(residual, t, x).prev_sample
|
| 80 |
+
|
| 81 |
+
# Restore original embedding layer
|
| 82 |
+
net.class_emb.forward = original_forward
|
| 83 |
+
|
| 84 |
+
progress_bar.progress(1.0)
|
| 85 |
+
|
| 86 |
+
x = (x.clamp(-1, 1) + 1) / 2
|
| 87 |
+
return x
|
| 88 |
+
|
| 89 |
+
def main():
|
| 90 |
+
st.title("AI Face Generator with Ethnic Features Mixing")
|
| 91 |
+
|
| 92 |
+
# Load model
|
| 93 |
+
try:
|
| 94 |
+
net, noise_scheduler = load_model('final_model/final_diffusion_model.pt')
|
| 95 |
+
except Exception as e:
|
| 96 |
+
st.error(f"Error loading model: {str(e)}")
|
| 97 |
+
return
|
| 98 |
+
|
| 99 |
+
# Create sliders for ethnicity percentages
|
| 100 |
+
st.subheader("Adjust Ethnicity Mix")
|
| 101 |
+
col1, col2, col3 = st.columns(3)
|
| 102 |
+
|
| 103 |
+
with col1:
|
| 104 |
+
asian_pct = st.slider("Asian Features %", 0, 100, 33, 1)
|
| 105 |
+
with col2:
|
| 106 |
+
indian_pct = st.slider("Indian Features %", 0, 100, 33, 1)
|
| 107 |
+
with col3:
|
| 108 |
+
european_pct = st.slider("European Features %", 0, 100, 34, 1)
|
| 109 |
+
|
| 110 |
+
# Calculate total and normalize if needed
|
| 111 |
+
total = asian_pct + indian_pct + european_pct
|
| 112 |
+
if total == 0:
|
| 113 |
+
st.warning("Total percentage cannot be 0%. Please adjust the sliders.")
|
| 114 |
+
return
|
| 115 |
+
|
| 116 |
+
# Normalize weights to sum to 1
|
| 117 |
+
weights = [asian_pct/total, indian_pct/total, european_pct/total]
|
| 118 |
+
|
| 119 |
+
# Display current mix
|
| 120 |
+
st.write("Current mix (normalized):")
|
| 121 |
+
st.write(f"Asian: {weights[0]:.2%}, Indian: {weights[1]:.2%}, European: {weights[2]:.2%}")
|
| 122 |
+
|
| 123 |
+
# Generate button
|
| 124 |
+
if st.button("Generate Face"):
|
| 125 |
+
try:
|
| 126 |
+
with st.spinner("Generating face..."):
|
| 127 |
+
# Generate the image
|
| 128 |
+
generated_images = generate_mixed_faces(net, noise_scheduler, weights)
|
| 129 |
+
|
| 130 |
+
# Convert to numpy and display
|
| 131 |
+
img = generated_images[0].permute(1, 2, 0).cpu().numpy()
|
| 132 |
+
st.image(img, caption="Generated Face", use_column_width=True)
|
| 133 |
+
|
| 134 |
+
except Exception as e:
|
| 135 |
+
st.error(f"Error generating image: {str(e)}")
|
| 136 |
+
|
| 137 |
+
if __name__ == "__main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 7 |
+
anyio==4.4.0
|
| 8 |
+
asttokens==2.4.1
|
| 9 |
+
async-timeout==4.0.3
|
| 10 |
+
attrs==24.2.0
|
| 11 |
+
blinker==1.8.2
|
| 12 |
+
boto3==1.35.54
|
| 13 |
+
botocore==1.35.54
|
| 14 |
+
cachetools==5.5.0
|
| 15 |
+
certifi==2024.8.30
|
| 16 |
+
charset-normalizer==3.3.2
|
| 17 |
+
click==8.1.7
|
| 18 |
+
colorama==0.4.6
|
| 19 |
+
comm==0.2.2
|
| 20 |
+
contourpy==1.3.1
|
| 21 |
+
cycler==0.12.1
|
| 22 |
+
databricks-api==0.9.0
|
| 23 |
+
databricks-cli==0.18.0
|
| 24 |
+
dataclasses==0.6
|
| 25 |
+
debugpy==1.8.5
|
| 26 |
+
decorator==5.1.1
|
| 27 |
+
diffusers==0.31.0
|
| 28 |
+
exceptiongroup==1.2.2
|
| 29 |
+
executing==2.1.0
|
| 30 |
+
faiss-cpu==1.8.0.post1
|
| 31 |
+
filelock==3.16.0
|
| 32 |
+
fonttools==4.55.3
|
| 33 |
+
frozenlist==1.4.1
|
| 34 |
+
fsspec==2024.9.0
|
| 35 |
+
gitdb==4.0.11
|
| 36 |
+
GitPython==3.1.43
|
| 37 |
+
greenlet==3.1.0
|
| 38 |
+
h11==0.14.0
|
| 39 |
+
httpcore==1.0.5
|
| 40 |
+
httpx==0.27.2
|
| 41 |
+
huggingface==0.0.1
|
| 42 |
+
huggingface-hub==0.24.7
|
| 43 |
+
idna==3.10
|
| 44 |
+
importlib_metadata==8.5.0
|
| 45 |
+
ipykernel==6.29.5
|
| 46 |
+
ipython==8.27.0
|
| 47 |
+
jedi==0.19.1
|
| 48 |
+
Jinja2==3.1.4
|
| 49 |
+
jmespath==1.0.1
|
| 50 |
+
joblib==1.4.2
|
| 51 |
+
johnsnowlabs==5.5.0
|
| 52 |
+
jsonpatch==1.33
|
| 53 |
+
jsonpointer==3.0.0
|
| 54 |
+
jsonschema==4.23.0
|
| 55 |
+
jsonschema-specifications==2023.12.1
|
| 56 |
+
jupyter_client==8.6.3
|
| 57 |
+
jupyter_core==5.7.2
|
| 58 |
+
kiwisolver==1.4.7
|
| 59 |
+
langchain==0.3.0
|
| 60 |
+
langchain-core==0.3.0
|
| 61 |
+
langchain-text-splitters==0.3.0
|
| 62 |
+
langsmith==0.1.121
|
| 63 |
+
markdown-it-py==3.0.0
|
| 64 |
+
MarkupSafe==2.1.5
|
| 65 |
+
matplotlib==3.9.3
|
| 66 |
+
matplotlib-inline==0.1.7
|
| 67 |
+
mdurl==0.1.2
|
| 68 |
+
mpmath==1.3.0
|
| 69 |
+
multidict==6.1.0
|
| 70 |
+
narwhals==1.8.1
|
| 71 |
+
nest-asyncio==1.6.0
|
| 72 |
+
networkx==3.3
|
| 73 |
+
nlu==5.4.1
|
| 74 |
+
numpy==1.26.4
|
| 75 |
+
oauthlib==3.2.2
|
| 76 |
+
orjson==3.10.7
|
| 77 |
+
packaging==24.1
|
| 78 |
+
pandas==2.2.2
|
| 79 |
+
parso==0.8.4
|
| 80 |
+
pillow==10.4.0
|
| 81 |
+
platformdirs==4.3.3
|
| 82 |
+
prompt_toolkit==3.0.47
|
| 83 |
+
protobuf==5.28.1
|
| 84 |
+
psutil==6.0.0
|
| 85 |
+
pure_eval==0.2.3
|
| 86 |
+
py4j==0.10.9
|
| 87 |
+
pyarrow==17.0.0
|
| 88 |
+
pydantic==2.9.1
|
| 89 |
+
pydantic_core==2.23.3
|
| 90 |
+
pydeck==0.9.1
|
| 91 |
+
Pygments==2.18.0
|
| 92 |
+
PyJWT==2.9.0
|
| 93 |
+
pyparsing==3.2.0
|
| 94 |
+
pyspark==3.0.2
|
| 95 |
+
python-dateutil==2.9.0.post0
|
| 96 |
+
pytz==2024.2
|
| 97 |
+
pywin32==306
|
| 98 |
+
PyYAML==6.0.2
|
| 99 |
+
pyzmq==26.2.0
|
| 100 |
+
referencing==0.35.1
|
| 101 |
+
regex==2024.9.11
|
| 102 |
+
requests==2.32.3
|
| 103 |
+
rich==13.8.1
|
| 104 |
+
rpds-py==0.20.0
|
| 105 |
+
s3transfer==0.10.3
|
| 106 |
+
safetensors==0.4.5
|
| 107 |
+
scikit-learn==1.5.2
|
| 108 |
+
scipy==1.14.1
|
| 109 |
+
sentence-transformers==3.1.0
|
| 110 |
+
six==1.16.0
|
| 111 |
+
smmap==5.0.1
|
| 112 |
+
sniffio==1.3.1
|
| 113 |
+
spark-nlp==5.5.0
|
| 114 |
+
spark-nlp-display==5.0
|
| 115 |
+
SQLAlchemy==2.0.35
|
| 116 |
+
stack-data==0.6.3
|
| 117 |
+
streamlit==1.38.0
|
| 118 |
+
streamlit-chat==0.1.1
|
| 119 |
+
svgwrite==1.4
|
| 120 |
+
sympy==1.13.1
|
| 121 |
+
tabulate==0.9.0
|
| 122 |
+
tenacity==8.5.0
|
| 123 |
+
threadpoolctl==3.5.0
|
| 124 |
+
tiktoken==0.7.0
|
| 125 |
+
tokenizers==0.19.1
|
| 126 |
+
toml==0.10.2
|
| 127 |
+
torch==2.5.1
|
| 128 |
+
torchvision==0.20.1
|
| 129 |
+
tornado==6.4.1
|
| 130 |
+
tqdm==4.66.5
|
| 131 |
+
traitlets==5.14.3
|
| 132 |
+
transformers==4.44.2
|
| 133 |
+
typing_extensions==4.12.2
|
| 134 |
+
tzdata==2024.1
|
| 135 |
+
urllib3==2.2.3
|
| 136 |
+
watchdog==4.0.2
|
| 137 |
+
wcwidth==0.2.13
|
| 138 |
+
yarl==1.11.1
|
| 139 |
+
zipp==3.21.0
|