File size: 1,394 Bytes
fd59947
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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()