snair94 commited on
Commit
02029ac
·
verified ·
1 Parent(s): 1601b51

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -60
app.py CHANGED
@@ -1,60 +1,60 @@
1
- import gradio as gr
2
- from PIL import Image
3
- from diffusers import StableDiffusionImg2ImgPipeline
4
- import torch
5
- import uuid
6
-
7
- # Set device
8
- device = "cuda" if torch.cuda.is_available() else "cpu"
9
- dtype = torch.float16 if device == "cuda" else torch.float32
10
-
11
- # Load model
12
- pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
13
- "runwayml/stable-diffusion-v1-5",
14
- torch_dtype=dtype,
15
- use_safetensors=True
16
- ).to(device)
17
-
18
- # Resize to match selected aspect ratio
19
- def resize_to_aspect(image, aspect_ratio):
20
- width, height = image.size
21
- aspect_map = {
22
- "1:1": (min(width, height), min(width, height)),
23
- "16:9": (width, int(width * 9 / 16)),
24
- "4:5": (width, int(width * 5 / 4)),
25
- "9:16": (int(height * 9 / 16), height)
26
- }
27
- target_w, target_h = aspect_map.get(aspect_ratio, (width, height))
28
- return image.resize((target_w, target_h))
29
-
30
- def resize_to_512(image):
31
- return image.resize((512, 512))
32
-
33
- # Generate the new image
34
- def generate_img(product_img, prompt, aspect_ratio):
35
- resized_img = resize_to_aspect(product_img, aspect_ratio).convert("RGB")
36
- resized_img = resize_to_512(resized_img)
37
- output = pipe(prompt=prompt, image=resized_img, strength=0.75, guidance_scale=7.5)
38
- image = output.images[0]
39
- save_path = f"/tmp/generated_{uuid.uuid4().hex}.png"
40
- image.save(save_path)
41
- return image, save_path
42
-
43
- # Launch interface
44
- demo = gr.Interface(
45
- fn=generate_img,
46
- inputs=[
47
- gr.Image(type="pil", label="Upload Product Image", image_mode='RGB'),
48
- gr.Textbox(label="Prompt", placeholder="Describe what you want to generate"),
49
- gr.Dropdown(["1:1", "16:9", "4:5", "9:16"], label="Aspect Ratio", value="1:1")
50
- ],
51
- outputs=[
52
- gr.Image(label='Preview'),
53
- gr.File(label='Download Image')
54
- ],
55
- title="Image-to-Image Product Generator",
56
- description="Upload a product image, describe your idea, and select the output aspect ratio."
57
- )
58
-
59
- if __name__ == "__main__":
60
- demo.launch(server_name="0.0.0.0", server_port=7860, debug=True, share=True, ssr_mode=False)
 
1
+ import gradio as gr
2
+ from PIL import Image
3
+ from diffusers import StableDiffusionImg2ImgPipeline
4
+ import torch
5
+ import uuid
6
+
7
+ # Set device
8
+ device = "cuda" if torch.cuda.is_available() else "cpu"
9
+ dtype = torch.float16 if device == "cuda" else torch.float32
10
+
11
+ # Load model
12
+ pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
13
+ "runwayml/stable-diffusion-v1-5",
14
+ torch_dtype=dtype,
15
+ use_safetensors=True
16
+ ).to(device)
17
+
18
+ # Resize to match selected aspect ratio
19
+ def resize_to_aspect(image, aspect_ratio):
20
+ width, height = image.size
21
+ aspect_map = {
22
+ "1:1": (min(width, height), min(width, height)),
23
+ "16:9": (width, int(width * 9 / 16)),
24
+ "4:5": (width, int(width * 5 / 4)),
25
+ "9:16": (int(height * 9 / 16), height)
26
+ }
27
+ target_w, target_h = aspect_map.get(aspect_ratio, (width, height))
28
+ return image.resize((target_w, target_h))
29
+
30
+ def resize_to_512(image):
31
+ return image.resize((512, 512))
32
+
33
+ # Generate the new image
34
+ def generate_img(product_img, prompt, aspect_ratio):
35
+ resized_img = resize_to_aspect(product_img, aspect_ratio).convert("RGB")
36
+ resized_img = resize_to_512(resized_img)
37
+ output = pipe(prompt=prompt, image=resized_img, strength=0.75, guidance_scale=7.5)
38
+ image = output.images[0]
39
+ save_path = f"/tmp/generated_{uuid.uuid4().hex}.png"
40
+ image.save(save_path)
41
+ return image, save_path
42
+
43
+ # Launch interface
44
+ demo = gr.Interface(
45
+ fn=generate_img,
46
+ inputs=[
47
+ gr.Image(type="pil", label="Upload Product Image", image_mode='RGB'),
48
+ gr.Textbox(label="Prompt", placeholder="Describe what you want to generate"),
49
+ gr.Dropdown(["1:1", "16:9", "4:5", "9:16"], label="Aspect Ratio", value="1:1")
50
+ ],
51
+ outputs=[
52
+ gr.Image(label='Preview'),
53
+ gr.File(label='Download Image')
54
+ ],
55
+ title="Image-to-Image Product Generator",
56
+ description="Upload a product image, describe your idea, and select the output aspect ratio."
57
+ )
58
+
59
+ if __name__ == "__main__":
60
+ demo.launch(server_name="0.0.0.0", server_port=7860, debug=True, ssr_mode=False)