alidev2002 commited on
Commit
d053104
Β·
verified Β·
1 Parent(s): e91e2a1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -20
app.py CHANGED
@@ -1,38 +1,70 @@
1
  import gradio as gr
2
- from diffusers import DiffusionPipeline
3
- import torch
4
-
5
- pipe = DiffusionPipeline.from_pretrained(
6
- "stabilityai/stable-diffusion-xl-base-1.0",
7
- torch_dtype=torch.float32,
8
- use_safetensors=True,
9
- variant="fp16"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  )
11
- pipe.to("cpu")
12
 
13
- pipe.enable_attention_slicing()
14
 
15
  # =========================
16
- # generate function
17
  # =========================
18
  def generate(prompt):
19
 
20
- image = pipe(
21
- prompt=prompt,
22
- num_inference_steps=20,
23
- guidance_scale=6.5
24
- ).images[0]
 
 
 
 
 
25
 
26
- return image
27
 
28
  # =========================
29
- # gradio UI
30
  # =========================
31
  demo = gr.Interface(
32
  fn=generate,
33
  inputs=gr.Textbox(label="Prompt"),
34
- outputs=gr.Image(),
35
- title="Small Stable Diffusion (CPU)"
36
  )
37
 
38
  demo.launch()
 
1
  import gradio as gr
2
+ import os
3
+ import subprocess
4
+ import zipfile
5
+ import urllib.request
6
+ from huggingface_hub import hf_hub_download
7
+
8
+ # =========================
9
+ # 1. download sd.cpp binary (only once)
10
+ # =========================
11
+ SD_ZIP_URL = "https://github.com/leejet/stable-diffusion.cpp/releases/download/master-586-c97702e/sd-master-c97702e-bin-Linux-Ubuntu-24.04-x86_64.zip"
12
+ SD_DIR = "sdcpp"
13
+ SD_CLI_PATH = os.path.join(SD_DIR, "sd-cli")
14
+
15
+ if not os.path.exists(SD_CLI_PATH):
16
+ print("Downloading stable-diffusion.cpp binary...")
17
+
18
+ os.makedirs(SD_DIR, exist_ok=True)
19
+
20
+ zip_path = os.path.join(SD_DIR, "sd.zip")
21
+ urllib.request.urlretrieve(SD_ZIP_URL, zip_path)
22
+
23
+ print("Unzipping...")
24
+ with zipfile.ZipFile(zip_path, 'r') as zip_ref:
25
+ zip_ref.extractall(SD_DIR)
26
+
27
+ os.remove(zip_path)
28
+
29
+ # make executable
30
+ os.system(f"chmod +x {SD_CLI_PATH}")
31
+
32
+ # =========================
33
+ # 2. download GGUF model
34
+ # =========================
35
+ model_path = hf_hub_download(
36
+ repo_id="gpustack/stable-diffusion-v2-1-GGUF",
37
+ filename="stable-diffusion-v2-1-FP16.gguf"
38
  )
 
39
 
40
+ OUTPUT_IMAGE = "output.png"
41
 
42
  # =========================
43
+ # 3. generate function
44
  # =========================
45
  def generate(prompt):
46
 
47
+ cmd = [
48
+ SD_CLI_PATH,
49
+ "-m", model_path,
50
+ "-p", prompt,
51
+ "--steps", "20",
52
+ "--cfg-scale", "6.5",
53
+ "-o", OUTPUT_IMAGE
54
+ ]
55
+
56
+ subprocess.run(cmd, check=True)
57
 
58
+ return OUTPUT_IMAGE
59
 
60
  # =========================
61
+ # 4. gradio UI
62
  # =========================
63
  demo = gr.Interface(
64
  fn=generate,
65
  inputs=gr.Textbox(label="Prompt"),
66
+ outputs=gr.Image(type="filepath"),
67
+ title="GGUF Text-to-Image (sd.cpp on HF Space)"
68
  )
69
 
70
  demo.launch()