RamaManna commited on
Commit
0db4d23
·
verified ·
1 Parent(s): 6405f1a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -57
app.py CHANGED
@@ -1,95 +1,55 @@
1
  import gradio as gr
2
  import torch
3
- from diffusers import StableDiffusionPipeline
4
  from PIL import Image
5
 
6
- # Tiny model that fits in free tier memory
7
- MODEL_NAME = "OFA-Sys/small-stable-diffusion-v0"
8
 
9
- # Load model (will cache after first run)
10
  @gr.cache()
11
  def load_model():
12
- return StableDiffusionPipeline.from_pretrained(
13
- MODEL_NAME,
14
  torch_dtype=torch.float16,
15
- safety_checker=None
 
16
  ).to("cpu")
17
 
18
  def generate_character(description, seed=42):
19
  try:
20
  pipe = load_model()
21
 
22
- # Reduce memory usage
23
  torch.manual_seed(seed)
24
  with torch.inference_mode():
25
  image = pipe(
26
  prompt=f"pixel art character, {description}",
27
- num_inference_steps=15, # Fewer steps = less memory
28
  guidance_scale=7.0,
29
- width=256, # Smaller resolution
30
  height=256
31
  ).images[0]
32
 
33
  return image
34
  except Exception as e:
35
- return f"Error: {str(e)}\nTry a simpler description or different words."
36
 
37
- # Create simple animation effect by generating variations
38
- def generate_animation(description, frames=3):
39
- images = []
40
- for i in range(frames):
41
- img = generate_character(description, seed=i)
42
- if isinstance(img, str): # If error returned
43
- return img
44
- images.append(img)
45
-
46
- # Create simple animation (GIF)
47
- images[0].save(
48
- "animation.gif",
49
- save_all=True,
50
- append_images=images[1:],
51
- duration=500,
52
- loop=0
53
- )
54
- return "animation.gif"
55
-
56
- # Minimal interface
57
- with gr.Blocks(title="Tiny Character Animator") as demo:
58
- gr.Markdown("""
59
- # 🎮 Tiny Character Animator
60
- *Free-tier optimized for Hugging Face Spaces*
61
- """)
62
 
63
  with gr.Row():
64
  desc = gr.Textbox(
65
  label="Describe your character",
66
- placeholder="e.g., 'blue robot with antennae'",
67
  max_lines=2
68
  )
69
 
70
- with gr.Row():
71
- btn_still = gr.Button("Generate Still", variant="secondary")
72
- btn_animate = gr.Button("Generate Animation", variant="primary")
73
 
74
- with gr.Row():
75
- output_still = gr.Image(label="Character", shape=(256, 256))
76
- output_anim = gr.Image(label="Animation", format="gif", visible=False)
77
-
78
- # Button actions
79
- btn_still.click(
80
  generate_character,
81
  inputs=desc,
82
- outputs=output_still
83
- )
84
-
85
- btn_animate.click(
86
- lambda: (gr.Image(visible=False), gr.Image(visible=True)), # Toggle visibility
87
- None,
88
- [output_still, output_anim]
89
- ).then(
90
- generate_animation,
91
- inputs=desc,
92
- outputs=output_anim
93
  )
94
 
95
  demo.launch(debug=False)
 
1
  import gradio as gr
2
  import torch
 
3
  from PIL import Image
4
 
5
+ # We'll use the smallest available pipeline
6
+ from diffusers import DiffusionPipeline
7
 
8
+ # Load model (cached after first run)
9
  @gr.cache()
10
  def load_model():
11
+ return DiffusionPipeline.from_pretrained(
12
+ "OFA-Sys/small-stable-diffusion-v0",
13
  torch_dtype=torch.float16,
14
+ safety_checker=None,
15
+ use_safetensors=True
16
  ).to("cpu")
17
 
18
  def generate_character(description, seed=42):
19
  try:
20
  pipe = load_model()
21
 
 
22
  torch.manual_seed(seed)
23
  with torch.inference_mode():
24
  image = pipe(
25
  prompt=f"pixel art character, {description}",
26
+ num_inference_steps=15,
27
  guidance_scale=7.0,
28
+ width=256,
29
  height=256
30
  ).images[0]
31
 
32
  return image
33
  except Exception as e:
34
+ return f"Error: {str(e)}\nTry a simpler description."
35
 
36
+ with gr.Blocks(title="Lightweight Character Generator") as demo:
37
+ gr.Markdown("# 🎨 Tiny Character Creator")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
  with gr.Row():
40
  desc = gr.Textbox(
41
  label="Describe your character",
42
+ placeholder="e.g., 'green alien with one eye'",
43
  max_lines=2
44
  )
45
 
46
+ generate_btn = gr.Button("Generate Character")
47
+ output = gr.Image(label="Your Character", shape=(256, 256))
 
48
 
49
+ generate_btn.click(
 
 
 
 
 
50
  generate_character,
51
  inputs=desc,
52
+ outputs=output
 
 
 
 
 
 
 
 
 
 
53
  )
54
 
55
  demo.launch(debug=False)