primerz commited on
Commit
1277f89
·
verified ·
1 Parent(s): 0f36a29

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -69
app.py CHANGED
@@ -1,43 +1,31 @@
1
  import gradio as gr
2
  import spaces
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, # <-- RE-ENABLED
21
  steps,
22
- img_strength,
23
- depth_strength,
24
- edge_strength,
25
  seed
26
  ):
27
- if image is None:
28
- raise gr.Error("Please upload an image first.")
29
-
30
  try:
31
  print("--- Starting Generation ---")
32
  result = gen.predict(
33
- image,
34
- prompt,
35
  negative_prompt=negative_prompt,
36
- guidance_scale=cfg_scale, # <-- RE-ENABLED
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,30 +36,29 @@ def process_img(
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
 
59
  with gr.Row():
60
  with gr.Column(scale=2):
61
- input_img = gr.Image(type="pil", label="Input Image")
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):
75
  seed = gr.Number(
76
  label="Seed",
77
  value=-1,
@@ -79,79 +66,48 @@ with gr.Blocks(title="Face To Style", theme=gr.themes.Soft()) as demo:
79
  precision=0
80
  )
81
 
82
- # --- RE-ENABLED CFG/GUIDANCE SLIDER ---
83
  cfg_scale = gr.Slider(
84
  elem_id="cfg_scale",
85
  minimum=1.0,
86
- maximum=10.0, # Range for TCD+Style
87
  step=0.1,
88
- value=Config.CGF_SCALE, # Default 4.0
89
- label="Style Strength (Guidance)"
90
  )
91
-
92
  steps = gr.Slider(
93
  elem_id="steps",
94
- minimum=1,
95
  maximum=20,
96
  step=1,
97
- value=8, # TCD default
98
- label="Steps Number"
99
- )
100
- img_strength = gr.Slider(
101
- elem_id="img_strength",
102
- minimum=0.1,
103
- maximum=1.0,
104
- step=0.05,
105
- value=Config.IMG_STRENGTH,
106
- label="Image Strength (Img2Img)"
107
- )
108
- depth_strength = gr.Slider(
109
- elem_id="depth_strength",
110
- minimum=0.0,
111
- maximum=1.0,
112
- step=0.05,
113
- value=Config.DEPTH_STRENGTH,
114
- label="DepthMap Strength"
115
- )
116
- edge_strength = gr.Slider(
117
- elem_id="edge_strength",
118
- minimum=0.0,
119
- maximum=1.0,
120
- step=0.05,
121
- value=Config.EDGE_STRENGTH,
122
- label="EdgeMap Strength (LineArt)"
123
  )
124
 
125
  run_btn = gr.Button("Generate", variant="primary")
126
 
127
  with gr.Column(scale=1):
128
- output_img = gr.Image(label="Styled Result")
129
 
130
  # Event Handler
131
  all_inputs = [
132
- input_img,
133
  prompt,
134
  negative_prompt,
135
- cfg_scale, # <-- RE-ENABLED
136
  steps,
137
- img_strength,
138
- depth_strength,
139
- edge_strength,
140
  seed
141
  ]
142
 
143
  run_btn.click(
144
- fn=process_img,
145
  inputs=all_inputs,
146
  outputs=[output_img]
147
  )
148
 
149
-
150
- # 4. Launch the App
151
  if __name__ == "__main__":
152
  demo.queue(max_size=20, api_open=True)
153
  demo.launch(
154
  server_name="0.0.0.0",
155
  server_port=7860,
156
- show_api=True
157
  )
 
1
  import gradio as gr
2
  import spaces
 
3
  from model import ModelHandler
4
  from generator import Generator
5
  from config import Config
6
 
7
+ # 1. Initialize Models
8
  print("Initializing Application...")
9
  handler = ModelHandler()
10
  handler.load_models()
11
  gen = Generator(handler)
12
 
13
+ # 2. Define GPU Inference Function
14
  @spaces.GPU(duration=20)
15
+ def process_text(
 
16
  prompt,
17
  negative_prompt,
18
+ cfg_scale,
19
  steps,
 
 
 
20
  seed
21
  ):
 
 
 
22
  try:
23
  print("--- Starting Generation ---")
24
  result = gen.predict(
25
+ user_prompt=prompt,
 
26
  negative_prompt=negative_prompt,
27
+ guidance_scale=cfg_scale,
28
  num_inference_steps=steps,
 
 
 
29
  seed=seed
30
  )
31
  print("--- Generation Complete ---")
 
36
  raise gr.Error(f"An error occurred: {str(e)}")
37
 
38
  # 3. Build Gradio Interface
39
+ with gr.Blocks(title="Pixel Art Generator", theme=gr.themes.Soft()) as demo:
40
  gr.Markdown(
41
  """
42
+ # 🎮 Text to Pixel Art
43
+ Type a prompt to generate high-quality pixel art scenes.
44
  """
45
  )
46
 
47
  with gr.Row():
48
  with gr.Column(scale=2):
 
49
  prompt = gr.Textbox(
50
+ label="Prompt",
51
+ placeholder="e.g. cyberpunk city street at night, rain",
52
+ info="The trigger words 'p1x3l4rt, pixel art' are added automatically."
53
  )
54
 
55
  negative_prompt = gr.Textbox(
56
+ label="Negative Prompt",
57
  placeholder="e.g., blurry, text, watermark, bad art...",
58
  value=Config.DEFAULT_NEGATIVE_PROMPT
59
  )
60
 
61
+ with gr.Accordion("Settings", open=True):
62
  seed = gr.Number(
63
  label="Seed",
64
  value=-1,
 
66
  precision=0
67
  )
68
 
 
69
  cfg_scale = gr.Slider(
70
  elem_id="cfg_scale",
71
  minimum=1.0,
72
+ maximum=5.0,
73
  step=0.1,
74
+ value=Config.CGF_SCALE,
75
+ label="CFG Scale"
76
  )
 
77
  steps = gr.Slider(
78
  elem_id="steps",
79
+ minimum=4,
80
  maximum=20,
81
  step=1,
82
+ value=Config.STEPS_NUMBER,
83
+ label="Steps"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
  )
85
 
86
  run_btn = gr.Button("Generate", variant="primary")
87
 
88
  with gr.Column(scale=1):
89
+ output_img = gr.Image(label="Result")
90
 
91
  # Event Handler
92
  all_inputs = [
 
93
  prompt,
94
  negative_prompt,
95
+ cfg_scale,
96
  steps,
 
 
 
97
  seed
98
  ]
99
 
100
  run_btn.click(
101
+ fn=process_text,
102
  inputs=all_inputs,
103
  outputs=[output_img]
104
  )
105
 
106
+ # 4. Launch
 
107
  if __name__ == "__main__":
108
  demo.queue(max_size=20, api_open=True)
109
  demo.launch(
110
  server_name="0.0.0.0",
111
  server_port=7860,
112
+ show_api=True
113
  )