ikmalsaid commited on
Commit
b9b236f
1 Parent(s): 8f36b8e

added/updated app files

Browse files
Files changed (5) hide show
  1. README.md +1 -1
  2. app.py +0 -37
  3. app_eraser.py +34 -0
  4. modules/engine_eraser.py +7 -13
  5. modules/service_configs.py +241 -38
README.md CHANGED
@@ -5,7 +5,7 @@ colorFrom: indigo
5
  colorTo: indigo
6
  sdk: gradio
7
  sdk_version: 4.20.1
8
- app_file: app.py
9
  pinned: false
10
  license: agpl-3.0
11
  ---
 
5
  colorTo: indigo
6
  sdk: gradio
7
  sdk_version: 4.20.1
8
+ app_file: app_eraser.py
9
  pinned: false
10
  license: agpl-3.0
11
  ---
app.py DELETED
@@ -1,37 +0,0 @@
1
- import gradio as ui; from modules.engine_eraser import *
2
- feature = 'Image Eraser'
3
- '''
4
- _____________________________________________________________________
5
-
6
- Copyright © 2023-2024 Ikmal Said. All rights reserved.
7
-
8
- This program is the property of Ikmal Said. You may not reproduce
9
- distribute, or modify this code without the express permission of
10
- the author, Ikmal Said.
11
- _____________________________________________________________________
12
-
13
- '''
14
- with ui.Blocks(css=css, title=title(feature), theme=theme, analytics_enabled=False) as stella:
15
- with ui.Tabs() as tabs_eraser:
16
- with ui.TabItem(title(feature), id=0):
17
- with ui.Group():
18
- input_eraser = ui.ImageEditor(
19
- show_label=False, transforms=[],
20
- brush=ui.Brush(default_size=24, colors=['#ffffff'], color_mode=['fixed']),
21
- eraser=ui.Eraser(default_size=24), sources=['upload', 'webcam'])
22
-
23
- with ui.Row():
24
- submit_eraser = ui.Button("Submit", variant="primary")
25
-
26
- with ui.TabItem('Result', id=1):
27
- with ui.Group():
28
- output_eraser = ui.Gallery(preview=True, show_label=False, object_fit="cover", columns=1, rows=1, show_share_button=False)
29
-
30
- with ui.Row():
31
- clear_eraser = ui.ClearButton(value="Reset", components=[input_eraser, output_eraser])
32
-
33
- process_eraser = submit_eraser.click(fn=eraser2, inputs=[input_eraser], outputs=[tabs_eraser, input_eraser, output_eraser])
34
- clear_eraser.click(fn=reset, inputs=None, outputs=tabs_eraser)
35
-
36
- if __name__ == "__main__":
37
- stella.queue(default_concurrency_limit=100, api_open=True).launch(inbrowser=True, favicon_path="favicon.ico")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app_eraser.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from modules.engine_eraser import *
2
+ feature = 'Image Eraser'
3
+ '''
4
+ _____________________________________________________________________
5
+
6
+ Copyright © 2023-2024 Ikmal Said. All rights reserved.
7
+
8
+ This program is the property of Ikmal Said. You may not reproduce
9
+ distribute, or modify this code without the express permission of
10
+ the author, Ikmal Said.
11
+ _____________________________________________________________________
12
+
13
+ '''
14
+ with ui.Blocks(css=adj+css, title=title(feature), theme=theme, analytics_enabled=False, head=jsx) as stella:
15
+ with ui.Group():
16
+ input_eraser = ui.ImageEditor(
17
+ label=title(feature), transforms=[],
18
+ brush=ui.Brush(default_size=24, colors=['#ffffff'], color_mode=['fixed']),
19
+ eraser=ui.Eraser(default_size=24), sources=['upload', 'webcam'])
20
+
21
+ output_eraser = ui.Gallery(preview=True, show_label=False, object_fit="cover", columns=1, rows=1, height="50vh", show_share_button=False, visible=False)
22
+
23
+ with ui.Row():
24
+ clear_eraser = ui.ClearButton(value="Reset", components=[input_eraser, output_eraser])
25
+ submit_eraser = ui.Button("Submit", variant="primary")
26
+ retry_eraser = ui.Button("Try Again", variant="primary", visible=False)
27
+
28
+ process_eraser = submit_eraser.click(fn=submit, inputs=[input_eraser], outputs=[input_eraser, output_eraser, submit_eraser, retry_eraser, output_eraser])
29
+ clear_eraser.click(fn=reset, inputs=None, outputs=[input_eraser, output_eraser, submit_eraser, retry_eraser])
30
+ retry_eraser.click(fn=reset, inputs=None, outputs=[input_eraser, output_eraser, submit_eraser, retry_eraser])
31
+
32
+
33
+ if __name__ == "__main__":
34
+ stella.queue(default_concurrency_limit=100).launch(inbrowser=True, favicon_path="favicon.ico")
modules/engine_eraser.py CHANGED
@@ -1,17 +1,9 @@
1
- import gradio as ui; import requests; from requests.exceptions import Timeout
2
  from io import BytesIO; from PIL import Image;
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_img, progress=ui.Progress()):
16
  image_pil = Image.fromarray(input_img["background"])
