File size: 5,458 Bytes
779590a
 
 
 
 
3885620
4c2aab8
779590a
3885620
 
2799929
779590a
2799929
779590a
 
2799929
3885620
cc293c4
 
243381c
2d874ee
3885620
cc293c4
 
c297eeb
cc293c4
243381c
3885620
2799929
cc293c4
779590a
 
 
 
2799929
3885620
cc293c4
 
 
2d874ee
3885620
cc293c4
 
c297eeb
cc293c4
2799929
3885620
2799929
cc293c4
2799929
 
779590a
 
2799929
779590a
 
2799929
3885620
779590a
 
3885620
 
779590a
 
 
 
cc293c4
2799929
779590a
2799929
779590a
3885620
779590a
cc293c4
243381c
2799929
9572b9a
3885620
243381c
 
2799929
 
 
 
 
 
243381c
 
2799929
 
 
3885620
 
 
 
cc293c4
2799929
 
3885620
 
2799929
3885620
2799929
 
 
 
3885620
2799929
 
 
 
 
c297eeb
 
 
 
 
 
 
 
2799929
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3885620
 
f830017
3885620
f830017
 
3885620
2799929
 
cc293c4
 
 
2d874ee
3885620
cc293c4
c297eeb
 
cc293c4
243381c
3885620
2799929
cc293c4
 
779590a
 
cc293c4
2799929
779590a
 
 
2799929
779590a
 
 
 
 
3885620
779590a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import gradio as gr
import spaces
import torch
from model import ModelHandler
from generator import Generator
# --- IMPORT CONFIG ---
from config import Config

# 1. Initialize Models Globally (in RAM)
# ZeroGPU will move them to VRAM inside the @spaces.GPU function
print("Initializing Application...")
handler = ModelHandler()
handler.load_models()
gen = Generator(handler)

# 2. Define GPU-enabled Inference Function
@spaces.GPU(duration=20) # <-- MODIFIED
def process_img(
    image, 
    prompt,
    negative_prompt,
    cfg_scale, 
    steps, 
    img_strength, 
    face_strength,
    depth_strength, 
    edge_strength,
    # tile_strength,     # <-- REMOVED
    seed
):
    if image is None:
        raise gr.Error("Please upload an image first.")
    
    try:
        print("--- Starting Generation ---")
        # Pass all parameters to the generator
        result = gen.predict(
            image, 
            prompt,
            negative_prompt=negative_prompt,
            guidance_scale=cfg_scale,
            num_inference_steps=steps,
            img2img_strength=img_strength,
            face_strength=face_strength,
            depth_strength=depth_strength,
            lineart_strength=edge_strength,
            # tile_strength=tile_strength,      # <-- REMOVED
            seed=seed
        )
        print("--- Generation Complete ---")
        return result
        
    except Exception as e:
        print(f"Error during generation: {e}")
        raise gr.Error(f"An error occurred: {str(e)}")

# 3. Build Gradio Interface
with gr.Blocks(title="Face To Pixel Art", theme=gr.themes.Soft()) as demo:
    gr.Markdown(
        """
        # 🎮 Face to Pixel Art
        Upload any image. If there is a face, we'll keep the identity. If not, we'll pixelate the scene!
        """
    )
    
    with gr.Row():
        with gr.Column(scale=2):
            input_img = gr.Image(type="pil", label="Input Image")
            prompt = gr.Textbox(
                label="Prompt (Optional)", 
                placeholder="Leave empty for auto-captioning...",
                info="The trigger words 'p1x3l4rt, pixel art' are added automatically."
            )
            
            negative_prompt = gr.Textbox(
                label="Negative Prompt (Optional)", 
                placeholder="e.g., blurry, text, watermark, bad art...",
                value=Config.DEFAULT_NEGATIVE_PROMPT # <-- MODIFIED
            )
            
            with gr.Accordion("Advanced Settings", open=False):
                seed = gr.Number(
                    label="Seed", 
                    value=-1, 
                    info="-1 for random", 
                    precision=0
                )
                
                cfg_scale = gr.Slider(
                    elem_id="cfg_scale",
                    minimum=1.0, 
                    maximum=5.0, 
                    step=0.1, 
                    value=Config.CGF_SCALE, 
                    label="CFG Scale"
                )
                steps = gr.Slider(
                    elem_id="steps",
                    minimum=4, 
                    maximum=20, 
                    step=1, 
                    value=Config.STEPS_NUMBER, 
                    label="Steps Number"
                )
                img_strength = gr.Slider(
                    elem_id="img_strength",
                    minimum=0.1, 
                    maximum=1.0, 
                    step=0.05, 
                    value=Config.IMG_STRENGTH, 
                    label="Image Strength (Img2Img)"
                )
                face_strength = gr.Slider(
                    elem_id="face_strength",
                    minimum=0.0, 
                    maximum=1.0, 
                    step=0.05, 
                    value=Config.FACE_STRENGTH, 
                    label="Face Strength"
                )
                depth_strength = gr.Slider(
                    elem_id="depth_strength",
                    minimum=0.0, 
                    maximum=1.0, 
                    step=0.05, 
                    value=Config.DEPTH_STRENGTH, 
                    label="DepthMap Strength"
                )
                edge_strength = gr.Slider(
                    elem_id="edge_strength",
                    minimum=0.0, 
                    maximum=1.0, 
                    step=0.05, 
                    value=Config.EDGE_STRENGTH, 
                    label="EdgeMap Strength (LineArt)"
                )
                # --- MODIFIED: Renamed slider ---
                # tile_strength = gr.Slider(...) # <-- REMOVED
            
            run_btn = gr.Button("Generate Pixel Art", variant="primary")
            
        with gr.Column(scale=1):
            output_img = gr.Image(label="Pixel Art Result")
                
    # Event Handler
    all_inputs = [
        input_img, 
        prompt, 
        negative_prompt,
        cfg_scale, 
        steps, 
        img_strength,
        face_strength,
        depth_strength, 
        edge_strength,
        # tile_strength,     # <-- REMOVED
        seed
    ]
    
    run_btn.click(
        fn=process_img, 
        inputs=all_inputs, 
        outputs=[output_img]
    )


# 4. Launch the App
if __name__ == "__main__":
    demo.queue(max_size=20, api_open=True)
    demo.launch(
        server_name="0.0.0.0",
        server_port=7860,
        show_api=True # share=True is not needed on Spaces
    )