Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,30 +4,42 @@ import random
|
|
| 4 |
import torch
|
| 5 |
from openvino.runtime import Core
|
| 6 |
from PIL import Image
|
| 7 |
-
|
|
|
|
| 8 |
|
| 9 |
-
print("Initializing OpenVINO...
|
| 10 |
|
| 11 |
-
# --------- Detect
|
| 12 |
core = Core()
|
| 13 |
available_devices = core.available_devices
|
| 14 |
-
device = "CPU"
|
| 15 |
|
| 16 |
if "GPU" in available_devices:
|
| 17 |
device = "GPU"
|
| 18 |
elif "MYRIAD" in available_devices:
|
| 19 |
-
device = "MYRIAD"
|
| 20 |
elif "HDDL" in available_devices:
|
| 21 |
-
device = "HDDL"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
|
|
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
compiled_model = core.compile_model(model_xml, device_name=device)
|
| 29 |
|
| 30 |
-
# --------- Dummy NSFW
|
| 31 |
def detect_nsfw(text: str):
|
| 32 |
banned = ["nude", "sex", "porn", "xxx", "nsfw"]
|
| 33 |
for word in banned:
|
|
@@ -35,13 +47,11 @@ def detect_nsfw(text: str):
|
|
| 35 |
return "NSFW"
|
| 36 |
return "SFW"
|
| 37 |
|
| 38 |
-
# --------- Preprocessing
|
| 39 |
-
def preprocess_input(prompt, width, height):
|
| 40 |
-
#
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
input_data = np.random.rand(1, 3, height, width).astype(np.float32) # Dummy data for input
|
| 44 |
-
return input_data
|
| 45 |
|
| 46 |
# --------- Inference function ---------
|
| 47 |
def infer(
|
|
@@ -56,31 +66,25 @@ def infer(
|
|
| 56 |
progress=gr.Progress(track_tqdm=True),
|
| 57 |
):
|
| 58 |
if detect_nsfw(prompt) == "NSFW":
|
| 59 |
-
raise
|
| 60 |
|
| 61 |
if randomize_seed:
|
| 62 |
seed = random.randint(0, np.iinfo(np.int32).max)
|
| 63 |
|
| 64 |
-
# Adjust width and height to be divisible by 8
|
| 65 |
width = (width // 8) * 8
|
| 66 |
height = (height // 8) * 8
|
| 67 |
|
| 68 |
-
#
|
| 69 |
-
input_data = preprocess_input(prompt, width, height)
|
| 70 |
|
| 71 |
-
# Run inference
|
| 72 |
-
|
| 73 |
-
output_tensor = compiled_model.output(0) # Get output layer
|
| 74 |
|
| 75 |
-
#
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
# Post-process the result (convert to image)
|
| 79 |
-
output_image = np.random.rand(3, width, height) # Dummy image for now
|
| 80 |
-
output_image = np.transpose(output_image, (1, 2, 0)) # Convert from (C, H, W) to (H, W, C)
|
| 81 |
output_image = (output_image * 255).astype(np.uint8)
|
| 82 |
pil_image = Image.fromarray(output_image)
|
| 83 |
-
|
| 84 |
return pil_image, seed
|
| 85 |
|
| 86 |
# --------- UI ---------
|
|
@@ -90,80 +94,29 @@ examples = [
|
|
| 90 |
"A bowl of ramen in anime style",
|
| 91 |
]
|
| 92 |
|
| 93 |
-
css
|
| 94 |
-
#col-container {
|
| 95 |
-
margin: 0 auto;
|
| 96 |
-
max-width: 640px;
|
| 97 |
-
}
|
| 98 |
-
"""
|
| 99 |
-
|
| 100 |
-
with gr.Blocks(css=css) as demo:
|
| 101 |
with gr.Column(elem_id="col-container"):
|
| 102 |
-
gr.Markdown("
|
| 103 |
|
| 104 |
with gr.Row():
|
| 105 |
-
prompt = gr.Text(
|
| 106 |
-
|
| 107 |
-
show_label=False,
|
| 108 |
-
max_lines=1,
|
| 109 |
-
placeholder="Enter your prompt",
|
| 110 |
-
container=False,
|
| 111 |
-
)
|
| 112 |
-
run_button = gr.Button("Run", scale=0, variant="primary")
|
| 113 |
|
| 114 |
-
result = gr.Image(label="
|
| 115 |
|
| 116 |
with gr.Accordion("Advanced Settings", open=False):
|
| 117 |
-
negative_prompt = gr.Text(
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
visible=True,
|
| 122 |
-
)
|
| 123 |
-
|
| 124 |
-
seed = gr.Slider(
|
| 125 |
-
label="Seed",
|
| 126 |
-
minimum=0,
|
| 127 |
-
maximum=np.iinfo(np.int32).max,
|
| 128 |
-
step=1,
|
| 129 |
-
value=0,
|
| 130 |
-
)
|
| 131 |
-
|
| 132 |
-
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 133 |
|
| 134 |
with gr.Row():
|
| 135 |
-
width = gr.Slider(
|
| 136 |
-
|
| 137 |
-
minimum=256,
|
| 138 |
-
maximum=1024,
|
| 139 |
-
step=8,
|
| 140 |
-
value=512,
|
| 141 |
-
)
|
| 142 |
-
|
| 143 |
-
height = gr.Slider(
|
| 144 |
-
label="Height",
|
| 145 |
-
minimum=256,
|
| 146 |
-
maximum=1024,
|
| 147 |
-
step=8,
|
| 148 |
-
value=512,
|
| 149 |
-
)
|
| 150 |
|
| 151 |
with gr.Row():
|
| 152 |
-
guidance_scale = gr.Slider(
|
| 153 |
-
|
| 154 |
-
minimum=0.0,
|
| 155 |
-
maximum=15.0,
|
| 156 |
-
step=0.1,
|
| 157 |
-
value=5.0,
|
| 158 |
-
)
|
| 159 |
-
|
| 160 |
-
num_inference_steps = gr.Slider(
|
| 161 |
-
label="Number of inference steps",
|
| 162 |
-
minimum=1,
|
| 163 |
-
maximum=30,
|
| 164 |
-
step=1,
|
| 165 |
-
value=15,
|
| 166 |
-
)
|
| 167 |
|
| 168 |
gr.Examples(examples=examples, inputs=[prompt])
|
| 169 |
|
|
|
|
| 4 |
import torch
|
| 5 |
from openvino.runtime import Core
|
| 6 |
from PIL import Image
|
| 7 |
+
from huggingface_hub import hf_hub_download
|
| 8 |
+
import os
|
| 9 |
|
| 10 |
+
print("🔧 Initializing OpenVINO Runtime...")
|
| 11 |
|
| 12 |
+
# --------- Detect OpenVINO Device ---------
|
| 13 |
core = Core()
|
| 14 |
available_devices = core.available_devices
|
| 15 |
+
device = "CPU"
|
| 16 |
|
| 17 |
if "GPU" in available_devices:
|
| 18 |
device = "GPU"
|
| 19 |
elif "MYRIAD" in available_devices:
|
| 20 |
+
device = "MYRIAD"
|
| 21 |
elif "HDDL" in available_devices:
|
| 22 |
+
device = "HDDL"
|
| 23 |
+
|
| 24 |
+
print(f"✅ Using device: {device}")
|
| 25 |
+
|
| 26 |
+
# --------- Download model from HF Hub ---------
|
| 27 |
+
model_repo = "HARRY07979/sd-v1-5-openvino"
|
| 28 |
+
model_filename = "stable-diffusion-v1-5.xml" # you may need to adjust based on actual file
|
| 29 |
+
|
| 30 |
+
model_path_xml = hf_hub_download(repo_id=model_repo, filename=model_filename)
|
| 31 |
+
model_path_bin = model_path_xml.replace(".xml", ".bin")
|
| 32 |
+
|
| 33 |
+
print("📦 Model files downloaded")
|
| 34 |
|
| 35 |
+
# --------- Compile OpenVINO model ---------
|
| 36 |
+
compiled_model = core.compile_model(model_path_xml, device)
|
| 37 |
|
| 38 |
+
# Get input and output tensor names
|
| 39 |
+
input_layer = compiled_model.input(0)
|
| 40 |
+
output_layer = compiled_model.output(0)
|
|
|
|
| 41 |
|
| 42 |
+
# --------- Dummy NSFW filter ---------
|
| 43 |
def detect_nsfw(text: str):
|
| 44 |
banned = ["nude", "sex", "porn", "xxx", "nsfw"]
|
| 45 |
for word in banned:
|
|
|
|
| 47 |
return "NSFW"
|
| 48 |
return "SFW"
|
| 49 |
|
| 50 |
+
# --------- Dummy Preprocessing (you should integrate tokenizer/text encoder for real use) ---------
|
| 51 |
+
def preprocess_input(prompt, width, height, seed):
|
| 52 |
+
# Just generate dummy latent vector
|
| 53 |
+
np.random.seed(seed)
|
| 54 |
+
return np.random.rand(1, 3, height, width).astype(np.float32)
|
|
|
|
|
|
|
| 55 |
|
| 56 |
# --------- Inference function ---------
|
| 57 |
def infer(
|
|
|
|
| 66 |
progress=gr.Progress(track_tqdm=True),
|
| 67 |
):
|
| 68 |
if detect_nsfw(prompt) == "NSFW":
|
| 69 |
+
raise gr.Error("⚠️ Prompt contains NSFW content. Please use a safe prompt.")
|
| 70 |
|
| 71 |
if randomize_seed:
|
| 72 |
seed = random.randint(0, np.iinfo(np.int32).max)
|
| 73 |
|
|
|
|
| 74 |
width = (width // 8) * 8
|
| 75 |
height = (height // 8) * 8
|
| 76 |
|
| 77 |
+
# Prepare dummy input for now
|
| 78 |
+
input_data = preprocess_input(prompt, width, height, seed)
|
| 79 |
|
| 80 |
+
# Run OpenVINO inference
|
| 81 |
+
result = compiled_model([input_data])[output_layer]
|
|
|
|
| 82 |
|
| 83 |
+
# Postprocess dummy result
|
| 84 |
+
output_image = np.clip(result[0].transpose(1, 2, 0), 0, 1)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
output_image = (output_image * 255).astype(np.uint8)
|
| 86 |
pil_image = Image.fromarray(output_image)
|
| 87 |
+
|
| 88 |
return pil_image, seed
|
| 89 |
|
| 90 |
# --------- UI ---------
|
|
|
|
| 94 |
"A bowl of ramen in anime style",
|
| 95 |
]
|
| 96 |
|
| 97 |
+
with gr.Blocks(css="#col-container { max-width: 640px; margin: auto; }") as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
with gr.Column(elem_id="col-container"):
|
| 99 |
+
gr.Markdown("## 🧠 SD 1.5 OpenVINO via HARRY07979 on HuggingFace")
|
| 100 |
|
| 101 |
with gr.Row():
|
| 102 |
+
prompt = gr.Text(label="Prompt", placeholder="Enter your prompt", show_label=False)
|
| 103 |
+
run_button = gr.Button("Generate", variant="primary")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
+
result = gr.Image(label="Output", show_label=False)
|
| 106 |
|
| 107 |
with gr.Accordion("Advanced Settings", open=False):
|
| 108 |
+
negative_prompt = gr.Text(label="Negative Prompt", placeholder="e.g. low quality, blurry")
|
| 109 |
+
|
| 110 |
+
seed = gr.Slider(label="Seed", minimum=0, maximum=np.iinfo(np.int32).max, step=1, value=0)
|
| 111 |
+
randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
with gr.Row():
|
| 114 |
+
width = gr.Slider(label="Width", minimum=256, maximum=1024, step=8, value=512)
|
| 115 |
+
height = gr.Slider(label="Height", minimum=256, maximum=1024, step=8, value=512)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
with gr.Row():
|
| 118 |
+
guidance_scale = gr.Slider(label="Guidance Scale", minimum=0.0, maximum=15.0, step=0.1, value=7.5)
|
| 119 |
+
num_inference_steps = gr.Slider(label="Steps", minimum=1, maximum=50, step=1, value=25)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
gr.Examples(examples=examples, inputs=[prompt])
|
| 122 |
|