17
  mask_pil = Image.fromarray(input_img["layers"][0])
@@ -34,7 +26,7 @@ def eraser(input_img, progress=ui.Progress()):
34
  }
35
 
36
  try:
37
- progress(0.99, desc="Processing image...")
38
  response = requests.post(mode['eraser'], headers=head, data=payload, files=data, timeout=(None, None))
39
 
40
  if len(response.content) < 65 * 1024:
@@ -53,8 +45,10 @@ def eraser(input_img, progress=ui.Progress()):
53
  ui.Warning(message=single_error)
54
  return None
55
 
56
- def eraser2(a):
57
- return ui.Tabs(selected=1), a, eraser(a)
 
58
 
59
  def reset():
60
- return ui.Tabs(selected=0)
 
 
1
+ import requests; from requests.exceptions import Timeout
2
  from io import BytesIO; from PIL import Image;
3
 
 
 
 
 
4
  from modules.service_endpoints import *
5
  from modules.service_configs import *
6
 
 
 
 
 
7
  def eraser(input_img, progress=ui.Progress()):
8
  image_pil = Image.fromarray(input_img["background"])
9
  mask_pil = Image.fromarray(input_img["layers"][0])
 
26
  }
27
 
28
  try:
29
+ progress(0.95, desc="Processing image...")
30
  response = requests.post(mode['eraser'], headers=head, data=payload, files=data, timeout=(None, None))
31
 
32
  if len(response.content) < 65 * 1024:
 
45
  ui.Warning(message=single_error)
46
  return None
47
 
48
+ def submit(a):
49
+ # input, output, submit, try again, eraser
50
+ return ui.ImageEditor(visible=False), ui.Gallery(visible=True), ui.Button(visible=False), ui.Button(visible=True), eraser(a)
51
 
52
  def reset():
53
+ # input, output, submit, try again
54
+ return ui.ImageEditor(visible=True), ui.Gallery(visible=False), ui.Button(visible=True), ui.Button(visible=False)
modules/service_configs.py CHANGED
@@ -1,53 +1,23 @@
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.Default(
8
- font=[ui.themes.GoogleFont('Roboto Condensed'), 'Arial'],
9
- text_size=ui.themes.Size(lg="16px", md="16px", sm="16px", xl="16px", xs="16px", xxl="16px", xxs="16px"),
10
  primary_hue='rose', secondary_hue='rose', neutral_hue='zinc', radius_size=ui.themes.sizes.radius_none)
11
 
12
- css = '''
13
- footer {
14
- display: none !important;
15
- }
16
-
17
- div.svelte-vt1mxs>*, div.svelte-vt1mxs>.form > * {
18
- border: none;
19
- }
20
-
21
- .app.svelte-182fdeq.svelte-182fdeq {
22
- padding: 0px !important;
23
- }
24
-
25
- .grid-wrap.svelte-hpz95u.svelte-hpz95u {
26
- overflow-y: auto !important;
27
- }
28
-
29
- .selected.svelte-1uw5tnk {
30
- background: #262629 !important;
31
- }
32
-
33
- .closed.svelte-g7pfx4>button.svelte-g7pfx4 {
34
- display: none !important;
35
- }
36
-
37
- gradio-app {
38
- background: none !important;
39
- }
40
-
41
- input.svelte-1f354aw.svelte-1f354aw, textarea.svelte-1f354aw.svelte-1f354aw {
42
- text-align: justify !important;
43
- }
44
- '''
45
-
46
  # global locale - english
