artificialguybr commited on
Commit
d4f55c7
1 Parent(s): 6962b64

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -88
app.py CHANGED
@@ -3,13 +3,8 @@ import requests
3
  import io
4
  from PIL import Image
5
  import json
6
- import os
7
- import logging
8
- import time
9
- from tqdm import tqdm
10
- from image_processing import downscale_image, limit_colors, resize_image, convert_to_grayscale, convert_to_black_and_white
11
 
12
- # Placeholder class for processed images
13
  class SomeClass:
14
  def __init__(self):
15
  self.images = []
@@ -17,131 +12,63 @@ class SomeClass:
17
  with open('loras.json', 'r') as f:
18
  loras = json.load(f)
19
 
20
- def update_selection(selected_state: gr.SelectData):
21
  selected_lora_index = selected_state.index
22
  selected_lora = loras[selected_lora_index]
23
  new_placeholder = f"Type a prompt for {selected_lora['title']}"
24
  lora_repo = selected_lora["repo"]
25
  updated_text = f"### Selected: [{lora_repo}](https://huggingface.co/{lora_repo}) ✨"
26
- return (
27
- gr.update(placeholder=new_placeholder),
28
- updated_text,
29
- selected_state
30
- )
31
 
32
- def run_lora(prompt, selected_state, pixel_art_options, postprocess_options, progress=gr.Progress(track_tqdm=True)):
33
  selected_lora_index = selected_state.index
34
  selected_lora = loras[selected_lora_index]
35
  api_url = f"https://api-inference.huggingface.co/models/{selected_lora['repo']}"
36
- payload = {
37
- "inputs": f"{prompt} {selected_lora['trigger_word']}",
38
- "parameters": {"negative_prompt": "bad art, ugly, watermark, deformed"},
39
- }
40
  response = requests.post(api_url, json=payload)
41
  if response.status_code == 200:
42
  original_image = Image.open(io.BytesIO(response.content))
43
-
44
  processed = SomeClass()
45
  processed.images = [original_image]
46
-
47
- pixel_art_script = PixelArtScript()
48
- postprocess_script = ScriptPostprocessingUpscale()
49
-
50
- pixel_art_script.postprocess(
51
- processed,
52
- **pixel_art_options
53
- )
54
-
55
- postprocess_script.process(
56
- processed,
57
- **postprocess_options
58
- )
59
-
60
  refined_image = processed.images[-1]
61
-
62
  return original_image, refined_image
63
 
64
- def apply_post_processing(image, image_processing_options):
65
  processed_image = image.copy()
66
-
67
- if image_processing_options['downscale'] > 1:
68
- processed_image = downscale_image(processed_image, image_processing_options['downscale'])
69
-
70
- if image_processing_options['limit_colors']:
71
  processed_image = limit_colors(processed_image)
72
-
73
- if image_processing_options['grayscale']:
74
  processed_image = convert_to_grayscale(processed_image)
75
-
76
- if image_processing_options['black_and_white']:
77
  processed_image = convert_to_black_and_white(processed_image)
78
-
79
  return processed_image
80
 
81
  with gr.Blocks() as app:
82
  title = gr.Markdown("# artificialguybr LoRA portfolio")
83
  description = gr.Markdown("### This is a Pixel Art Generator using SD Loras.")
84
  selected_state = gr.State()
85
-
86
  with gr.Row():
87
- gallery = gr.Gallery(
88
- [(item["image"], item["title"]) for item in loras],
89
- label="LoRA Gallery",
90
- allow_preview=False,
91
- columns=3
92
- )
93
-
94
  with gr.Column():
95
  prompt_title = gr.Markdown("### Click on a LoRA in the gallery to create with it")
96
  selected_info = gr.Markdown("")
97
-
98
  with gr.Row():
99
  prompt = gr.Textbox(label="Prompt", show_label=False, lines=1, max_lines=1, placeholder="Type a prompt after selecting a LoRA")
100
  button = gr.Button("Run")
101
-
102
  result = gr.Image(interactive=False, label="Generated Image")
103
  refined_result = gr.Image(interactive=False, label="Refined Generated Image")
104
-
105
- # New Output for Post-Processed Image
106
  post_processed_result = gr.Image(interactive=False, label="Post-Processed Image")
107
-
108
- # New UI elements for pixel art options
109
- with gr.Row():
110
- pixel_art_options = PixelArtScript().ui(True)
111
- postprocess_options = ScriptPostprocessingUpscale().ui()
112
-
113
- # New UI elements for image processing options
114
  with gr.Row():
115
  downscale = gr.Slider(minimum=1, maximum=10, step=1, label="Downscale")
116
  limit_colors = gr.Checkbox(label="Limit Colors")
117
  grayscale = gr.Checkbox(label="Grayscale")
118
  black_and_white = gr.Checkbox(label="Black and White")
119
-
120
- image_processing_options = {
121
- 'downscale': downscale,
122
- 'limit_colors': limit_colors,
123
- 'grayscale': grayscale,
124
- 'black_and_white': black_and_white
125
- }
126
-
127
  post_process_button = gr.Button("Apply Post-Processing")
