sd / app.py
ngockha's picture
Add initial implementation for Stable Diffusion A1111 web UI in CPU mode
fd59947
import os
import subprocess
import gradio as gr
# Clone A1111 Web UI only if not already cloned
if not os.path.exists("stable-diffusion-webui"):
subprocess.run(
["git", "clone", "https://github.com/AUTOMATIC1111/stable-diffusion-webui.git"],
check=True,
)
os.chdir("stable-diffusion-webui")
# Install necessary dependencies
subprocess.run(["pip", "install", "-r", "requirements.txt"], check=True)
# Ensure CPU mode is enabled
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
# Download a smaller SD model to fit CPU constraints (e.g., SD 1.5 EMA-only)
MODEL_PATH = "models/Stable-diffusion/sd-v1-5-ema.ckpt"
MODEL_NAME = "sd-v1-5-ema.ckpt"
MODEL_API = "https://civitai.com/api/download/models/11745?type=Model&format=SafeTensor&size=full&fp=fp16"
if not os.path.exists(MODEL_PATH):
os.makedirs("models/Stable-diffusion", exist_ok=True)
subprocess.run(
["wget", "-O", MODEL_PATH, MODEL_API],
check=True,
)
# Launch web UI with CPU mode
def launch_webui():
os.system(
"python webui.py --listen --share --no-half --precision full --disable-nan-check --use-cpu all --skip-torch-cuda-test"
)
# Create a simple Gradio interface
def start():
return (
"Stable Diffusion A1111 (CPU Mode) is running! Click the link to open the UI."
)
interface = gr.Interface(fn=start, inputs=[], outputs="text")
interface.launch()