ikmalsaid commited on
Commit
0a83ae5
1 Parent(s): 0e92534

Added/Updated files

Browse files
app.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as ui; from modules.engine_inpaint import *
2
+ feature = 'Image Inpainting'
3
+ '''
4
+ _______ _______ _______ _____ _____ _______ __
5
+ | __||_ _|| ___|| |_ | |_ | _ | .---.-.|__|
6
+ |__ | | | | ___|| || || | __ | _ || |
7
+ |_______| |___| |_______||_______||_______||___|___||__||___._||__|
8
+ _____________________________________________________________________
9
+
10
+ Copyright © 2023-2024 Ikmal Said. All rights reserved.
11
+
12
+ This program is the property of Ikmal Said. You may not reproduce
13
+ distribute, or modify this code without the express permission of
14
+ the author, Ikmal Said.
15
+ _____________________________________________________________________
16
+
17
+ '''
18
+ with ui.Blocks(css=css, title=title(feature), theme=theme, analytics_enabled=False) as stella:
19
+
20
+ with ui.Column():
21
+ input_inpaint = ui.ImageEditor(label=ssource, brush=ui.Brush(colors=['#ffffff']), sources=['upload', 'webcam'])
22
+ output_inpaint = ui.Gallery(label=sresults, object_fit="contain", height="60vh")
23
+ prompt_inpaint = ui.Textbox(label="What to include:", show_copy_button=True, placeholder=spholder1)
24
+ negative_inpaint = ui.Textbox(label="What not to include:", show_copy_button=True, placeholder=spholder2)
25
+ number_inpaint = ui.Dropdown(label=snumber, choices=[1,2,3,4], value=4)
26
+ prompt_example = ui.Examples(label="Suggested for you:", examples=['highly detailed face', 'detailed girl face', 'detailed man face', 'detailed hand', 'detailed leg', 'beautiful eyes'], inputs=[prompt_inpaint])
27
+
28
+ with ui.Row():
29
+ stop_inpaint = ui.Button("Cancel", variant="secondary", scale=1)
30
+ clear_i2t = ui.ClearButton(value="Reset", components=[input_inpaint, output_inpaint, prompt_inpaint, negative_inpaint], scale=1)
31
+ t2i_inpaint = ui.Button("Submit", variant="primary", scale=5)
32
+
33
+ process_inpaint = t2i_inpaint.click(fn=quads_inpaint, inputs=[input_inpaint, prompt_inpaint, negative_inpaint, number_inpaint], outputs=[output_inpaint])
34
+ stop_inpaint.click(fn=None, inputs=None, outputs=None, cancels=[process_inpaint])
35
+
36
+ if __name__ == "__main__":
37
+ stella.queue(default_concurrency_limit=100, api_open=True).launch(inbrowser=True)
modules/engine_inpaint.py ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as ui; import requests; from requests.exceptions import Timeout, ConnectionError
2
+ from io import BytesIO; from PIL import Image; import concurrent.futures, time
3
+
4
+ ###################################################################################################################
5
+ # Import from modules
6
+ ###################################################################################################################
7
+
8
+ from modules.service_endpoints import *
9
+ from modules.service_configs import *
10
+
11
+ ###################################################################################################################
12
+ # Image inpaint process
13
+ ###################################################################################################################
14
+
15
+ def inpaint(input_image, input_mask, prompt, negative):
16
+ image_pil = Image.fromarray(input_image)
17
+ mask_pil = Image.fromarray(input_mask)
18
+
19
+ image_bytes = BytesIO()
20
+ mask_bytes = BytesIO()
21
+ image_pil.save(image_bytes, format='PNG')
22
+ mask_pil.save(mask_bytes, format='PNG')
23
+
24
+ print(f"{receive()} -> {prompt}")
25
+
26
+ payload = {
27
+ 'model_version': (None, '1'),
28
+ 'prompt': (None, prompt),
29
+ 'neg_prompt': (None, negative),
30
+ 'inpaint_strength': (None, '0.75'),
31
+ 'cfg': (None, '9.5'),
32
+ 'priority': (None, '1'),
33
+ }
34
+
35
+ data = {
36
+ 'image': ('input_image.png', image_bytes.getvalue(), 'image/png'),
37
+ 'mask': ('input_mask.png', mask_bytes.getvalue(), 'image/png')
38
+ }
39
+
40
+ try:
41
+ response = requests.post(mode['inpaint'], headers=head, data=payload, files=data, timeout=(None, None))
42
+
43
+ if len(response.content) < 0 * 1024:
44
+ print(reject())
45
+ return None
46
+
47
+ print(done())
48
+ return Image.open(BytesIO(response.content))
49
+
50
+ except Timeout:
51
+ print(timeout())
52
+ return None
53
+
54
+ ###################################################################################################################
55
+ # 4 image for each generation
56
+ ###################################################################################################################
57
+
58
+ def quads_inpaint(a, b, c, d, progress=ui.Progress()):
59
+ quantities = int(d)
60
+ result_list = [None] * quantities
61
+ percent = 0
62
+
63
+ with concurrent.futures.ThreadPoolExecutor() as executor:
64
+ futures = []
65
+
66
+ for i in range(quantities):
67
+ future = executor.submit(lambda x: inpaint(a["background"], a["layers"][0], b, c), i)
68
+ futures.append(future)
69
+ multiplier = 0.99 / quantities
70
+ percent += multiplier
71
+ progress(percent, desc=f"Processing image {i + 1} of {quantities}")
72
+ time.sleep(0.25)
73
+
74
+ for i, future in enumerate(futures):
75
+ result = future.result()
76
+ result_list[i] = result
77
+
78
+ successful_results = [result for result in result_list if result is not None]
79
+
80
+ if len(successful_results) < quantities:
81
+ if quantities == 1:
82
+ ui.Warning(message=single_error)
83
+ else:
84
+ ui.Warning(message=quads_error)
85
+ else:
86
+ ui.Info(message=success)
87
+
88
+ return successful_results
modules/service_configs.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as ui; from datetime import datetime; import logging
2
+
3
+ # enable debug
4
+ logging.basicConfig(level=logging.DEBUG)
5
+
6
+ # global theme
7
+ theme = ui.themes.Base(
8
+ font=[ui.themes.GoogleFont('Segoe UI'), 'system-ui', 'sans-serif'],
9
+ text_size=ui.themes.Size(lg="15px", md="15px", sm="15px", xl="15px", xs="15px", xxl="15px", xxs="15px"),
10
+ primary_hue='rose', secondary_hue='rose', neutral_hue='zinc'); css = "footer {visibility: hidden};"
11
+
12
+ # global locale - english
13
+ success = 'That worked successfully!'
14
+ single_error = 'That did not work. Please check and try again.'
15
+ quads_error = 'Some images cannot be processed. Please check and try again.'
16
+ received = 'Request Received'
17
+ timed = 'Request Timeout'
18
+ rejected = 'Request Error/Rejected'
19
+ complete = 'Request Completed'
20
+ liability = 'STELLA can make mistakes and inaccuracies.'
21
+ rights = '© 2023-2024 Ikmal Said. All rights reserved.'
22
+ spholder = 'Imagine your favorite person, places or anything!'
23
+ spholder1 = 'Elements to add into the image!'
24
+ spholder2 = 'Things to get rid of!'
25
+ spholder3 = 'Sprinkle some wonders to the generated prompt!'
26
+ sprompt = 'Generate images of:'
27
+ sprompt1 = 'Based on image, create:'
28
+ smodel = 'Using the AI model:'
29
+ smode = 'Using the mode:'
30
+ sratio = 'In the size of:'
31
+ sstyle = 'Inspired by the style of:'
32
+ squality = 'At a quality level of:'
33
+ snumber = 'With a quantity of:'
34
+ ssource = 'Source'
35
+ sresult = 'Result'
36
+ sresults = 'Results'
37
+
38
+ # global function
39
+ def timestamp(): return f"[{datetime.now().strftime('%d/%m/%y at %H:%M:%S')}]"
40
+ def receive(): return f"{timestamp()} \U0001F680 {received}"
41
+ def timeout(): return f"{timestamp()} \U000023F0 {timed}"
42
+ def reject(): return f"{timestamp()} \U0000274C {rejected}"
43
+ def done(): return f"{timestamp()} \U0001F618 {complete}"
44
+ def header(feature): ui.HTML(f'<center><h4 style="font-size: 1em; margin: 5px 0px 5px">{feature}</h4></center>')
45
+ def footer(): ui.HTML(f'<center><h4 style="font-size: 1em; margin: 5px 0px 0px">{liability}<br></h4>{rights}</center>')
46
+ def title(feature): return f"{feature}"
modules/service_endpoints.py ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ mode = {
4
+ 'inpaint' : os.getenv('inpaint')
5
+ }
6
+
7
+ head = {
8
+ 'bearer' : os.getenv('bearer')
9
+ }