fragger246 commited on
Commit
21e64dd
1 Parent(s): 9cb0f11

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +109 -110
app.py CHANGED
@@ -1,110 +1,109 @@
1
- import gradio as gr
2
- import torch
3
- from PIL import Image
4
- import numpy as np
5
- import cv2
6
- from diffusers import StableDiffusionPipeline
7
- from huggingface_hub import login
8
-
9
-
10
-
11
- # Setup the model
12
- device = "cuda" if torch.cuda.is_available() else "cpu"
13
- model_id = "s3nh/artwork-arcane-stable-diffusion"
14
- pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16 if device == "cuda" else torch.float32, use_auth_token=True)
15
- pipe = pipe.to(device)
16
-
17
- # Generate T-shirt design function
18
- def generate_tshirt_design(text):
19
- prompt = f"{text}"
20
- image = pipe(prompt).images[0]
21
- return image
22
-
23
- # Remove background from the generated design
24
- def remove_background(design_image):
25
- design_np = np.array(design_image)
26
- gray = cv2.cvtColor(design_np, cv2.COLOR_BGR2GRAY)
27
- _, alpha = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY)
28
- b, g, r = cv2.split(design_np)
29
- rgba = [b, g, r, alpha]
30
- design_np = cv2.merge(rgba, 4)
31
- return design_np
32
-
33
- # T-shirt mockup generator with Gradio interface
34
- examples = [
35
- ["MyBrand"],
36
- ["Hello World"],
37
- ["Team logo"],
38
- ]
39
-
40
- css = """
41
- #col-container {
42
- margin: 0 auto;
43
- max-width: 520px;
44
- }
45
- """
46
-
47
- with gr.Blocks(css=css) as demo:
48
- with gr.Column(elem_id="col-container"):
49
- gr.Markdown("""
50
- # T-shirt Design Generator with Stable Diffusion
51
- """)
52
-
53
- with gr.Row():
54
- text = gr.Textbox(
55
- label="Text",
56
- placeholder="Enter text for the T-shirt design",
57
- visible=True,
58
- )
59
-
60
- run_button = gr.Button("Generate Design", scale=0)
61
-
62
- result = gr.Image(label="Design", show_label=False)
63
-
64
- gr.Examples(
65
- examples=examples,
66
- inputs=[text]
67
- )
68
-
69
- def generate_tshirt_mockup(text):
70
- # Generate T-shirt design
71
- design_image = generate_tshirt_design(text)
72
-
73
- # Remove background from design image
74
- design_np = remove_background(design_image)
75
-
76
- # Load blank T-shirt mockup template image
77
- mockup_template = Image.open("/content/drive/MyDrive/unnamed.jpg")
78
-
79
- # Convert mockup template to numpy array
80
- mockup_np = np.array(mockup_template)
81
-
82
- # Resize design image to fit mockup
83
- design_resized = cv2.resize(design_np, (mockup_np.shape[1] // 4, mockup_np.shape[0] // 4)) # Adjust size as needed
84
-
85
- # Center the design on the mockup
86
- y_offset = (mockup_np.shape[0] - design_resized.shape[0]) // 2
87
- x_offset = (mockup_np.shape[1] - design_resized.shape[1]) // 2
88
- y1, y2 = y_offset, y_offset + design_resized.shape[0]
89
- x1, x2 = x_offset, x_offset + design_resized.shape[1]
90
-
91
- # Blend design with mockup using alpha channel
92
- alpha_s = design_resized[:, :, 3] / 255.0 if design_resized.shape[2] == 4 else np.ones(design_resized.shape[:2])
93
- alpha_l = 1.0 - alpha_s
94
-
95
- for c in range(0, 3):
96
- mockup_np[y1:y2, x1:x2, c] = (alpha_s * design_resized[:, :, c] +
97
- alpha_l * mockup_np[y1:y2, x1:x2, c])
98
-
99
- # Convert back to PIL image for Gradio output
100
- result_image = Image.fromarray(mockup_np)
101
-
102
- return result_image
103
-
104
- run_button.click(
105
- fn=generate_tshirt_mockup,
106
- inputs=[text],
107
- outputs=[result]
108
- )
109
-
110
- demo.queue().launch()
 
1
+ import gradio as gr
2
+ import torch
3
+ from PIL import Image
4
+ import numpy as np
5
+ import cv2
6
+ from diffusers import StableDiffusionPipeline
7
+
8
+
9
+
10
+ # Setup the model
11
+ device = "cuda" if torch.cuda.is_available() else "cpu"
12
+ model_id = "s3nh/artwork-arcane-stable-diffusion"
13
+ pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16 if device == "cuda" else torch.float32, use_auth_token=True)
14
+ pipe = pipe.to(device)
15
+
16
+ # Generate T-shirt design function
17
+ def generate_tshirt_design(text):
18
+ prompt = f"{text}"
19
+ image = pipe(prompt).images[0]
20
+ return image
21
+
22
+ # Remove background from the generated design
23
+ def remove_background(design_image):
24
+ design_np = np.array(design_image)
25
+ gray = cv2.cvtColor(design_np, cv2.COLOR_BGR2GRAY)
26
+ _, alpha = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY)
27
+ b, g, r = cv2.split(design_np)
28
+ rgba = [b, g, r, alpha]
29
+ design_np = cv2.merge(rgba, 4)
30
+ return design_np
31
+
32
+ # T-shirt mockup generator with Gradio interface
33
+ examples = [
34
+ ["MyBrand"],
35
+ ["Hello World"],
36
+ ["Team logo"],
37
+ ]
38
+
39
+ css = """
40
+ #col-container {
41
+ margin: 0 auto;
42
+ max-width: 520px;
43
+ }
44
+ """
45
+
46
+ with gr.Blocks(css=css) as demo:
47
+ with gr.Column(elem_id="col-container"):
48
+ gr.Markdown("""
49
+ # T-shirt Design Generator with Stable Diffusion
50
+ """)
51
+
52
+ with gr.Row():
53
+ text = gr.Textbox(
54
+ label="Text",
55
+ placeholder="Enter text for the T-shirt design",
56
+ visible=True,
57
+ )
58
+
59
+ run_button = gr.Button("Generate Design", scale=0)
60
+
61
+ result = gr.Image(label="Design", show_label=False)
62
+
63
+ gr.Examples(
64
+ examples=examples,
65
+ inputs=[text]
66
+ )
67
+
68
+ def generate_tshirt_mockup(text):
69
+ # Generate T-shirt design
70
+ design_image = generate_tshirt_design(text)
71
+
72
+ # Remove background from design image
73
+ design_np = remove_background(design_image)
74
+
75
+ # Load blank T-shirt mockup template image
76
+ mockup_template = Image.open("/content/drive/MyDrive/unnamed.jpg")
77
+
78
+ # Convert mockup template to numpy array
79
+ mockup_np = np.array(mockup_template)
80
+
81
+ # Resize design image to fit mockup
82
+ design_resized = cv2.resize(design_np, (mockup_np.shape[1] // 4, mockup_np.shape[0] // 4)) # Adjust size as needed
83
+
84
+ # Center the design on the mockup
85
+ y_offset = (mockup_np.shape[0] - design_resized.shape[0]) // 2
86
+ x_offset = (mockup_np.shape[1] - design_resized.shape[1]) // 2
87
+ y1, y2 = y_offset, y_offset + design_resized.shape[0]
88
+ x1, x2 = x_offset, x_offset + design_resized.shape[1]
89
+
90
+ # Blend design with mockup using alpha channel
91
+ alpha_s = design_resized[:, :, 3] / 255.0 if design_resized.shape[2] == 4 else np.ones(design_resized.shape[:2])
92
+ alpha_l = 1.0 - alpha_s
93
+
94
+ for c in range(0, 3):
95
+ mockup_np[y1:y2, x1:x2, c] = (alpha_s * design_resized[:, :, c] +
96
+ alpha_l * mockup_np[y1:y2, x1:x2, c])
97
+
98
+ # Convert back to PIL image for Gradio output
99
+ result_image = Image.fromarray(mockup_np)
100
+
101
+ return result_image
102
+
103
+ run_button.click(
104
+ fn=generate_tshirt_mockup,
105
+ inputs=[text],
106
+ outputs=[result]
107
+ )
108
+
109
+ demo.queue().launch()