Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,12 +6,19 @@ from PIL import Image
|
|
| 6 |
import time
|
| 7 |
from datetime import datetime
|
| 8 |
from optimum.intel import OVStableDiffusionPipeline
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# Đảm bảo tính tái tạo
|
| 11 |
torch.manual_seed(0)
|
| 12 |
np.random.seed(0)
|
| 13 |
random.seed(0)
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
print("🔧 Loading OpenVINO pipeline: HARRY07979/sd-v1-5-openvino")
|
| 16 |
|
| 17 |
try:
|
|
@@ -35,60 +42,64 @@ except Exception as e:
|
|
| 35 |
print(f"Error loading local model: {e}")
|
| 36 |
raise RuntimeError("Failed to load the pipeline.")
|
| 37 |
|
|
|
|
| 38 |
pipeline.to("cpu")
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
],
|
| 53 |
-
"medium": [
|
| 54 |
-
"bikini", "swimwear", "sexy", "succubus", "leather", "latex", "stockings", "miniskirt",
|
| 55 |
-
"cleavage", "thighs", "ass", "butt", "skirt", "dress", "topless", "wet", "moaning",
|
| 56 |
-
"spread", "legs apart", "tight", "revealing", "provocative", "suggestive", "flirty"
|
| 57 |
-
],
|
| 58 |
-
"low": [
|
| 59 |
-
"girl", "woman", "man", "boy", "beach", "bedroom", "kiss", "hug", "dance", "pose",
|
| 60 |
-
"model", "photo", "art", "illustration", "anime", "cosplay", "costume", "fashion"
|
| 61 |
-
]
|
| 62 |
-
}
|
| 63 |
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
"blowjob scene", "anal sex", "titty fuck", "gang rape", "group sex", "public sex",
|
| 75 |
-
"hidden camera", "peeing girl", "pooping girl", "covered in blood", "cutting flesh",
|
| 76 |
-
"snorting cocaine", "injecting heroin", "hallucinating", "smoking weed"
|
| 77 |
-
]
|
| 78 |
-
for phrase in nsfw_phrases:
|
| 79 |
-
if phrase in prompt_lower:
|
| 80 |
-
return True
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
return True
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
return False
|
| 93 |
|
| 94 |
def infer(
|
|
@@ -108,28 +119,44 @@ def infer(
|
|
| 108 |
print(f"⏰ Run at: {current_time}")
|
| 109 |
print(f"📝 Prompt: {prompt}")
|
| 110 |
print(f"🚫 Negative Prompt: {negative_prompt or '[None]'}")
|
|
|
|
|
|
|
| 111 |
if detect_nsfw(prompt):
|
| 112 |
raise gr.Error("⚠️ Prompt contains NSFW content. Please use a safe prompt.")
|
|
|
|
| 113 |
if randomize_seed:
|
| 114 |
seed = random.randint(0, np.iinfo(np.int32).max)
|
| 115 |
print(f"🎲 Random Seed generated: {seed}")
|
| 116 |
else:
|
| 117 |
print(f"🔢 Using fixed Seed: {seed}")
|
|
|
|
|
|
|
| 118 |
width = (width // 8) * 8
|
| 119 |
height = (height // 8) * 8
|
| 120 |
print(f"🖼️ Image Size: {width}x{height}")
|
| 121 |
print(f"🎯 Guidance Scale: {guidance_scale}")
|
| 122 |
print(f"📈 Inference Steps: {num_inference_steps}")
|
|
|
|
|
|
|
| 123 |
generator = torch.Generator("cpu").manual_seed(seed)
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
image = result.images[0]
|
| 134 |
return image, seed
|
| 135 |
|
|
@@ -138,11 +165,26 @@ css = """
|
|
| 138 |
margin: 0 auto;
|
| 139 |
max-width: 640px;
|
| 140 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
"""
|
| 142 |
|
| 143 |
with gr.Blocks(css=css) as demo:
|
| 144 |
with gr.Column(elem_id="col-container"):
|
| 145 |
-
gr.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
with gr.Row():
|
| 147 |
prompt = gr.Text(label="Prompt", placeholder="Enter your prompt", show_label=False)
|
| 148 |
run_button = gr.Button("Generate", variant="primary")
|
|
@@ -180,6 +222,9 @@ with gr.Blocks(css=css) as demo:
|
|
| 180 |
],
|
| 181 |
outputs=[result, seed],
|
| 182 |
)
|
|
|
|
|
|
|
|
|
|
| 183 |
|
| 184 |
if __name__ == "__main__":
|
| 185 |
demo.launch()
|
|
|
|
| 6 |
import time
|
| 7 |
from datetime import datetime
|
| 8 |
from optimum.intel import OVStableDiffusionPipeline
|
| 9 |
+
import re
|
| 10 |
+
from functools import lru_cache
|
| 11 |
|
| 12 |
# Đảm bảo tính tái tạo
|
| 13 |
torch.manual_seed(0)
|
| 14 |
np.random.seed(0)
|
| 15 |
random.seed(0)
|
| 16 |
|
| 17 |
+
# Tối ưu hóa bộ nhớ và tốc độ
|
| 18 |
+
torch.set_num_threads(1) # Giảm xung đột thread
|
| 19 |
+
torch.backends.mkldnn.enabled = True # Kích hoạt MKL-DNN
|
| 20 |
+
torch.backends.openmp.enabled = True
|
| 21 |
+
|
| 22 |
print("🔧 Loading OpenVINO pipeline: HARRY07979/sd-v1-5-openvino")
|
| 23 |
|
| 24 |
try:
|
|
|
|
| 42 |
print(f"Error loading local model: {e}")
|
| 43 |
raise RuntimeError("Failed to load the pipeline.")
|
| 44 |
|
| 45 |
+
# Tối ưu hóa pipeline
|
| 46 |
pipeline.to("cpu")
|
| 47 |
+
pipeline.model.half() # Sử dụng half precision để tăng tốc
|
| 48 |
+
pipeline.compile() # Biên dịch mô hình nếu hỗ trợ
|
| 49 |
|
| 50 |
+
# Tối ưu hóa từ điển NSFW - sử dụng set để tìm kiếm nhanh hơn
|
| 51 |
+
NSFW_HIGH = {
|
| 52 |
+
"nude", "naked", "sex", "porn", "xxx", "fuck", "dick", "cock", "pussy", "vagina", "penis",
|
| 53 |
+
"boobs", "tits", "breasts", "bra", "panties", "underwear", "lingerie", "orgasm", "cum",
|
| 54 |
+
"blowjob", "handjob", "masturbate", "rape", "gangbang", "incest", "hentai", "lewd",
|
| 55 |
+
"erotic", "kinky", "bondage", "bdsm", "squirt", "creampie", "threesome", "orgy", "yaoi",
|
| 56 |
+
"yuri", "futanari", "cunnilingus", "fellatio", "anal", "paizuri", "bukkake", "guro",
|
| 57 |
+
"vore", "tentacle", "netorare", "cuckold", "exhibitionism", "voyeurism", "poop", "pee",
|
| 58 |
+
"poo", "shit", "piss", "scat", "diarrhea", "vomit", "gore", "blood", "murder",
|
| 59 |
+
"torture", "suicide", "decapitation", "mutilation", "drugs", "cocaine", "heroin",
|
| 60 |
+
"lsd", "ecstasy", "vlxx"
|
| 61 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
+
NSFW_MEDIUM = {
|
| 64 |
+
"bikini", "swimwear", "sexy", "succubus", "leather", "latex", "stockings", "miniskirt",
|
| 65 |
+
"cleavage", "thighs", "ass", "butt", "skirt", "dress", "topless", "wet", "moaning",
|
| 66 |
+
"spread", "legs apart", "tight", "revealing", "provocative", "suggestive", "flirty"
|
| 67 |
+
}
|
| 68 |
|
| 69 |
+
NSFW_PHRASES = [
|
| 70 |
+
"spreading legs", "removing bra", "pulling panties", "sucking dick", "licking pussy",
|
| 71 |
+
"penetrating", "fucking scene", "hard cock", "wet pussy", "big tits", "exposed breasts",
|
| 72 |
+
"nipples visible", "ass spread", "thigh gap", "camel toe", "pussy lips", "cum on face",
|
| 73 |
+
"blowjob scene", "anal sex", "titty fuck", "gang rape", "group sex", "public sex",
|
| 74 |
+
"hidden camera", "peeing girl", "pooping girl", "covered in blood", "cutting flesh",
|
| 75 |
+
"snorting cocaine", "injecting heroin", "hallucinating", "smoking weed"
|
| 76 |
+
]
|
| 77 |
|
| 78 |
+
SENSITIVE_CONTEXT = {
|
| 79 |
+
"spread", "removing", "pulling", "sucking", "licking", "penetrating",
|
| 80 |
+
"fucking", "hard", "wet", "exposed", "visible", "ass", "tight", "revealing"
|
| 81 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
+
# Tối ưu hóa hàm NSFW detection
|
| 84 |
+
@lru_cache(maxsize=1024)
|
| 85 |
+
def detect_nsfw(prompt: str):
|
| 86 |
+
prompt_lower = prompt.lower()
|
| 87 |
+
|
| 88 |
+
# Kiểm tra từ khóa cao
|
| 89 |
+
words = set(re.findall(r'\b\w+\b', prompt_lower))
|
| 90 |
+
if words & NSFW_HIGH:
|
| 91 |
+
return True
|
| 92 |
+
|
| 93 |
+
# Kiểm tra cụm từ NSFW
|
| 94 |
+
for phrase in NSFW_PHRASES:
|
| 95 |
+
if phrase in prompt_lower:
|
| 96 |
return True
|
| 97 |
+
|
| 98 |
+
# Kiểm tra từ khóa trung bình + ngữ cảnh nhạy cảm
|
| 99 |
+
medium_matches = words & NSFW_MEDIUM
|
| 100 |
+
if medium_matches and (words & SENSITIVE_CONTEXT):
|
| 101 |
+
return True
|
| 102 |
+
|
| 103 |
return False
|
| 104 |
|
| 105 |
def infer(
|
|
|
|
| 119 |
print(f"⏰ Run at: {current_time}")
|
| 120 |
print(f"📝 Prompt: {prompt}")
|
| 121 |
print(f"🚫 Negative Prompt: {negative_prompt or '[None]'}")
|
| 122 |
+
|
| 123 |
+
# Tối ưu hóa kiểm tra NSFW
|
| 124 |
if detect_nsfw(prompt):
|
| 125 |
raise gr.Error("⚠️ Prompt contains NSFW content. Please use a safe prompt.")
|
| 126 |
+
|
| 127 |
if randomize_seed:
|
| 128 |
seed = random.randint(0, np.iinfo(np.int32).max)
|
| 129 |
print(f"🎲 Random Seed generated: {seed}")
|
| 130 |
else:
|
| 131 |
print(f"🔢 Using fixed Seed: {seed}")
|
| 132 |
+
|
| 133 |
+
# Tối ưu hóa kích thước ảnh
|
| 134 |
width = (width // 8) * 8
|
| 135 |
height = (height // 8) * 8
|
| 136 |
print(f"🖼️ Image Size: {width}x{height}")
|
| 137 |
print(f"🎯 Guidance Scale: {guidance_scale}")
|
| 138 |
print(f"📈 Inference Steps: {num_inference_steps}")
|
| 139 |
+
|
| 140 |
+
# Tối ưu hóa generator
|
| 141 |
generator = torch.Generator("cpu").manual_seed(seed)
|
| 142 |
+
|
| 143 |
+
# Sử dụng inference_mode để tăng tốc
|
| 144 |
+
with torch.inference_mode():
|
| 145 |
+
result = pipeline(
|
| 146 |
+
prompt=prompt,
|
| 147 |
+
negative_prompt=negative_prompt,
|
| 148 |
+
guidance_scale=guidance_scale,
|
| 149 |
+
num_inference_steps=num_inference_steps,
|
| 150 |
+
width=width,
|
| 151 |
+
height=height,
|
| 152 |
+
generator=generator,
|
| 153 |
+
# Tối ưu hóa batch
|
| 154 |
+
batch_size=1,
|
| 155 |
+
# Tăng chất lượng
|
| 156 |
+
eta=0.0, # Giảm nhiễu
|
| 157 |
+
use_karras_sigmas=True, # Cải thiện chất lượng
|
| 158 |
+
)
|
| 159 |
+
|
| 160 |
image = result.images[0]
|
| 161 |
return image, seed
|
| 162 |
|
|
|
|
| 165 |
margin: 0 auto;
|
| 166 |
max-width: 640px;
|
| 167 |
}
|
| 168 |
+
.donate-button {
|
| 169 |
+
background-color: #FFDD00 !important;
|
| 170 |
+
color: #000000 !important;
|
| 171 |
+
}
|
| 172 |
+
"""
|
| 173 |
+
|
| 174 |
+
# Hàm JavaScript để mở trang donate khi nhấn nút
|
| 175 |
+
donate_js = """
|
| 176 |
+
function() {
|
| 177 |
+
window.open('https://buymeacoffee.com/harry07?status=1', '_blank');
|
| 178 |
+
}
|
| 179 |
"""
|
| 180 |
|
| 181 |
with gr.Blocks(css=css) as demo:
|
| 182 |
with gr.Column(elem_id="col-container"):
|
| 183 |
+
with gr.Row():
|
| 184 |
+
gr.Markdown("## Stable Diffusion v1.5 DEMO")
|
| 185 |
+
# Thêm nút Donate với biểu tượng ☕
|
| 186 |
+
donate_btn = gr.Button("☕ Donate", elem_classes="donate-button")
|
| 187 |
+
|
| 188 |
with gr.Row():
|
| 189 |
prompt = gr.Text(label="Prompt", placeholder="Enter your prompt", show_label=False)
|
| 190 |
run_button = gr.Button("Generate", variant="primary")
|
|
|
|
| 222 |
],
|
| 223 |
outputs=[result, seed],
|
| 224 |
)
|
| 225 |
+
|
| 226 |
+
# Gán sự kiện click cho nút Donate
|
| 227 |
+
donate_btn.click(None, js=donate_js)
|
| 228 |
|
| 229 |
if __name__ == "__main__":
|
| 230 |
demo.launch()
|