artificialguybr commited on
Commit
07b8c5e
1 Parent(s): fa4bc6a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +115 -0
app.py ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ 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
+
11
+ # Import additional modules
12
+ from pixelart import Script as PixelArtScript
13
+ from postprocessing_pixelart import ScriptPostprocessingUpscale
14
+ from utils import downscale_image, limit_colors, resize_image, convert_to_grayscale, convert_to_black_and_white
15
+
16
+ # Placeholder class for processed images
17
+ class SomeClass:
18
+ def __init__(self):
19
+ self.images = []
20
+
21
+ with open('loras.json', 'r') as f:
22
+ loras = json.load(f)
23
+
24
+ def update_selection(selected_state: gr.SelectData):
25
+ selected_lora_index = selected_state.index
26
+ selected_lora = loras[selected_lora_index]
27
+ new_placeholder = f"Type a prompt for {selected_lora['title']}"
28
+ lora_repo = selected_lora["repo"]
29
+ updated_text = f"### Selected: [{lora_repo}](https://huggingface.co/{lora_repo}) ✨"
30
+ return (
31
+ gr.update(placeholder=new_placeholder),
32
+ updated_text,
33
+ selected_state
34
+ )
35
+
36
+ def run_lora(prompt, selected_state, pixel_art_options, postprocess_options, progress=gr.Progress(track_tqdm=True)):
37
+ selected_lora_index = selected_state.index
38
+ selected_lora = loras[selected_lora_index]
39
+ api_url = f"https://api-inference.huggingface.co/models/{selected_lora['repo']}"
40
+ payload = {
41
+ "inputs": f"{prompt} {selected_lora['trigger_word']}",
42
+ "parameters": {"negative_prompt": "bad art, ugly, watermark, deformed"},
43
+ }
44
+ response = requests.post(api_url, json=payload)
45
+ if response.status_code == 200:
46
+ original_image = Image.open(io.BytesIO(response.content))
47
+
48
+ processed = SomeClass()
49
+ processed.images = [original_image]
50
+
51
+ pixel_art_script = PixelArtScript()
52
+ postprocess_script = ScriptPostprocessingUpscale()
53
+
54
+ pixel_art_script.postprocess(
55
+ processed,
56
+ **pixel_art_options
57
+ )
58
+
59
+ postprocess_script.process(
60
+ processed,
61
+ **postprocess_options
62
+ )
63
+
64
+ refined_image = processed.images[-1]
65
+
66
+ return original_image, refined_image
67
+
68
+ with gr.Blocks() as app:
69
+ title = gr.Markdown("# artificialguybr LoRA portfolio")
70
+ description = gr.Markdown("### This is a Pixel Art Generator using SD Loras.")
71
+ selected_state = gr.State()
72
+
73
+ with gr.Row():
74
+ gallery = gr.Gallery(
75
+ [(item["image"], item["title"]) for item in loras],
76
+ label="LoRA Gallery",
77
+ allow_preview=False,
78
+ columns=3
79
+ )
80
+
81
+ with gr.Column():
82
+ prompt_title = gr.Markdown("### Click on a LoRA in the gallery to create with it")
83
+ selected_info = gr.Markdown("")
84
+
85
+ with gr.Row():
86
+ prompt = gr.Textbox(label="Prompt", show_label=False, lines=1, max_lines=1, placeholder="Type a prompt after selecting a LoRA")
87
+ button = gr.Button("Run")
88
+
89
+ result = gr.Image(interactive=False, label="Generated Image")
90
+ refined_result = gr.Image(interactive=False, label="Refined Generated Image") # New Output
91
+
92
+ # New UI elements for pixel art options
93
+ with gr.Row():
94
+ pixel_art_options = PixelArtScript().ui(True)
95
+ postprocess_options = ScriptPostprocessingUpscale().ui()
96
+
97
+ gallery.select(
98
+ update_selection,
99
+ outputs=[prompt, selected_info, selected_state]
100
+ )
101
+
102
+ prompt.submit(
103
+ fn=run_lora,
104
+ inputs=[prompt, selected_state, pixel_art_options, postprocess_options],
105
+ outputs=[result, refined_result]
106
+ )
107
+
108
+ button.click(
109
+ fn=run_lora,
110
+ inputs=[prompt, selected_state, pixel_art_options, postprocess_options],
111
+ outputs=[result, refined_result]
112
+ )
113
+
114
+ app.queue(max_size=20, concurrency_count=5)
115
+ app.launch()