cavargas10 commited on
Commit
abf9628
·
verified ·
1 Parent(s): b983696

Update launch/image_generation.py

Browse files
Files changed (1) hide show
  1. launch/image_generation.py +107 -116
launch/image_generation.py CHANGED
@@ -1,116 +1,107 @@
1
- import os
2
-
3
- import gradio as gr
4
- import rembg
5
- import spaces
6
- import torch
7
- from diffusers import DiffusionPipeline
8
-
9
- from instantMesh.src.utils.infer_util import (
10
- remove_background, resize_foreground)
11
-
12
-
13
- pipe = DiffusionPipeline.from_pretrained(
14
- "playgroundai/playground-v2.5-1024px-aesthetic",
15
- torch_dtype=torch.float16,
16
- variant="fp16"
17
- ).to("cuda")
18
-
19
-
20
- def generate_prompt(subject, style, color_scheme, angle, lighting_type, additional_details):
21
- return f"A 3D cartoon render of {subject}, featuring the entire body and shape, on a transparent background. The style should be {style}, with {color_scheme} colors, emphasizing the essential features and lines. The pose should clearly showcase the full form of the {subject} from a {angle} perspective. Lighting is {lighting_type}, highlighting the volume and depth of the subject. {additional_details}. Output as a high-resolution PNG with no background."
22
-
23
-
24
- @spaces.GPU
25
- def generate_image(subject, style, color_scheme, angle, lighting_type, additional_details):
26
- prompt = generate_prompt(subject, style, color_scheme,
27
- angle, lighting_type, additional_details)
28
- results = pipe(prompt, num_inference_steps=25, guidance_scale=7.5)
29
- return results.images[0]
30
-
31
-
32
- def check_input_image(input_image):
33
- if input_image is None:
34
- raise gr.Error("No image selected!")
35
-
36
-
37
- def preprocess(input_image):
38
- rembg_session = rembg.new_session()
39
-
40
- input_image = remove_background(input_image, rembg_session)
41
- input_image = resize_foreground(input_image, 0.85)
42
-
43
- return input_image
44
-
45
-
46
- def image_generation_ui():
47
- with gr.Row():
48
- subject = gr.Textbox(label='Subject', scale=2)
49
- style = gr.Dropdown(
50
- label='Style',
51
- choices=['Pixar-like', 'Disney-esque', 'Anime-inspired'],
52
- value='Pixar-like',
53
- multiselect=False,
54
- scale=2
55
- )
56
- color_scheme = gr.Dropdown(
57
- label='Color Scheme',
58
- choices=['Vibrant', 'Pastel', 'Monochromatic', 'Black and White'],
59
- value='Vibrant',
60
- multiselect=False,
61
- scale=2
62
- )
63
- angle = gr.Dropdown(
64
- label='Angle',
65
- choices=['Front', 'Side', 'Three-quarter'],
66
- value='Front',
67
- multiselect=False,
68
- scale=2
69
- )
70
- lighting_type = gr.Dropdown(
71
- label='Lighting Type',
72
- choices=['Bright and Even', 'Dramatic Shadows', 'Soft and Warm'],
73
- value='Bright and Even',
74
- multiselect=False,
75
- scale=2
76
- )
77
- additional_details = gr.Textbox(label='Additional Details', scale=2)
78
- submit_prompt = gr.Button('Generate Image', scale=1, variant='primary')
79
-
80
- with gr.Row(variant="panel"):
81
- with gr.Column():
82
- with gr.Row():
83
- input_image = gr.Image(
84
- label="Input Image",
85
- image_mode="RGBA",
86
- sources="upload",
87
- type="pil",
88
- elem_id="content_image",
89
- )
90
- processed_image = gr.Image(
91
- label="Processed Image",
92
- image_mode="RGBA",
93
- type="pil",
94
- interactive=False
95
- )
96
- with gr.Row():
97
- submit_process = gr.Button(
98
- "Remove Background", elem_id="process", variant="primary")
99
- with gr.Row(variant="panel"):
100
- gr.Examples(
101
- examples=[os.path.join("examples/images", img_name)
102
- for img_name in sorted(os.listdir("examples/images"))],
103
- inputs=[input_image],
104
- label="Examples",
105
- cache_examples=False,
106
- examples_per_page=16
107
- )
108
-
109
- submit_prompt.click(fn=generate_image, inputs=[subject, style, color_scheme, angle, lighting_type, additional_details], outputs=input_image).success(
110
- fn=preprocess, inputs=[input_image], outputs=[processed_image]
111
- )
112
- submit_process.click(fn=check_input_image, inputs=[input_image]).success(
113
- fn=preprocess, inputs=[input_image], outputs=[processed_image],
114
- )
115
-
116
- return input_image, processed_image
 