128
-
129
- gallery.select(
130
- update_selection,
131
- outputs=[prompt, selected_info, selected_state]
132
- )
133
-
134
- prompt.submit(
135
- fn=run_lora,
136
- inputs=[prompt, selected_state, pixel_art_options, postprocess_options],
137
- outputs=[result, refined_result]
138
- )
139
-
140
- post_process_button.click(
141
- fn=apply_post_processing,
142
- inputs=[refined_result, image_processing_options],
143
- outputs=[post_processed_result]
144
- )
145
 
146
  app.queue(max_size=20, concurrency_count=5)
147
  app.launch()
 
3
  import io
4
  from PIL import Image
5
  import json
6
+ from image_processing import downscale_image, limit_colors, convert_to_grayscale, convert_to_black_and_white
 
 
 
 
7
 
 
8
  class SomeClass:
9
  def __init__(self):
10
  self.images = []
 
12
  with open('loras.json', 'r') as f:
13
  loras = json.load(f)
14
 
15
+ def update_selection(selected_state):
16
  selected_lora_index = selected_state.index
17
  selected_lora = loras[selected_lora_index]
18
  new_placeholder = f"Type a prompt for {selected_lora['title']}"
19
  lora_repo = selected_lora["repo"]
20
  updated_text = f"### Selected: [{lora_repo}](https://huggingface.co/{lora_repo}) ✨"
21
+ return (gr.update(placeholder=new_placeholder), updated_text, selected_state)
 
 
 
 
22
 
23
+ def run_lora(prompt, selected_state, progress=gr.Progress(track_tqdm=True)):
24
  selected_lora_index = selected_state.index
25
  selected_lora = loras[selected_lora_index]
26
  api_url = f"https://api-inference.huggingface.co/models/{selected_lora['repo']}"
27
+ payload = {"inputs": f"{prompt} {selected_lora['trigger_word']}", "parameters": {"negative_prompt": "bad art, ugly, watermark, deformed"}}
 
 
 
28
  response = requests.post(api_url, json=payload)
29
  if response.status_code == 200:
30
  original_image = Image.open(io.BytesIO(response.content))
 
31
  processed = SomeClass()
32
  processed.images = [original_image]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  refined_image = processed.images[-1]
 
34
  return original_image, refined_image
35
 
36
+ def apply_post_processing(image, downscale, limit_colors, grayscale, black_and_white):
37
  processed_image = image.copy()
38
+ if downscale > 1:
39
+ processed_image = downscale_image(processed_image, downscale)
40
+ if limit_colors:
 
 
41
  processed_image = limit_colors(processed_image)
42
+ if grayscale:
 
43
  processed_image = convert_to_grayscale(processed_image)
44
+ if black_and_white:
 
45
  processed_image = convert_to_black_and_white(processed_image)
 
46
  return processed_image
47
 
48
  with gr.Blocks() as app:
49
  title = gr.Markdown("# artificialguybr LoRA portfolio")
50
  description = gr.Markdown("### This is a Pixel Art Generator using SD Loras.")
51
  selected_state = gr.State()
 
52
  with gr.Row():
53
+ gallery = gr.Gallery([(item["image"], item["title"]) for item in loras], label="LoRA Gallery", allow_preview=False, columns=3)
 
 
 
 
 
 
54
  with gr.Column():
55
  prompt_title = gr.Markdown("### Click on a LoRA in the gallery to create with it")
56
  selected_info = gr.Markdown("")
 
57
  with gr.Row():
58
  prompt = gr.Textbox(label="Prompt", show_label=False, lines=1, max_lines=1, placeholder="Type a prompt after selecting a LoRA")
59
  button = gr.Button("Run")
 
60
  result = gr.Image(interactive=False, label="Generated Image")
61
  refined_result = gr.Image(interactive=False, label="Refined Generated Image")
 
 
62
  post_processed_result = gr.Image(interactive=False, label="Post-Processed Image")
 
 
 
 
 
 
 
63
  with gr.Row():
64
  downscale = gr.Slider(minimum=1, maximum=10, step=1, label="Downscale")
65
  limit_colors = gr.Checkbox(label="Limit Colors")
66
  grayscale = gr.Checkbox(label="Grayscale")
67
  black_and_white = gr.Checkbox(label="Black and White")
 
 
 
 
 
 
 
 
68
  post_process_button = gr.Button("Apply Post-Processing")
69
+ gallery.select(update_selection, outputs=[prompt, selected_info, selected_state])
70
+ prompt.submit(fn=run_lora, inputs=[prompt, selected_state], outputs=[result, refined_result])
71
+ post_process_button.click(fn=apply_post_processing, inputs=[refined_result, downscale, limit_colors, grayscale, black_and_white], outputs=[post_processed_result])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
 
73
  app.queue(max_size=20, concurrency_count=5)
74
  app.launch()