ikmalsaid commited on
Commit
30d30ad
1 Parent(s): d945d63

Added/Updated files

Browse files
app.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as ui; from modules.engine_eraser import *
2
+ feature = 'Image Eraser'
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_eraser = ui.ImageEditor(label=ssource, brush=ui.Brush(colors=['#ffffff']), sources=['upload'], transforms=[])
22
+ output_eraser = ui.Gallery(preview=True, label=sresult, object_fit="contain", columns=1, rows=1, height="60vh")
23
+
24
+ with ui.Row():
25
+ stop_eraser = ui.Button("Cancel", variant="secondary", scale=1)
26
+ clear_i2t = ui.ClearButton(value="Reset", components=[input_eraser, output_eraser], scale=1)
27
+ t2i_eraser = ui.Button("Submit", variant="primary", scale=3)
28
+
29
+ process_eraser = t2i_eraser.click(fn=quads_eraser, inputs=[input_eraser], outputs=[output_eraser])
30
+ stop_eraser.click(fn=None, inputs=None, outputs=None, cancels=[process_eraser])
31
+
32
+ if __name__ == "__main__":
33
+ stella.queue(default_concurrency_limit=100, api_open=True).launch(inbrowser=True)
modules/engine_eraser.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as ui; import requests; from requests.exceptions import Timeout
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 erase process
13
+ ###################################################################################################################
14
+
15
+ def eraser(input_image, input_mask):
16
+ image_pil = Image.fromarray(input_image)
17
+ mask_pil = Image.fromarray(input_mask)
18
+ image_bytes = BytesIO()
19
+ mask_bytes = BytesIO()
20
+ image_pil.save(image_bytes, format='PNG')
21
+ mask_pil.save(mask_bytes, format='PNG')
22
+
23
+ print(receive())
24
+
25
+ payload = {
26
+ 'model_version': (None, '1'),
27
+ 'cfg': (None, '9.5'),
28
+ 'priority': (None, '1'),
29
+ }
30
+
31
+ data = {
32
+ 'image': ('input_image.png', image_bytes.getvalue(), 'image/png'),
33
+ 'mask': ('input_mask.png', mask_bytes.getvalue(), 'image/png')
34
+ }
35
+
36
+ try:
37
+ response = requests.post(mode['eraser'], headers=head, data=payload, files=data, timeout=(None, None))
38
+
39
+ if len(response.content) < 65 * 1024:
40
+ print(reject())
41
+ return None
42
+
43
+ print(done())
44
+ return Image.open(BytesIO(response.content))
45
+
46
+ except Timeout:
47
+ print(timeout())
48
+ return None
49
+
50
+ ###################################################################################################################
51
+ # 1 image for each generation
52
+ ###################################################################################################################
53
+
54
+ def quads_eraser(a, progress=ui.Progress()):
55
+ quantities = 1
56
+ result_list = [None] * quantities
57
+ percent = 0
58
+
59
+ with concurrent.futures.ThreadPoolExecutor() as executor:
60
+ futures = []
61
+
62
+ for i in range(quantities):
63
+ future = executor.submit(lambda x: eraser(a["background"], a["layers"][0]), i)
64
+ futures.append(future)
65
+ multiplier = 0.99 / quantities
66
+ percent += multiplier
67
+ progress(percent, desc=f"Processing image {i + 1} of {quantities}")
68
+ time.sleep(0.25)
69
+
70
+ for i, future in enumerate(futures):
71
+ result = future.result()
72
+ result_list[i] = result
73
+
74
+ successful_results = [result for result in result_list if result is not None]
75
+
76
+ if len(successful_results) < quantities:
77
+ if quantities == 1:
78
+ ui.Warning(message=single_error)
79
+ else:
80
+ ui.Warning(message=quads_error)
81
+ else:
82
+ ui.Info(message=success)
83
+
84
+ 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
+ 'eraser' : os.getenv('eraser')
5
+ }
6
+
7
+ head = {
8
+ 'bearer' : os.getenv('bearer')
9
+ }