File size: 2,070 Bytes
e5f3fb7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff424f5
a229981
e5f3fb7
9db34e0
 
e5f3fb7
a229981
9db34e0
 
 
 
 
ff424f5
9db34e0
 
ff424f5
9db34e0
 
 
 
6faa520
e84961b
 
 
9db34e0
 
 
6faa520
9db34e0
ff424f5
9db34e0
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# import torch
# import numpy as np
# from diffusers import StableDiffusionImg2ImgPipeline
# from PIL import Image
# import gradio as gr

# # Load Stable Diffusion Image-to-Image Pipeline
# pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
#     "CompVis/stable-diffusion-v1-4",
#     torch_dtype=torch.float16
# )
# pipe.to("cuda" if torch.cuda.is_available() else "cpu")  # Use GPU if available

# def generate_headshot(image):
#     # Convert NumPy array to PIL Image
#     if isinstance(image, np.ndarray):
#         image = Image.fromarray(image)

#     # Define the AI prompt for professional headshots
#     prompt = "A professional corporate headshot, studio lighting, high resolution, DSLR quality"

#     # Generate the AI-enhanced headshot
#     generated_image = pipe(prompt=prompt, image=image, strength=0.7).images[0]

#     return generated_image

# # Create Gradio UI
# iface = gr.Interface(fn=generate_headshot, inputs="image", outputs="image")
# iface.launch()
# ===================================
import torch
import numpy as np
from diffusers import StableDiffusionImg2ImgPipeline
from PIL import Image
import gradio as gr


# Load Stable Diffusion Image-to-Image Pipeline
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
    "stabilityai/stable-diffusion-2-1",
    torch_dtype=torch.float32  # Use float32 for CPU
)

# Force execution on CPU (since no GPU is available)
pipe.to("cpu")

# Define function to generate professional headshots
def generate_headshot(image):
    if isinstance(image, np.ndarray):
        image = Image.fromarray(image)

    prompt = "Ultra-realistic professional headshot, studio lighting, 4K resolution, "
    "sharp details, DSLR quality, corporate portrait, neutral background, "
    "perfect skin texture, cinematic lighting"
    
    # Generate image (Lower strength for subtle changes)
    generated_image = pipe(prompt=prompt, image=image, strength=0.5).images[0]

    return generated_image

# Create Gradio UI
iface = gr.Interface(fn=generate_headshot, inputs="image", outputs="image")
iface.launch()