NeelTA commited on
Commit
85c93d5
·
1 Parent(s): e411b3b

using runwayml/stable-diffusion-v1-5

Browse files
Files changed (1) hide show
  1. app.py +29 -22
app.py CHANGED
@@ -1,42 +1,49 @@
1
  import gradio as gr
2
  import torch
3
- from huggingface_hub import login
4
  import os
5
 
6
  # Use CPU optimized model
7
  os.environ["HF_HOME"] = "/tmp/hf_cache"
8
 
9
- # FLUX model requires authentication
10
- MODEL_ID = "black-forest-labs/FLUX.1-dev"
11
 
12
- # You'll need to add your Hugging Face token as a secret in your Space
13
- # Then uncomment the following lines:
14
- from huggingface_hub import login
15
- login(token=os.getenv("HF_TOKEN")) # Add HF_TOKEN as a secret in your Space settings
16
-
17
- print(f"Loading model {MODEL_ID}...")
18
 
19
  try:
20
- from diffusers import FluxPipeline
21
-
22
- pipe = FluxPipeline.from_pretrained(
23
  MODEL_ID,
24
- torch_dtype=torch.bfloat16
 
 
 
25
  )
26
- pipe = pipe.to("cpu") # For CPU, but this will be very slow
27
  pipe.set_progress_bar_config(disable=True)
28
- print("FLUX Model loaded successfully!")
29
  except Exception as e:
30
- print(f"Error loading FLUX model: {e}")
31
- print("Please ensure you have access to the model and have set your HF_TOKEN")
32
- raise e
 
 
 
 
 
 
 
 
 
33
 
34
  def generate(prompt):
35
  try:
36
  result = pipe(
37
  prompt=prompt,
38
- num_inference_steps=20,
39
- guidance_scale=3.5 # FLUX typically uses lower guidance scale
 
40
  )
41
  return result.images[0]
42
  except Exception as e:
@@ -47,8 +54,8 @@ interface = gr.Interface(
47
  fn=generate,
48
  inputs=gr.Textbox(lines=3, label="Prompt", placeholder="Enter your image prompt here..."),
49
  outputs=gr.Image(type="pil", label="Generated Image"),
50
- title="⚡ FLUX Image Generation",
51
- description="Image generation with FLUX model (requires authentication)"
52
  )
53
 
54
  interface.launch()
 
1
  import gradio as gr
2
  import torch
3
+ from diffusers import OnnxStableDiffusionPipeline
4
  import os
5
 
6
  # Use CPU optimized model
7
  os.environ["HF_HOME"] = "/tmp/hf_cache"
8
 
9
+ # Using a proper ONNX model for CPU inference
10
+ MODEL_ID = "runwayml/stable-diffusion-v1-5"
11
 
12
+ print(f"Loading ONNX model {MODEL_ID}...")
 
 
 
 
 
13
 
14
  try:
15
+ # Use ONNX pipeline for better CPU performance
16
+ pipe = OnnxStableDiffusionPipeline.from_pretrained(
 
17
  MODEL_ID,
18
+ revision="onnx",
19
+ provider="CPUExecutionProvider",
20
+ torch_dtype=torch.float32,
21
+ low_cpu_mem_usage=True
22
  )
23
+ pipe.safety_checker = None # Disable safety checker for simplicity
24
  pipe.set_progress_bar_config(disable=True)
25
+ print("ONNX Model loaded successfully!")
26
  except Exception as e:
27
+ print(f"Failed to load ONNX model: {e}")
28
+ print("Falling back to regular Stable Diffusion pipeline...")
29
+ from diffusers import StableDiffusionPipeline
30
+ pipe = StableDiffusionPipeline.from_pretrained(
31
+ MODEL_ID,
32
+ torch_dtype=torch.float32,
33
+ low_cpu_mem_usage=True
34
+ )
35
+ pipe = pipe.to("cpu")
36
+ pipe.safety_checker = None
37
+ pipe.set_progress_bar_config(disable=True)
38
+ print("Model loaded!")
39
 
40
  def generate(prompt):
41
  try:
42
  result = pipe(
43
  prompt=prompt,
44
+ num_inference_steps=40,
45
+ num_images_per_prompt=1,
46
+ guidance_scale=7.5
47
  )
48
  return result.images[0]
49
  except Exception as e:
 
54
  fn=generate,
55
  inputs=gr.Textbox(lines=3, label="Prompt", placeholder="Enter your image prompt here..."),
56
  outputs=gr.Image(type="pil", label="Generated Image"),
57
+ title="⚡ ONNX Stable Diffusion",
58
+ description="Faster CPU-based image generation with ONNX optimization"
59
  )
60
 
61
  interface.launch()