Bavisetti commited on
Commit
9db34e0
·
verified ·
1 Parent(s): fea4667
Files changed (1) hide show
  1. app.py +21 -28
app.py CHANGED
@@ -28,39 +28,32 @@
28
  # iface = gr.Interface(fn=generate_headshot, inputs="image", outputs="image")
29
  # iface.launch()
30
  # ===================================
31
- # Install required packages (run in terminal if not installed)
32
- # pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu
33
- # pip install diffusers transformers accelerate safetensors Pillow numpy
34
-
35
  import torch
36
- import numpy as np
37
- from PIL import Image
38
  from diffusers import StableDiffusionImg2ImgPipeline
 
 
39
 
40
- # Load Stable Diffusion 2.1 img2img model
41
- model_id = "stabilityai/stable-diffusion-2-1"
42
- pipe = StableDiffusionImg2ImgPipeline.from_pretrained(model_id, torch_dtype=torch.float32)
43
- # pipe.to("cpu") # Change to "cuda" for GPU
44
- pipe.to("cuda" if torch.cuda.is_available() else "cpu")
45
-
46
- # Enable CPU optimizations
47
- pipe.enable_attention_slicing()
48
- pipe.enable_sequential_cpu_offload()
49
-
50
- # Load your sample image (make sure it's a clear, high-quality image)
51
- input_image = Image.open("your_sample_image.jpg").convert("RGB")
52
-
53
- # Resize image to match model's expected size (512x512)
54
- input_image = input_image.resize((512, 512))
55
 
56
- # Define a denoising strength (0.2-0.8) Lower = keeps original details, Higher = more AI changes
57
- denoising_strength = 0.5
58
 
59
- # Generate a professional headshot using img2img
60
- generated_image = pipe(image=input_image, strength=denoising_strength, guidance_scale=7.5).images[0]
 
 
61
 
62
- # Save and display the generated image
63
- generated_image.save("ai_headshot.png")
64
- generated_image.show()
 
65
 
 
66
 
 
 
 
 
28
  # iface = gr.Interface(fn=generate_headshot, inputs="image", outputs="image")
29
  # iface.launch()
30
  # ===================================
 
 
 
 
31
  import torch
 
 
32
  from diffusers import StableDiffusionImg2ImgPipeline
33
+ from PIL import Image
34
+ import gradio as gr
35
 
36
+ # Load Stable Diffusion Image-to-Image Pipeline
37
+ pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
38
+ "stabilityai/stable-diffusion-2-1",
39
+ torch_dtype=torch.float32 # Use float32 for CPU
40
+ )
 
 
 
 
 
 
 
 
 
 
41
 
42
+ # Force execution on CPU (since no GPU is available)
43
+ pipe.to("cpu")
44
 
45
+ # Define function to generate professional headshots
46
+ def generate_headshot(image):
47
+ if isinstance(image, np.ndarray):
48
+ image = Image.fromarray(image)
49
 
50
+ prompt = "A professional corporate headshot, studio lighting, high resolution, DSLR quality"
51
+
52
+ # Generate image (Lower strength for subtle changes)
53
+ generated_image = pipe(prompt=prompt, image=image, strength=0.5).images[0]
54
 
55
+ return generated_image
56
 
57
+ # Create Gradio UI
58
+ iface = gr.Interface(fn=generate_headshot, inputs="image", outputs="image")
59
+ iface.launch()