Arnaviit commited on
Commit
ff52c34
·
verified ·
1 Parent(s): 85dc613

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -27
app.py CHANGED
@@ -3,37 +3,41 @@ import torch
3
  from diffusers import StableDiffusion3Pipeline
4
 
5
  def image_generation(prompt):
6
- try:
7
- device = "cpu"
8
- pipeline = StableDiffusion3Pipeline.from_pretrained(
9
- "stabilityai/stable-diffusion-3-medium-diffusers",
10
- torch_dtype=torch.float32, # ✅ Use float32 on CPU
11
- text_encoder_3=None,
12
- tokenizer_3=None
13
- )
14
- pipeline.to(device)
15
-
16
- image = pipeline(
17
- prompt=prompt,
18
- negative_prompt="blurred, ugly, watermark, low resolution, blurry",
19
- num_inference_steps=20, # ✅ Reduce steps for performance
20
- height=1024, # ✅ Smaller size to avoid memory error
21
- width=1024,
22
- guidance_scale=7.5,
23
- ).images[0]
24
-
25
- return image
26
-
27
- except Exception as e:
28
- print(f"[ERROR] {e}")
29
- return f"Error: {e}"
30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  interface = gr.Interface(
32
  fn=image_generation,
33
- inputs=gr.Textbox(lines=2, placeholder="Enter your prompt..."),
34
- outputs="image",
35
  title="AI Image Generator By Arnav Anand",
36
- description="Generate images using Stable Diffusion 3 (CPU-Only Version)"
37
  )
38
 
39
  interface.launch()
 
3
  from diffusers import StableDiffusion3Pipeline
4
 
5
  def image_generation(prompt):
6
+ device = "cuda" if torch.cuda.is_available() else "cpu"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
+ # Load the pipeline (with resume_download if interrupted previously)
9
+ pipeline = StableDiffusion3Pipeline.from_pretrained(
10
+ "stabilityai/stable-diffusion-3-medium-diffusers",
11
+ torch_dtype=torch.float16 if device == "cuda" else torch.float32,
12
+ text_encoder_3=None,
13
+ tokenizer_3=None,
14
+ resume_download=True
15
+ )
16
+
17
+ # ✅ Only use this line if you have GPU + Accelerate
18
+ # pipeline.enable_model_cpu_offload()
19
+
20
+ # ✅ Instead, move pipeline to CPU or CUDA manually
21
+ pipeline.to(device)
22
+
23
+ image = pipeline(
24
+ prompt=prompt,
25
+ negative_prompt="blurred,ugly,watermark, low resolution, blurry",
26
+ num_inference_steps=50,
27
+ height=1024,
28
+ width=1024,
29
+ guidance_scale=9.0,
30
+ ).images[0]
31
+
32
+ return image # ✅ Return the image for Gradio
33
+
34
+ # Gradio UI
35
  interface = gr.Interface(
36
  fn=image_generation,
37
+ inputs=gr.Textbox(lines=2, placeholder="Enter your Prompt..."),
38
+ outputs=gr.Image(type="pil"),
39
  title="AI Image Generator By Arnav Anand",
40
+ description="This application will be used to generate awesome images using SD3 model"
41
  )
42
 
43
  interface.launch()