primerz commited on
Commit
0f0e9c7
·
verified ·
1 Parent(s): 62313c9

Upload 7 files

Browse files
Files changed (7) hide show
  1. README.md +46 -13
  2. app.py +11 -1
  3. config.py +11 -1
  4. generator.py +10 -3
  5. model.py +1 -1
  6. requirements.txt +1 -1
  7. utils.py +1 -1
README.md CHANGED
@@ -1,13 +1,46 @@
1
- ---
2
- title: Pixel Art
3
- emoji: 🐠
4
- colorFrom: indigo
5
- colorTo: pink
6
- sdk: gradio
7
- sdk_version: 5.49.1
8
- app_file: app.py
9
- pinned: true
10
- license: mit
11
- ---
12
-
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Pixel Art Generator - Aspect Ratio Update
2
+
3
+ ## Changes Made
4
+
5
+ I've added aspect ratio selection to your pixel art generator with the following options:
6
+
7
+ ### Supported Aspect Ratios
8
+ All aspect ratios are capped at approximately 1 megapixel resolution:
9
+
10
+ - **16:9** - 1344x768 (landscape, widescreen)
11
+ - **4:3** - 1152x896 (landscape, classic)
12
+ - **1:1** - 1024x1024 (square, default)
13
+ - **3:4** - 896x1152 (portrait, classic)
14
+ - **9:16** - 768x1344 (portrait, mobile)
15
+
16
+ All dimensions are divisible by 64 for optimal performance with SDXL.
17
+
18
+ ## Modified Files
19
+
20
+ ### 1. `config.py`
21
+ - Added `ASPECT_RATIOS` dictionary with all aspect ratio options and their dimensions
22
+ - Added `DEFAULT_ASPECT_RATIO` set to "1:1"
23
+
24
+ ### 2. `app.py`
25
+ - Added aspect ratio dropdown in the Settings accordion
26
+ - Updated the `process_text` function to accept `aspect_ratio` parameter
27
+ - Updated the inputs list to include the aspect ratio selection
28
+
29
+ ### 3. `generator.py`
30
+ - Added `aspect_ratio` parameter to the `predict` method (defaults to "1:1")
31
+ - Extracts width and height from `Config.ASPECT_RATIOS` based on selection
32
+ - Passes width and height to the pipeline for generation
33
+
34
+ ### Other Files
35
+ - `model.py`, `requirements.txt`, and `utils.py` remain unchanged
36
+
37
+ ## Usage
38
+
39
+ Users can now select their preferred aspect ratio from the dropdown menu in the Settings section before generating pixel art. The dropdown appears above the seed input and shows all 5 available aspect ratios with a helpful description.
40
+
41
+ ## Technical Details
42
+
43
+ - All aspect ratios maintain approximately 1MP total pixels
44
+ - Dimensions are optimized for SDXL (divisible by 64)
45
+ - The aspect ratio is displayed in the console logs during generation
46
+ - Default fallback is 1:1 (1024x1024) if an invalid ratio is provided
app.py CHANGED
@@ -15,6 +15,7 @@ gen = Generator(handler)
15
  def process_text(
16
  prompt,
17
  negative_prompt,
 
18
  cfg_scale,
19
  steps,
20
  seed
@@ -24,6 +25,7 @@ def process_text(
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
@@ -59,6 +61,13 @@ with gr.Blocks(title="Pixel Art Generator", theme=gr.themes.Soft()) as demo:
59
  )
60
 
61
  with gr.Accordion("Settings", open=True):
 
 
 
 
 
 
 
62
  seed = gr.Number(
63
  label="Seed",
64
  value=-1,
@@ -92,6 +101,7 @@ with gr.Blocks(title="Pixel Art Generator", theme=gr.themes.Soft()) as demo:
92
  all_inputs = [
93
  prompt,
94
  negative_prompt,
 
95
  cfg_scale,
96
  steps,
97
  seed
@@ -110,4 +120,4 @@ if __name__ == "__main__":
110
  server_name="0.0.0.0",
111
  server_port=7860,
112
  show_api=True
113
- )
 
15
  def process_text(
16
  prompt,
17
  negative_prompt,
18
+ aspect_ratio,
19
  cfg_scale,
20
  steps,
21
  seed
 
25
  result = gen.predict(
26
  user_prompt=prompt,
27
  negative_prompt=negative_prompt,
28
+ aspect_ratio=aspect_ratio,
29
  guidance_scale=cfg_scale,
30
  num_inference_steps=steps,
31
  seed=seed
 
61
  )
62
 
63
  with gr.Accordion("Settings", open=True):
64
+ aspect_ratio = gr.Dropdown(
65
+ label="Aspect Ratio",
66
+ choices=list(Config.ASPECT_RATIOS.keys()),
67
+ value=Config.DEFAULT_ASPECT_RATIO,
68
+ info="Image dimensions (all ~1MP resolution)"
69
+ )
70
+
71
  seed = gr.Number(
72
  label="Seed",
73
  value=-1,
 
101
  all_inputs = [
102
  prompt,
103
  negative_prompt,
104
+ aspect_ratio,
105
  cfg_scale,
106
  steps,
107
  seed
 
120
  server_name="0.0.0.0",
121
  server_port=7860,
122
  show_api=True
123
+ )
config.py CHANGED
@@ -19,4 +19,14 @@ class Config:
19
 
20
  # Gradio Parameters
21
  CGF_SCALE = 1.2
22
- STEPS_NUMBER = 8 # Reduced slightly for optimal LCM T2I performance
 
 
 
 
 
 
 
 
 
 
 
19
 
20
  # Gradio Parameters
21
  CGF_SCALE = 1.2
22
+ STEPS_NUMBER = 8 # Reduced slightly for optimal LCM T2I performance
23
+
24
+ # Aspect Ratios (width, height) - all fitting within ~1MP resolution
25
+ ASPECT_RATIOS = {
26
+ "16:9": (1344, 768),
27
+ "4:3": (1152, 896),
28
+ "1:1": (1024, 1024),
29
+ "3:4": (896, 1152),
30
+ "9:16": (768, 1344)
31
+ }
32
+ DEFAULT_ASPECT_RATIO = "1:1"
generator.py CHANGED
@@ -9,6 +9,7 @@ class Generator:
9
  self,
10
  user_prompt,
11
  negative_prompt="",
 
12
  guidance_scale=1.6,
13
  num_inference_steps=8,
14
  seed=-1
@@ -22,21 +23,27 @@ class Generator:
22
 
23
  print(f"Prompt: {final_prompt}")
24
 
25
- # 2. Handle Seed
 
 
 
 
26
  if seed == -1 or seed is None:
27
  seed = torch.Generator().seed()
28
  generator = torch.Generator(device=Config.DEVICE).manual_seed(int(seed))
29
  print(f"Using seed: {seed}")
30
 
31
- # 3. Run Text-to-Image Inference
32
  print("Running pipeline...")
33
  result = self.mh.pipeline(
34
  prompt=final_prompt,
35
  negative_prompt=negative_prompt,
 
 
36
  generator=generator,
37
  num_inference_steps=num_inference_steps,
38
  guidance_scale=guidance_scale,
39
  clip_skip=2, # Optional, often helps with anime/pixel styles
40
  ).images[0]
41
 
42
- return result
 
9
  self,
10
  user_prompt,
11
  negative_prompt="",
12
+ aspect_ratio="1:1",
13
  guidance_scale=1.6,
14
  num_inference_steps=8,
15
  seed=-1
 
23
 
24
  print(f"Prompt: {final_prompt}")
25
 
26
+ # 2. Get dimensions from aspect ratio
27
+ width, height = Config.ASPECT_RATIOS.get(aspect_ratio, Config.ASPECT_RATIOS[Config.DEFAULT_ASPECT_RATIO])
28
+ print(f"Aspect Ratio: {aspect_ratio} ({width}x{height})")
29
+
30
+ # 3. Handle Seed
31
  if seed == -1 or seed is None:
32
  seed = torch.Generator().seed()
33
  generator = torch.Generator(device=Config.DEVICE).manual_seed(int(seed))
34
  print(f"Using seed: {seed}")
35
 
36
+ # 4. Run Text-to-Image Inference
37
  print("Running pipeline...")
38
  result = self.mh.pipeline(
39
  prompt=final_prompt,
40
  negative_prompt=negative_prompt,
41
+ width=width,
42
+ height=height,
43
  generator=generator,
44
  num_inference_steps=num_inference_steps,
45
  guidance_scale=guidance_scale,
46
  clip_skip=2, # Optional, often helps with anime/pixel styles
47
  ).images[0]
48
 
49
+ return result
model.py CHANGED
@@ -64,4 +64,4 @@ class ModelHandler:
64
  self.pipeline.fuse_lora(lora_scale=Config.LORA_STRENGTH)
65
  print(" [OK] LoRA fused.")
66
 
67
- print("--- All models loaded successfully ---")
 
64
  self.pipeline.fuse_lora(lora_scale=Config.LORA_STRENGTH)
65
  print(" [OK] LoRA fused.")
66
 
67
+ print("--- All models loaded successfully ---")
requirements.txt CHANGED
@@ -11,4 +11,4 @@ gradio>=4.0.0
11
  controlnet_aux
12
  huggingface_hub
13
  mediapipe
14
- timm
 
11
  controlnet_aux
12
  huggingface_hub
13
  mediapipe
14
+ timm
utils.py CHANGED
@@ -23,4 +23,4 @@ def resize_image_to_1mp(image):
23
  if new_w == 0 or new_h == 0:
24
  new_w, new_h = 1024, 1024 # Fallback
25
 
26
- return image.resize((new_w, new_h), Image.LANCZOS)
 
23
  if new_w == 0 or new_h == 0:
24
  new_w, new_h = 1024, 1024 # Fallback
25
 
26
+ return image.resize((new_w, new_h), Image.LANCZOS)