Bavisetti commited on
Commit
6faa520
·
verified ·
1 Parent(s): 7931157

updated code

Browse files
Files changed (1) hide show
  1. app.py +20 -14
app.py CHANGED
@@ -1,21 +1,27 @@
1
- import gradio as gr
2
- from diffusers import StableDiffusionPipeline
3
  import torch
 
 
 
4
 
5
- # Load the model (Make sure to set up the correct model path)
6
- pipe = StableDiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16)
7
- # pipe = pipe.to("cuda")
8
- pipe = pipe.to("cuda" if torch.cuda.is_available() else "cpu")
9
-
 
10
 
11
- # Define the function to process the uploaded image
12
  def generate_headshot(image):
13
- # Process the uploaded image and generate a professional headshot
14
- generated_image = pipe(image).images[0]
15
- return generated_image
16
 
17
- # Create the Gradio interface
18
- iface = gr.Interface(fn=generate_headshot, inputs=gr.Image(), outputs=gr.Image(), title="Professional Headshot Generator")
 
 
 
 
 
19
 
20
- # Launch the interface
 
21
  iface.launch()
 
 
 
1
  import torch
2
+ from diffusers import StableDiffusionImg2ImgPipeline
3
+ from PIL import Image
4
+ import gradio as gr
5
 
6
+ # Load Stable Diffusion Image-to-Image Pipeline
7
+ pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
8
+ "CompVis/stable-diffusion-v1-4",
9
+ torch_dtype=torch.float16
10
+ )
11
+ pipe.to("cuda" if torch.cuda.is_available() else "cpu") # Use GPU if available
12
 
 
13
  def generate_headshot(image):
14
+ # Convert to PIL format
15
+ image = Image.open(image).convert("RGB")
 
16
 
17
+ # Define the text prompt to guide AI
18
+ prompt = "A professional corporate headshot, studio lighting, high resolution, DSLR quality"
19
+
20
+ # Run the pipeline with the uploaded image as input
21
+ generated_image = pipe(prompt=prompt, image=image, strength=0.7).images[0]
22
+
23
+ return generated_image
24
 
25
+ # Create Gradio UI
26
+ iface = gr.Interface(fn=generate_headshot, inputs="image", outputs="image")
27
  iface.launch()