primerz commited on
Commit
a62ec62
·
verified ·
1 Parent(s): 6977800

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -30
app.py CHANGED
@@ -3,28 +3,25 @@ import spaces
3
  import torch
4
  from model import ModelHandler
5
  from generator import Generator
6
- # --- IMPORT CONFIG ---
7
  from config import Config
8
 
9
- # 1. Initialize Models Globally (in RAM)
10
- # ZeroGPU will move them to VRAM inside the @spaces.GPU function
11
  print("Initializing Application...")
12
  handler = ModelHandler()
13
  handler.load_models()
14
  gen = Generator(handler)
15
 
16
  # 2. Define GPU-enabled Inference Function
17
- @spaces.GPU(duration=20) # <-- MODIFIED
18
  def process_img(
19
  image,
20
  prompt,
21
  negative_prompt,
22
- cfg_scale,
23
  steps,
24
  img_strength,
25
  depth_strength,
26
  edge_strength,
27
- # tile_strength, # <-- REMOVED
28
  seed
29
  ):
30
  if image is None:
@@ -32,17 +29,15 @@ def process_img(
32
 
33
  try:
34
  print("--- Starting Generation ---")
35
- # Pass all parameters to the generator
36
  result = gen.predict(
37
  image,
38
  prompt,
39
  negative_prompt=negative_prompt,
40
- guidance_scale=cfg_scale,
41
  num_inference_steps=steps,
42
  img2img_strength=img_strength,
43
  depth_strength=depth_strength,
44
  lineart_strength=edge_strength,
45
- # tile_strength=tile_strength, # <-- REMOVED
46
  seed=seed
47
  )
48
  print("--- Generation Complete ---")
@@ -53,11 +48,11 @@ def process_img(
53
  raise gr.Error(f"An error occurred: {str(e)}")
54
 
55
  # 3. Build Gradio Interface
56
- with gr.Blocks(title="Face To Pixel Art", theme=gr.themes.Soft()) as demo:
57
  gr.Markdown(
58
  """
59
- # 🎮 Face to Pixel Art
60
- Upload any image. If there is a face, we'll keep the identity. If not, we'll pixelate the scene!
61
  """
62
  )
63
 
@@ -67,13 +62,13 @@ with gr.Blocks(title="Face To Pixel Art", theme=gr.themes.Soft()) as demo:
67
  prompt = gr.Textbox(
68
  label="Prompt (Optional)",
69
  placeholder="Leave empty for auto-captioning...",
70
- info="The trigger words 'p1x3l4rt, pixel art' are added automatically."
71
  )
72
 
73
  negative_prompt = gr.Textbox(
74
  label="Negative Prompt (Optional)",
75
  placeholder="e.g., blurry, text, watermark, bad art...",
76
- value=Config.DEFAULT_NEGATIVE_PROMPT # <-- MODIFIED
77
  )
78
 
79
  with gr.Accordion("Advanced Settings", open=False):
@@ -84,20 +79,15 @@ with gr.Blocks(title="Face To Pixel Art", theme=gr.themes.Soft()) as demo:
84
  precision=0
85
  )
86
 
87
- cfg_scale = gr.Slider(
88
- elem_id="cfg_scale",
89
- minimum=1.0,
90
- maximum=5.0,
91
- step=0.1,
92
- value=Config.CGF_SCALE,
93
- label="CFG Scale"
94
- )
95
  steps = gr.Slider(
96
  elem_id="steps",
97
  minimum=4,
98
  maximum=20,
99
  step=1,
100
- value=Config.STEPS_NUMBER,
101
  label="Steps Number"
102
  )
103
  img_strength = gr.Slider(
@@ -124,25 +114,22 @@ with gr.Blocks(title="Face To Pixel Art", theme=gr.themes.Soft()) as demo:
124
  value=Config.EDGE_STRENGTH,
125
  label="EdgeMap Strength (LineArt)"
126
  )
127
- # --- MODIFIED: Renamed slider ---
128
- # tile_strength = gr.Slider(...) # <-- REMOVED
129
 
130
- run_btn = gr.Button("Generate Pixel Art", variant="primary")
131
 
132
  with gr.Column(scale=1):
133
- output_img = gr.Image(label="Pixel Art Result")
134
 
135
  # Event Handler
136
  all_inputs = [
137
  input_img,
138
  prompt,
139
  negative_prompt,
140
- cfg_scale,
141
  steps,
142
  img_strength,
143
  depth_strength,
144
  edge_strength,
145
- # tile_strength, # <-- REMOVED
146
  seed
147
  ]
148
 
@@ -159,5 +146,5 @@ if __name__ == "__main__":
159
  demo.launch(
160
  server_name="0.0.0.0",
161
  server_port=7860,
162
- show_api=True # share=True is not needed on Spaces
163
  )
 
3
  import torch
4
  from model import ModelHandler
5
  from generator import Generator
 
6
  from config import Config
7
 
8
+ # 1. Initialize Models Globally
 
9
  print("Initializing Application...")
10
  handler = ModelHandler()
11
  handler.load_models()
12
  gen = Generator(handler)
13
 
14
  # 2. Define GPU-enabled Inference Function
15
+ @spaces.GPU(duration=20)
16
  def process_img(
17
  image,
18
  prompt,
19
  negative_prompt,
20
+ # cfg_scale, <-- REMOVED (TCD uses 0.0)
21
  steps,
22
  img_strength,
23
  depth_strength,
24
  edge_strength,
 
25
  seed
26
  ):
27
  if image is None:
 
29
 
30
  try:
31
  print("--- Starting Generation ---")
 
32
  result = gen.predict(
33
  image,
34
  prompt,
35
  negative_prompt=negative_prompt,
36
+ guidance_scale=Config.CGF_SCALE, # Hardcoded to 0.0 from Config
37
  num_inference_steps=steps,
38
  img2img_strength=img_strength,
39
  depth_strength=depth_strength,
40
  lineart_strength=edge_strength,
 
41
  seed=seed
42
  )
43
  print("--- Generation Complete ---")
 
48
  raise gr.Error(f"An error occurred: {str(e)}")
49
 
50
  # 3. Build Gradio Interface
51
+ with gr.Blocks(title="Face To Style", theme=gr.themes.Soft()) as demo:
52
  gr.Markdown(
53
  """
54
+ # 🎮 Face to Style
55
+ Upload any image. If there is a face, we'll keep the identity. If not, we'll stylize the scene!
56
  """
57
  )
58
 
 
62
  prompt = gr.Textbox(
63
  label="Prompt (Optional)",
64
  placeholder="Leave empty for auto-captioning...",
65
+ info=f"The trigger words '{Config.STYLE_TRIGGER}' are added automatically."
66
  )
67
 
68
  negative_prompt = gr.Textbox(
69
  label="Negative Prompt (Optional)",
70
  placeholder="e.g., blurry, text, watermark, bad art...",
71
+ value=Config.DEFAULT_NEGATIVE_PROMPT
72
  )
73
 
74
  with gr.Accordion("Advanced Settings", open=False):
 
79
  precision=0
80
  )
81
 
82
+ # --- REMOVED CFG/GUIDANCE SLIDER ---
83
+ # TCD works best with guidance_scale=0.0
84
+
 
 
 
 
 
85
  steps = gr.Slider(
86
  elem_id="steps",
87
  minimum=4,
88
  maximum=20,
89
  step=1,
90
+ value=8, # TCD default
91
  label="Steps Number"
92
  )
93
  img_strength = gr.Slider(
 
114
  value=Config.EDGE_STRENGTH,
115
  label="EdgeMap Strength (LineArt)"
116
  )
 
 
117
 
118
+ run_btn = gr.Button("Generate", variant="primary")
119
 
120
  with gr.Column(scale=1):
121
+ output_img = gr.Image(label="Styled Result")
122
 
123
  # Event Handler
124
  all_inputs = [
125
  input_img,
126
  prompt,
127
  negative_prompt,
128
+ # cfg_scale, <-- REMOVED
129
  steps,
130
  img_strength,
131
  depth_strength,
132
  edge_strength,
 
133
  seed
134
  ]
135
 
 
146
  demo.launch(
147
  server_name="0.0.0.0",
148
  server_port=7860,
149
+ show_api=True
150
  )