Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,25 +1,95 @@
|
|
1 |
-
import streamlit as st
|
2 |
import os
|
3 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
-
#
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
-
#
|
9 |
-
|
10 |
-
|
11 |
-
test_order = st.selectbox("Test Order:", ["paired", "unpaired"])
|
12 |
-
save_name = st.text_input("Save Name:", "model_output")
|
13 |
|
14 |
-
#
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
18 |
|
19 |
-
# Run
|
20 |
-
with
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
st.code(result.stderr)
|
|
|
|
|
1 |
import os
|
2 |
+
import torch
|
3 |
+
import streamlit as st
|
4 |
+
from diffusers import AutoencoderKL, DDIMScheduler
|
5 |
+
from transformers import CLIPTextModel, CLIPTokenizer
|
6 |
+
from src.mgd_pipelines.mgd_pipe import MGDPipe
|
7 |
+
from src.mgd_pipelines.mgd_pipe_disentangled import MGDPipeDisentangled
|
8 |
+
from src.utils.image_from_pipe import generate_images_from_mgd_pipe
|
9 |
+
from accelerate import Accelerator
|
10 |
+
from diffusers.utils import check_min_version
|
11 |
+
from src.utils.set_seeds import set_seed
|
12 |
+
|
13 |
+
# Will error if the minimal version of diffusers is not installed. Remove at your own risks.
|
14 |
+
check_min_version("0.10.0.dev0")
|
15 |
+
|
16 |
+
# Set the environment variables for Hugging Face Spaces
|
17 |
+
os.environ["TOKENIZERS_PARALLELISM"] = "true"
|
18 |
+
os.environ["WANDB_START_METHOD"] = "thread"
|
19 |
+
|
20 |
+
# Streamlit interface components
|
21 |
+
st.title("Fashion Image Generation with Multimodal Garment Designer")
|
22 |
+
|
23 |
+
# Streamlit Input Parameters
|
24 |
+
category = st.selectbox("Select Category", ["dresses", "upper_body", "lower_body", "all"])
|
25 |
+
guidance_scale = st.slider("Guidance Scale", min_value=0.1, max_value=20.0, value=7.5, step=0.1)
|
26 |
+
guidance_scale_pose = st.slider("Guidance Scale (Pose)", min_value=0.1, max_value=20.0, value=7.5, step=0.1)
|
27 |
+
guidance_scale_sketch = st.slider("Guidance Scale (Sketch)", min_value=0.1, max_value=20.0, value=7.5, step=0.1)
|
28 |
+
sketch_cond_rate = st.slider("Sketch Conditioning Rate", min_value=0.1, max_value=1.0, value=0.5, step=0.05)
|
29 |
+
start_cond_rate = st.slider("Start Conditioning Rate", min_value=0.1, max_value=1.0, value=0.5, step=0.05)
|
30 |
+
seed = st.number_input("Seed", value=42, min_value=1)
|
31 |
+
|
32 |
+
# Button to run the image generation
|
33 |
+
if st.button("Generate Image"):
|
34 |
+
# Initialize Accelerator (for mixed precision, etc.)
|
35 |
+
accelerator = Accelerator()
|
36 |
+
device = accelerator.device
|
37 |
+
|
38 |
+
# Set the seed
|
39 |
+
set_seed(seed)
|
40 |
+
|
41 |
+
# Model and Tokenizer loading (use pre-trained from Hugging Face)
|
42 |
+
model_name = "stabilityai/stable-diffusion-2-1-base" # Use appropriate model name
|
43 |
+
|
44 |
+
# Load scheduler, tokenizer, and models
|
45 |
+
val_scheduler = DDIMScheduler.from_pretrained(model_name, subfolder="scheduler")
|
46 |
+
val_scheduler.set_timesteps(50, device=device)
|
47 |
+
|
48 |
+
tokenizer = CLIPTokenizer.from_pretrained(model_name, subfolder="tokenizer")
|
49 |
+
text_encoder = CLIPTextModel.from_pretrained(model_name, subfolder="text_encoder")
|
50 |
+
vae = AutoencoderKL.from_pretrained(model_name, subfolder="vae")
|
51 |
|
52 |
+
# Load UNet model (you can use your own model)
|
53 |
+
unet = torch.hub.load(
|
54 |
+
dataset="aimagelab/multimodal-garment-designer",
|
55 |
+
repo_or_dir="aimagelab/multimodal-garment-designer",
|
56 |
+
source="github",
|
57 |
+
model="mgd",
|
58 |
+
pretrained=True,
|
59 |
+
)
|
60 |
|
61 |
+
# Freeze VAE and text encoder
|
62 |
+
vae.requires_grad_(False)
|
63 |
+
text_encoder.requires_grad_(False)
|
|
|
|
|
64 |
|
65 |
+
# Select pipeline (use disentangled option if needed)
|
66 |
+
val_pipe = MGDPipe(
|
67 |
+
text_encoder=text_encoder,
|
68 |
+
vae=vae,
|
69 |
+
unet=unet.to(vae.dtype),
|
70 |
+
tokenizer=tokenizer,
|
71 |
+
scheduler=val_scheduler,
|
72 |
+
).to(device)
|
73 |
|
74 |
+
# Run image generation using your pipeline
|
75 |
+
with torch.no_grad():
|
76 |
+
# Generate the image
|
77 |
+
images = generate_images_from_mgd_pipe(
|
78 |
+
test_order="test", # or some predefined order
|
79 |
+
pipe=val_pipe,
|
80 |
+
test_dataloader=None, # Adjust accordingly, or use pre-existing dataset
|
81 |
+
save_name="generated_image",
|
82 |
+
dataset="dresscode", # Adjust if needed
|
83 |
+
output_dir=".", # Save location
|
84 |
+
guidance_scale=guidance_scale,
|
85 |
+
guidance_scale_pose=guidance_scale_pose,
|
86 |
+
guidance_scale_sketch=guidance_scale_sketch,
|
87 |
+
sketch_cond_rate=sketch_cond_rate,
|
88 |
+
start_cond_rate=start_cond_rate,
|
89 |
+
no_pose=False,
|
90 |
+
disentagle=False, # Adjust if needed
|
91 |
+
seed=seed,
|
92 |
+
)
|
93 |
|
94 |
+
# Display the generated image
|
95 |
+
st.image(images[0], caption="Generated Fashion Image", use_column_width=True)
|
|