1
+ import os
2
+
3
+ import gradio as gr
4
+ import rembg
5
+ import spaces
6
+ import torch
7
+ from diffusers import DiffusionPipeline
8
+
9
+ from instantMesh.src.utils.infer_util import (
10
+ remove_background, resize_foreground)
11
+
12
+
13
+ pipe = DiffusionPipeline.from_pretrained(
14
+ "playgroundai/playground-v2.5-1024px-aesthetic",
15
+ torch_dtype=torch.float16,
16
+ variant="fp16"
17
+ ).to("cuda")
18
+
19
+
20
+ def generate_prompt(subject, style, color_scheme, angle, lighting_type, additional_details):
21
+ return f"A 3D cartoon render of {subject}, featuring the entire body and shape, on a transparent background. The style should be {style}, with {color_scheme} colors, emphasizing the essential features and lines. The pose should clearly showcase the full form of the {subject} from a {angle} perspective. Lighting is {lighting_type}, highlighting the volume and depth of the subject. {additional_details}. Output as a high-resolution PNG with no background."
22
+
23
+
24
+ @spaces.GPU
25
+ def generate_image(subject, style, color_scheme, angle, lighting_type, additional_details):
26
+ prompt = generate_prompt(subject, style, color_scheme,
27
+ angle, lighting_type, additional_details)
28
+ results = pipe(prompt, num_inference_steps=25, guidance_scale=7.5)
29
+ return results.images[0]
30
+
31
+
32
+ def check_input_image(input_image):
33
+ if input_image is None:
34
+ raise gr.Error("No image selected!")
35
+
36
+
37
+ def preprocess(input_image):
38
+ rembg_session = rembg.new_session()
39
+
40
+ input_image = remove_background(input_image, rembg_session)
41
+ input_image = resize_foreground(input_image, 0.85)
42
+
43
+ return input_image
44
+
45
+
46
+ def image_generation_ui():
47
+ with gr.Row():
48
+ subject = gr.Textbox(label='Subject', scale=2)
49
+ style = gr.Dropdown(
50
+ label='Style',
51
+ choices=['Pixar-like', 'Disney-esque', 'Anime-inspired'],
52
+ value='Pixar-like',
53
+ multiselect=False,
54
+ scale=2
55
+ )
56
+ color_scheme = gr.Dropdown(
57
+ label='Color Scheme',
58
+ choices=['Vibrant', 'Pastel', 'Monochromatic', 'Black and White'],
59
+ value='Vibrant',
60
+ multiselect=False,
61
+ scale=2
62
+ )
63
+ angle = gr.Dropdown(
64
+ label='Angle',
65
+ choices=['Front', 'Side', 'Three-quarter'],
66
+ value='Front',
67
+ multiselect=False,
68
+ scale=2
69
+ )
70
+ lighting_type = gr.Dropdown(
71
+ label='Lighting Type',
72
+ choices=['Bright and Even', 'Dramatic Shadows', 'Soft and Warm'],
73
+ value='Bright and Even',
74
+ multiselect=False,
75
+ scale=2
76
+ )
77
+ additional_details = gr.Textbox(label='Additional Details', scale=2)
78
+ submit_prompt = gr.Button('Generate Image', scale=1, variant='primary')
79
+
80
+ with gr.Row(variant="panel"):
81
+ with gr.Column():
82
+ with gr.Row():
83
+ input_image = gr.Image(
84
+ label="Input Image",
85
+ image_mode="RGBA",
86
+ sources="upload",
87
+ type="pil",
88
+ elem_id="content_image",
89
+ )
90
+ processed_image = gr.Image(
91
+ label="Processed Image",
92
+ image_mode="RGBA",
93
+ type="pil",
94
+ interactive=False
95
+ )
96
+ with gr.Row():
97
+ submit_process = gr.Button(
98
+ "Remove Background", elem_id="process", variant="primary")
99
+
100
+ submit_prompt.click(fn=generate_image, inputs=[subject, style, color_scheme, angle, lighting_type, additional_details], outputs=input_image).success(
101
+ fn=preprocess, inputs=[input_image], outputs=[processed_image]
102
+ )
103
+ submit_process.click(fn=check_input_image, inputs=[input_image]).success(
104
+ fn=preprocess, inputs=[input_image], outputs=[processed_image],
105
+ )
106
+
107
+ return input_image, processed_image