updated
Browse files
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
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 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 |
-
#
|
| 57 |
-
|
| 58 |
|
| 59 |
-
#
|
| 60 |
-
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
| 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()
|