RamaManna commited on
Commit
c7cf423
·
verified ·
1 Parent(s): e4c5be7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -59
app.py CHANGED
@@ -1,23 +1,21 @@
1
  import gradio as gr
2
  import torch
3
- from diffusers import StableDiffusionPipeline, DPMSolverSinglestepScheduler
4
  from PIL import Image
5
 
6
- # Load a memory-efficient SD variant (under 12GB)
7
- model_id = "runwayml/stable-diffusion-v1-5"
8
 
9
  @gr.cache()
10
  def load_model():
11
  pipe = StableDiffusionPipeline.from_pretrained(
12
- model_id,
13
  torch_dtype=torch.float16,
14
  safety_checker=None,
15
  use_safetensors=True
16
  )
17
- pipe.scheduler = DPMSolverSinglestepScheduler.from_config(pipe.scheduler.config)
18
  pipe = pipe.to("cpu")
19
- pipe.enable_attention_slicing() # Reduces memory by 30%
20
- pipe.enable_model_cpu_offload() # Only loads needed components
21
  return pipe
22
 
23
  def generate_character(prompt, seed=42):
@@ -25,66 +23,36 @@ def generate_character(prompt, seed=42):
25
  pipe = load_model()
26
  generator = torch.Generator(device="cpu").manual_seed(seed)
27
 
28
- with torch.inference_mode():
29
- image = pipe(
30
- prompt=f"cartoon character {prompt}, vibrant colors, clean lines",
31
- negative_prompt="blurry, deformed, ugly",
32
- num_inference_steps=20,
33
- guidance_scale=7.5,
34
- width=512,
35
- height=512,
36
- generator=generator
37
- ).images[0]
38
 
39
  return image
40
  except Exception as e:
41
- return f"Error: {str(e)}\nTry simplifying your prompt."
42
 
43
- # Animation through img2img
44
- def generate_animation(prompt, frames=3):
45
- base_image = generate_character(prompt)
46
- if isinstance(base_image, str): # If error
47
- return base_image
48
-
49
- images = [base_image]
50
- pipe = load_model()
51
-
52
- for i in range(1, frames):
53
- result = pipe(
54
- prompt=prompt,
55
- image=images[-1],
56
- strength=0.3, # Small changes per frame
57
- generator=torch.Generator().manual_seed(i)
58
- )
59
- images.append(result.images[0])
60
-
61
- images[0].save(
62
- "animation.gif",
63
- save_all=True,
64
- append_images=images[1:],
65
- duration=500,
66
- loop=0
67
- )
68
- return "animation.gif"
69
-
70
- with gr.Blocks(theme=gr.themes.Base()) as demo:
71
- gr.Markdown("# 🎬 Character Animator (12GB Optimized)")
72
 
73
  with gr.Row():
74
  prompt = gr.Textbox(
75
- label="Character Description",
76
- placeholder="e.g. 'cyberpunk fox wearing sunglasses'"
 
77
  )
78
 
79
- with gr.Tab("Single Image"):
80
- img_out = gr.Image(label="Generated Character", type="pil")
81
- gen_btn = gr.Button("Generate")
82
 
83
- with gr.Tab("Animation"):
84
- anim_out = gr.Image(label="Animation", format="gif")
85
- anim_btn = gr.Button("Create Animation (3 frames)")
86
-
87
- gen_btn.click(generate_character, inputs=prompt, outputs=img_out)
88
- anim_btn.click(generate_animation, inputs=prompt, outputs=anim_out)
89
 
90
- demo.launch()
 
1
  import gradio as gr
2
  import torch
3
+ from diffusers import StableDiffusionPipeline
4
  from PIL import Image
5
 
6
+ # Use a smaller SD model variant that fits within free tier
7
+ MODEL_ID = "CompVis/ldm-super-resolution-4x-openimages" # Only ~1.4GB
8
 
9
  @gr.cache()
10
  def load_model():
11
  pipe = StableDiffusionPipeline.from_pretrained(
12
+ MODEL_ID,
13
  torch_dtype=torch.float16,
14
  safety_checker=None,
15
  use_safetensors=True
16
  )
 
17
  pipe = pipe.to("cpu")
18
+ pipe.enable_attention_slicing() # Reduces memory usage
 
19
  return pipe
20
 
21
  def generate_character(prompt, seed=42):
 
23
  pipe = load_model()
24
  generator = torch.Generator(device="cpu").manual_seed(seed)
25
 
26
+ image = pipe(
27
+ prompt=f"pixel art {prompt}, clean lines, vibrant colors",
28
+ num_inference_steps=20,
29
+ guidance_scale=7.0,
30
+ width=256,
31
+ height=256,
32
+ generator=generator
33
+ ).images[0]
 
 
34
 
35
  return image
36
  except Exception as e:
37
+ return f"Error: {str(e)}\nTry a simpler prompt."
38
 
39
+ with gr.Blocks(theme=gr.themes.Default()) as demo:
40
+ gr.Markdown("# 🎮 Lightweight Character Generator")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
  with gr.Row():
43
  prompt = gr.Textbox(
44
+ label="Describe your character",
45
+ placeholder="e.g. 'robot pirate with laser eye'",
46
+ max_lines=2
47
  )
48
 
49
+ generate_btn = gr.Button("Generate", variant="primary")
50
+ output = gr.Image(label="Your Character", type="pil")
 
51
 
52
+ generate_btn.click(
53
+ generate_character,
54
+ inputs=prompt,
55
+ outputs=output
56
+ )
 
57
 
58
+ demo.launch(debug=False)