Spaces:
Runtime error
Runtime error
Update app.py
Browse files1231 unmodified line567891031 unmodified lines4243444546474849505152535455565758596061626364import gradio as grimport torchimport numpy as np1 unmodified lineimport osimport legacyimport torch_utils# Load the pre-trained StyleGAN modeldevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')31 unmodified linesdef style_mixing_interface(image1, image2, mix_value):if image1 is None or image2 is None:return Noneselected_layers = list(range(mix_value + 1))return mix_styles(image1, image2, selected_layers)# Gradio UIiface = gr.Interface(fn=style_mixing_interface,inputs=[gr.Image(label="First Clothing Image", type="filepath"),gr.Image(label="Second Clothing Image", type="filepath"),gr.Slider(label="Style Mixing Strength (Layers 0 to N)", minimum=0, maximum=9, step=1, value=5)],outputs=gr.Image(label="Mixed Clothing Design"),live=True,title="Style Mixing for Clothing Design",description="Upload two projected images and choose how many early layers to mix.")iface.launch()12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970711 unmodified line73747576777879808131 unmodified lines113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166# import gradio as gr# import torch# import numpy as np# from PIL import Image# import os# import legacy# import torch_utils# # Load the pre-trained StyleGAN model# device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')# model_path = 'dress_model.pkl' # Place your .pkl in the same directory or update path# # Load StyleGAN Generator# with open(model_path, 'rb') as f:# G = legacy.load_network_pkl(f)['G_ema'].to(device)# def mix_styles(image1_path, image2_path, styles_to_mix):# # Extract image names (without extensions)# image1_name = os.path.splitext(os.path.basename(image1_path))[0]# image2_name = os.path.splitext(os.path.basename(image2_path))[0]# # Load latent vectors from .npz# latent_vector_1 = np.load(os.path.join("projection_results", image1_name, "projected_w.npz"))['w']# latent_vector_2 = np.load(os.path.join("projection_results", image2_name, "projected_w.npz"))['w']# # Convert to torch tensors# latent_1_tensor = torch.from_numpy(latent_vector_1).to(device)# latent_2_tensor = torch.from_numpy(latent_vector_2).to(device)# # Mix layers# mixed_latent = latent_1_tensor.clone()# mixed_latent[:, styles_to_mix] = latent_2_tensor[:, styles_to_mix]# # Generate image# with torch.no_grad():# image = G.synthesis(mixed_latent, noise_mode='const')# # Convert to image# image = (image.permute(0, 2, 3, 1) * 127.5 + 128).clamp(0, 255).to(torch.uint8).cpu().numpy()# mixed_image = Image.fromarray(image[0], 'RGB')# return mixed_image# def style_mixing_interface(image1, image2, mix_value):# if image1 is None or image2 is None:# return None# selected_layers = list(range(mix_value + 1))# return mix_styles(image1, image2, selected_layers)# # Gradio UI# iface = gr.Interface(# fn=style_mixing_interface,# inputs=[# gr.Image(label="First Clothing Image", type="filepath"),# gr.Image(label="Second Clothing Image", type="filepath"),# gr.Slider(label="Style Mixing Strength (Layers 0 to N)", minimum=0, maximum=9, step=1, value=5)# ],# outputs=gr.Image(label="Mixed Clothing Design"),# live=True,# title="Style Mixing for Clothing Design",# description="Upload two projected images and choose how many early layers to mix."# )# iface.launch()import gradio as grimport torchimport numpy as np1 unmodified lineimport osimport legacyimport torch_utilsimport requestsimport ioimport base64# Load the pre-trained StyleGAN modeldevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')31 unmodified linesdef style_mixing_interface(image1, image2, mix_value):if image1 is None or image2 is None:return None, Noneselected_layers = list(range(mix_value + 1))mixed_img = mix_styles(image1, image2, selected_layers)# Convert to base64buffer = io.BytesIO()mixed_img.save(buffer, format="PNG")img_bytes = buffer.getvalue()img_base64 = base64.b64encode(img_bytes).decode("utf-8")return mixed_img, img_base64def send_to_backend(base64_img):try:response = requests.post("http://localhost:3000/customisation/save", # Change if using different port/routejson={"image": base64_img},timeout=10)if response.status_code == 200:return "✅ Saved to database!"else:return f"❌ Failed to save: {response.status_code} - {response.text}"except Exception as e:return f"⚠️ Error: {str(e)}"# Gradio UIwith gr.Blocks(title="Style Mixing for Clothing Design") as iface:gr.Markdown("## Style Mixing for Clothing Design\nUpload two projected clothing images and select how many early layers to mix.")with gr.Row():image1_input = gr.Image(label="First Clothing Image", type="filepath")image2_input = gr.Image(label="Second Clothing Image", type="filepath")mix_slider = gr.Slider(label="Style Mixing Strength (Layers 0 to N)", minimum=0, maximum=9, step=1, value=5)output_image = gr.Image(label="Mixed Clothing Design")base64_output = gr.Textbox(visible=False)download_button = gr.Button("Download & Save to Database")save_status = gr.Textbox(label="Save Status", interactive=False)def mix_and_return(image1, image2, mix_value):return style_mixing_interface(image1, image2, mix_value)mix_slider.change(mix_and_return, inputs=[image1_input, image2_input, mix_slider], outputs=[output_image, base64_output])download_button.click(fn=send_to_backend, inputs=[base64_output], outputs=[save_status])iface.launch()
app.py+116-14