updated
Browse files
app.py
CHANGED
|
@@ -1,29 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import torch
|
| 2 |
import numpy as np
|
| 3 |
-
from diffusers import StableDiffusionImg2ImgPipeline
|
| 4 |
from PIL import Image
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
"CompVis/stable-diffusion-v1-4",
|
| 10 |
-
torch_dtype=torch.float16
|
| 11 |
-
)
|
| 12 |
-
pipe.to("cuda" if torch.cuda.is_available() else "cpu") # Use GPU if available
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
if isinstance(image, np.ndarray):
|
| 17 |
-
image = Image.fromarray(image)
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
|
|
|
| 24 |
|
| 25 |
-
return generated_image
|
| 26 |
|
| 27 |
-
# Create Gradio UI
|
| 28 |
-
iface = gr.Interface(fn=generate_headshot, inputs="image", outputs="image")
|
| 29 |
-
iface.launch()
|
|
|
|
| 1 |
+
# import torch
|
| 2 |
+
# import numpy as np
|
| 3 |
+
# from diffusers import StableDiffusionImg2ImgPipeline
|
| 4 |
+
# from PIL import Image
|
| 5 |
+
# import gradio as gr
|
| 6 |
+
|
| 7 |
+
# # Load Stable Diffusion Image-to-Image Pipeline
|
| 8 |
+
# pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
|
| 9 |
+
# "CompVis/stable-diffusion-v1-4",
|
| 10 |
+
# torch_dtype=torch.float16
|
| 11 |
+
# )
|
| 12 |
+
# pipe.to("cuda" if torch.cuda.is_available() else "cpu") # Use GPU if available
|
| 13 |
+
|
| 14 |
+
# def generate_headshot(image):
|
| 15 |
+
# # Convert NumPy array to PIL Image
|
| 16 |
+
# if isinstance(image, np.ndarray):
|
| 17 |
+
# image = Image.fromarray(image)
|
| 18 |
+
|
| 19 |
+
# # Define the AI prompt for professional headshots
|
| 20 |
+
# prompt = "A professional corporate headshot, studio lighting, high resolution, DSLR quality"
|
| 21 |
+
|
| 22 |
+
# # Generate the AI-enhanced headshot
|
| 23 |
+
# generated_image = pipe(prompt=prompt, image=image, strength=0.7).images[0]
|
| 24 |
+
|
| 25 |
+
# return generated_image
|
| 26 |
+
|
| 27 |
+
# # Create Gradio UI
|
| 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 |
+
|
| 45 |
+
# Enable CPU optimizations
|
| 46 |
+
pipe.enable_attention_slicing()
|
| 47 |
+
pipe.enable_sequential_cpu_offload()
|
| 48 |
+
|
| 49 |
+
# Load your sample image (make sure it's a clear, high-quality image)
|
| 50 |
+
input_image = Image.open("your_sample_image.jpg").convert("RGB")
|
| 51 |
|
| 52 |
+
# Resize image to match model's expected size (512x512)
|
| 53 |
+
input_image = input_image.resize((512, 512))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
+
# Define a denoising strength (0.2-0.8) → Lower = keeps original details, Higher = more AI changes
|
| 56 |
+
denoising_strength = 0.5
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
# Generate a professional headshot using img2img
|
| 59 |
+
generated_image = pipe(image=input_image, strength=denoising_strength, guidance_scale=7.5).images[0]
|
| 60 |
|
| 61 |
+
# Save and display the generated image
|
| 62 |
+
generated_image.save("ai_headshot.png")
|
| 63 |
+
generated_image.show()
|
| 64 |
|
|
|
|
| 65 |
|
|
|
|
|
|
|
|
|