47
  success = 'That worked successfully!'
48
  single_error = 'That did not work!'
49
  quads_error = 'Some images cannot be processed!'
50
  empty_error = 'Prompt field cannot be empty!'
 
 
 
51
  received = 'Request Received'
52
  timed = 'Request Timeout'
53
  rejected = 'Request Error/Rejected'
@@ -60,12 +30,14 @@ spholder2 = 'Things to get rid of!'
60
  spholder3 = 'Put wonders into the generated prompt!'
61
  sprompt = 'Generate images of:'
62
  sprompt1 = 'Based on image, create:'
 
63
  smodel = 'Using the AI model:'
64
  smode = 'Using the mode:'
65
  sratio = 'In the size of:'
66
  sstyle = 'Inspired by the style of:'
67
  squality = 'At a quality level of:'
68
  snumber = 'With a quantity of:'
 
69
 
70
  # global function
71
  def timestamp(): return f"[{datetime.now().strftime('%d/%m/%y at %H:%M:%S')}]"
@@ -75,4 +47,235 @@ def reject(): return f"{timestamp()} \U0000274C {rejected}"
75
  def done(): return f"{timestamp()} \U0001F618 {complete}"
76
  def header(feature): ui.HTML(f'<center><h4 style="font-size: 1em; margin: 5px 0px 5px">{feature}</h4></center>')
77
  def footer(): ui.HTML(f'<center><h4 style="font-size: 1em; margin: 5px 0px 0px">{liability}<br></h4>{rights}</center>')
78
- def title(feature): return f"{feature}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as ui; from datetime import datetime; import logging, requests, json, random
2
+ from modules.service_endpoints import *; import numpy as np
3
 
4
  # enable debug
5
  logging.basicConfig(level=logging.DEBUG)
6
 
7
  # global theme
8
  theme = ui.themes.Default(
9
+ font=[ui.themes.GoogleFont('IBM Plex Sans Condensed')], font_mono=[ui.themes.GoogleFont('IBM Plex Sans Condensed')],
10
+ text_size=ui.themes.Size(lg="18px", md="18px", sm="18px", xl="18px", xs="18px", xxl="18px", xxs="18px"),
11
  primary_hue='rose', secondary_hue='rose', neutral_hue='zinc', radius_size=ui.themes.sizes.radius_none)
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  # global locale - english
14
  success = 'That worked successfully!'
15
  single_error = 'That did not work!'
16
  quads_error = 'Some images cannot be processed!'
17
  empty_error = 'Prompt field cannot be empty!'
18
+ pf_error = 'Prompt fusion error. Proceeding...'
19
+ pe_error = 'Prompt expansion error. Proceeding...'
20
+ pe_cmd = '(The prompt must intricately describe every part of the image in concrete, objective detail. THINK about what the end goal of the description is, and extrapolate that to what would make satisfying images.)'
21
  received = 'Request Received'
22
  timed = 'Request Timeout'
23
  rejected = 'Request Error/Rejected'
 
30
  spholder3 = 'Put wonders into the generated prompt!'
31
  sprompt = 'Generate images of:'
32
  sprompt1 = 'Based on image, create:'
33
+ sprompt2 = 'Based on image, it shows:'
34
  smodel = 'Using the AI model:'
35
  smode = 'Using the mode:'
36
  sratio = 'In the size of:'
37
  sstyle = 'Inspired by the style of:'
38
  squality = 'At a quality level of:'
39
  snumber = 'With a quantity of:'
40
+ ssmart = 'Using SmartPrompt™ mode:'
41
 
42
  # global function
43
  def timestamp(): return f"[{datetime.now().strftime('%d/%m/%y at %H:%M:%S')}]"
 
