AkashKumarave commited on
Commit
24da2db
·
verified ·
1 Parent(s): e9e3816

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -22
app.py CHANGED
@@ -31,8 +31,7 @@ if not os.path.exists(buffalo_l_zip):
31
  print("Download completed.")
32
  except Exception as e:
33
  print(f"Failed to download buffalo_l.zip: {e}")
34
- # Fallback to a lighter model or raise error
35
- raise RuntimeError("Cannot download buffalo_l.zip. Please ensure network access or preload the file.")
36
 
37
  # Extract buffalo_l.zip
38
  if os.path.exists(buffalo_l_zip):
@@ -46,12 +45,26 @@ face_app = FaceAnalysis(providers=["CPUExecutionProvider"], root="/home/user/.in
46
  face_app.prepare(ctx_id=0, det_size=(480, 480))
47
 
48
  # Define paths for preloaded weights
49
- model_path = "./" # Kolors base model weights
50
  ip_adapter_path = "./"
51
 
52
- # Check if files exist
53
- if not os.path.exists(model_path + "diffusion_pytorch_model.safetensors"):
54
- raise FileNotFoundError(f"Kolors model weights not found at {model_path}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  if not os.path.exists(ip_adapter_path + "ip-adapter.bin"):
56
  raise FileNotFoundError(f"IP-Adapter weights not found at {ip_adapter_path}")
57
 
@@ -82,19 +95,4 @@ def generate_image(uploaded_image, prompt):
82
  image_embeds=face_emb,
83
  num_inference_steps=20,
84
  guidance_scale=7.5,
85
- height=512,
86
- width=512,
87
- ).images[0]
88
- return "Image generated successfully!", image
89
- except Exception as e:
90
- return f"Generation failed: {e}", None
91
-
92
- interface = gr.Interface(
93
- fn=generate_image,
94
- inputs=[gr.Image(type="pil", label="Upload Reference Image"), gr.Textbox(label="Enter Prompt", placeholder="e.g., A photorealistic astronaut in space")],
95
- outputs=[gr.Textbox(label="Status"), gr.Image(label="Generated Image")],
96
- title="Face Reference Image Generator (Kolors with IP-Adapter)",
97
- description="Upload an image with a face, enter a prompt, and generate a new image preserving the reference face."
98
- )
99
-
100
- interface.launch()
 
31
  print("Download completed.")
32
  except Exception as e:
33
  print(f"Failed to download buffalo_l.zip: {e}")
34
+ raise RuntimeError("Cannot download buffalo_l.zip. Please ensure network access.")
 
35
 
36
  # Extract buffalo_l.zip
37
  if os.path.exists(buffalo_l_zip):
 
45
  face_app.prepare(ctx_id=0, det_size=(480, 480))
46
 
47
  # Define paths for preloaded weights
48
+ model_path = "./unet/" # Adjusted for preloaded Kolors weights
49
  ip_adapter_path = "./"
50
 
51
+ # Check if Kolors weights exist, download if missing
52
+ kolors_weights = model_path + "diffusion_pytorch_model.safetensors"
53
+ if not os.path.exists(model_path):
54
+ os.makedirs(model_path, exist_ok=True)
55
+ if not os.path.exists(kolors_weights):
56
+ try:
57
+ print("Downloading Kolors model weights...")
58
+ urllib.request.urlretrieve(
59
+ "https://huggingface.co/Kwai-Kolors/Kolors-diffusers/resolve/main/unet/diffusion_pytorch_model.safetensors",
60
+ kolors_weights
61
+ )
62
+ print("Kolors weights downloaded.")
63
+ except Exception as e:
64
+ print(f"Failed to download Kolors weights: {e}")
65
+ raise RuntimeError("Cannot download Kolors weights. Please ensure network access.")
66
+
67
+ # Check if IP-Adapter weights exist
68
  if not os.path.exists(ip_adapter_path + "ip-adapter.bin"):
69
  raise FileNotFoundError(f"IP-Adapter weights not found at {ip_adapter_path}")
70
 
 
95
  image_embeds=face_emb,
96
  num_inference_steps=20,
97
  guidance_scale=7.5,
98
+ height=512,