alidev2002 commited on
Commit
fa14c3f
Β·
verified Β·
1 Parent(s): 1972bd2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -51
app.py CHANGED
@@ -1,73 +1,40 @@
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="stable-diffusion-v1-5/stable-diffusion-v1-5",
37
- filename="v1-5-pruned-emaonly.safetensors"
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", "8",
52
- "--cfg-scale", "7",
53
- "-o", OUTPUT_IMAGE
54
- ]
55
-
56
- env = os.environ.copy()
57
- env["LD_LIBRARY_PATH"] = SD_DIR
58
-
59
- subprocess.run(cmd, check=True, env=env)
60
 
61
- return OUTPUT_IMAGE
62
 
63
  # =========================
64
- # 4. gradio UI
65
  # =========================
66
  demo = gr.Interface(
67
  fn=generate,
68
  inputs=gr.Textbox(label="Prompt"),
69
- outputs=gr.Image(type="filepath"),
70
- title="GGUF Text-to-Image"
71
  )
72
 
73
  demo.launch()
 
1
  import gradio as gr
2
+ import torch
3
+ from diffusers import StableDiffusionPipeline
 
 
 
4
 
5
  # =========================
6
+ # load model (once)
7
  # =========================
8
+ model_id = "OFA-Sys/small-stable-diffusion-v0"
 
 
9
 
10
+ pipe = StableDiffusionPipeline.from_pretrained(
11
+ model_id,
12
+ torch_dtype=torch.float32 # CPU β†’ float32 Ψ¨Ω‡ΨͺΨ±Ω‡
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  )
14
 
15
+ pipe = pipe.to("cpu")
16
 
17
  # =========================
18
+ # generate function
19
  # =========================
20
  def generate(prompt):
21
 
22
+ image = pipe(
23
+ prompt,
24
+ num_inference_steps=15, # سبک و سریع
25
+ guidance_scale=6.5 # Ψ¨Ω‡ΨͺΨ± Ψ§Ψ² 1.0
26
+ ).images[0]
 
 
 
 
 
 
 
 
27
 
28
+ return image
29
 
30
  # =========================
31
+ # gradio UI
32
  # =========================
33
  demo = gr.Interface(
34
  fn=generate,
35
  inputs=gr.Textbox(label="Prompt"),
36
+ outputs=gr.Image(),
37
+ title="Small Stable Diffusion (CPU)"
38
  )
39
 
40
  demo.launch()