47
  def done(): return f"{timestamp()} \U0001F618 {complete}"
48
  def header(feature): ui.HTML(f'<center><h4 style="font-size: 1em; margin: 5px 0px 5px">{feature}</h4></center>')
49
  def footer(): ui.HTML(f'<center><h4 style="font-size: 1em; margin: 5px 0px 0px">{liability}<br></h4>{rights}</center>')
50
+ def title(feature): return f"{feature}"
51
+
52
+ # seed generator
53
+ max_seed = np.iinfo(np.int32).max
54
+
55
+ def seeds(a, i: ui.SelectData):
56
+ print(f"Seed mode: {a}")
57
+ if i.value == 'Fixed': return ui.Slider(value=random.randint(0, max_seed), visible=True)
58
+ else: return ui.Slider(visible=False)
59
+
60
+ # prompt fusion to en
61
+ def translate(fusion, progress=ui.Progress()):
62
+ progress(0.05, desc="Initiating fusion")
63
+ try: print(f"{receive()} -> {fusion}"); return requests.get(f"{mode['fusion']}{fusion}", timeout=15).json()[0][0]
64
+ except Exception as e: print(pf_error, e); return fusion
65
+
66
+ # prompt expander
67
+ def expand(expand, progress=ui.Progress()):
68
+ progress(0.10, desc="Initiating expansion")
69
+ # return str(input)
70
+ head = {'Content-Type': 'application/json'}
71
+ body = json.dumps({"sentence": f"{expand} {pe_cmd}", "target_word_count": 30})
72
+ # body = json.dumps({"topic": input, "word_count": 30, "writing_mode": "Simple", "story_genre": "Descriptive"}) # ['story']
73
+ try: print(f"{receive()} -> {expand}"); return requests.post(url=mode['expander'], headers=head, data=body, timeout=15).json()['expanded_sentence']
74
+ except Exception as e: print(pe_error, e); return expand
75
+
76
+ # global head script
77
+ jsx = '''
78
+ <script>
79
+ window.onbeforeunload = function (event) {
80
+ event.returnValue = "Are you sure you want to leave?";
81
+ };
82
+ </script>
83
+ '''
84
+
85
+ # specific css modifiers
86
+ adj = '''
87
+ /* remove layers button */
88
+ .layer-wrap.svelte-g7pfx4.svelte-g7pfx4 {
89
+ display: none !important;
90
+ }
91
+ '''
92
+
93
+ adj2 = '''
94
+ .grid-wrap.svelte-hpz95u.svelte-hpz95u {
95
+ height: 49.8vh;
96
+ }
97
+ '''
98
+
99
+ # global css modifiers
100
+ css = '''
101
+ /* remove scrollbars */
102
+ ::-webkit-scrollbar {
103
+ display: none;
104
+ }
105
+
106
+ ::-webkit-scrollbar-button {
107
+ display: none;
108
+ }
109
+
110
+ body {
111
+ -ms-overflow-style: none;
112
+ }
113
+
114
+ /* remove footer */
115
+ footer {
116
+ display: none !important;
117
+ }
118
+
119
+ /* remove all padding */
120
+ .app.svelte-182fdeq.svelte-182fdeq {
121
+ padding: 0px;
122
+ }
123
+
124
+ /* remove scroll bar in gallery */
125
+ .grid-wrap.svelte-hpz95u.svelte-hpz95u {
126
+ overflow-y: auto;
127
+ }
128
+
129
+ /* remove background */
130
+ gradio-app {
131
+ background: none !important;
132
+ }
133
+
134
+ /* remove borders for tabs*/
135
+ div.svelte-iyf88w {
136
+ border: 0px;
137
+ }
138
+
139
+ /* remove padding for tabs */
140
+ div.svelte-19hvt5v {
141
+ padding: 0px;
142
+ border: 0px;
143
+ }
144
+
145
+ /* remove borders for tab button bottom*/
146
+ .tab-nav.svelte-1uw5tnk {
147
+ border: 0px;
148
+ }
149
+
150
+ /* remove borders for tab button*/
151
+ button.svelte-1uw5tnk {
152
+ border: 0px;
153
+ }
154
+
155
+ /* modify container padding and bg */
156
+ .hide-container.svelte-90oupt {
157
+ padding: var(--block-padding);
158
+ background: var(--block-background-fill);
159
+ }
160
+
161
+ /* set brush to red */
162
+ span.svelte-btgkrd {
163
+ background: rgb(255 0 0 / 60%);
164
+ }
165
+
166
+ /* remove example symbol */
167
+ svg.svelte-13hsdno.svelte-13hsdno.svelte-13hsdno {
168
+ display: none;
169
+ }
170
+
171
+ /* replace selected tab color */
172
+ .selected.svelte-1uw5tnk {
173
+ background: #27272a;
174
+ }
175
+
176
+ /* justify textbox */
177
+ input.svelte-1f354aw.svelte-1f354aw,
178
+ textarea.svelte-1f354aw.svelte-1f354aw {
179
+ text-align: justify;
180
+ }
181
+
182
+ /* modify feature text */
183
+ label.float.svelte-1b6s6s {
184
+ padding-left: 10px;
185
+ padding-top: 6px;
186
+ }
187
+
188
+ /* change feature background */
189
+ label.svelte-1b6s6s {
190
+ background: #27272a;
191
+ }
192
+
193
+ /* remove feature icon */
194
+ span.svelte-1b6s6s {
195
+ display: none;
196
+ }
197
+
198
+ /* remove options line */
199
+ div.svelte-sfqy0y {
200
+ background: none;
201
+ }
202
+
203
+ /* modify layer button */
204
+ .layer-wrap.svelte-g7pfx4.svelte-g7pfx4 {
205
+ background: #27272a;
206
+ }
207
+
208
+ /* modify slider color */
209
+ .dark, .light {
210
+ --slider-color: var(--color-accent);
211
+ }
212
+
213
+ /* chanhe num input padding and height */
214
+ input[type="number"].svelte-pjtc3.svelte-pjtc3 {
215
+ padding: var(--checkbox-label-padding);
216
+ height: 45px;
217
+ }
218
+
219
+ /* disable anti-aliasing on gallery thumb */
220
+ .thumbnail-lg.svelte-hpz95u>img {
221
+ image-rendering: auto;
222
+ }
223
+
224
+ /* disable anti-aliasing on gallery view */
225
+ .image-button.svelte-hpz95u img {
226
+ image-rendering: auto;
227
+ }
228
+
229
+ /* make gallery buttons bigger */
230
+ .icon-buttons.svelte-hpz95u.svelte-hpz95u {
231
+ scale: 2;
232
+ padding-top: 8px;
233
+ padding-right: 15px;
234
+ }
235
+
236
+ /* make img clear btn bigger */
237
+ div.svelte-s6ybro {
238
+ scale: 2;
239
+ padding-right: 5px;
240
+ padding-top: 5px;
241
+ }
242
+
243
+ /* make img dload btn bigger */
244
+ .icon-buttons.svelte-1l6wqyv {
245
+ top: 8px;
246
+ right: 8px;
247
+ scale: 2;
248
+ padding-right: 5px;
249
+ padding-top: 5px;
250
+ }
251
+
252
+ /* remove bg completely */
253
+ body {
254
+ background: none !important;
255
+ }
256
+
257
+ /* wide mode */
258
+ .app.svelte-182fdeq.svelte-182fdeq {
259
+ max-width: 100% !important;
260
+ }
261
+
262
+ /* make prompt consistant */
263
+ label.svelte-1f354aw.svelte-1f354aw {
264
+ padding: 12px;
265
+ }
266
+
267
+ /* remove accord label */
268
+ .label-wrap.open.svelte-s1r2yt {
269
+ display: none;
270
+ }
271
+
272
+ /* make empty gallery taller */
273
+ .unpadded_box.svelte-1oiin9d {
274
+ min-height: 50vh;
275
+ }
276
+
277
+ /* make image contain */
278
+ .image-frame.svelte-rrgd5g img {
279
+ object-fit: contain;
280
+ }
281
+ '''