Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,73 +1,40 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
import zipfile
|
| 5 |
-
import urllib.request
|
| 6 |
-
from huggingface_hub import hf_hub_download
|
| 7 |
|
| 8 |
# =========================
|
| 9 |
-
#
|
| 10 |
# =========================
|
| 11 |
-
|
| 12 |
-
SD_DIR = "sdcpp"
|
| 13 |
-
SD_CLI_PATH = os.path.join(SD_DIR, "sd-cli")
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 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 |
-
|
| 41 |
|
| 42 |
# =========================
|
| 43 |
-
#
|
| 44 |
# =========================
|
| 45 |
def generate(prompt):
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 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
|
| 62 |
|
| 63 |
# =========================
|
| 64 |
-
#
|
| 65 |
# =========================
|
| 66 |
demo = gr.Interface(
|
| 67 |
fn=generate,
|
| 68 |
inputs=gr.Textbox(label="Prompt"),
|
| 69 |
-
outputs=gr.Image(
|
| 70 |
-
title="
|
| 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()
|