Update app.py
Browse files
app.py
CHANGED
|
@@ -11,14 +11,7 @@ import re
|
|
| 11 |
from cohere import ClientV2
|
| 12 |
|
| 13 |
# ------------------------------------------------------------
|
| 14 |
-
#
|
| 15 |
-
# ------------------------------------------------------------
|
| 16 |
-
pipe = None
|
| 17 |
-
img2img_pipe = None
|
| 18 |
-
device = None
|
| 19 |
-
|
| 20 |
-
# ------------------------------------------------------------
|
| 21 |
-
# INITIALIZE COHERE CLIENT FOR TRANSLATIONS AND PROMPT GENERATION
|
| 22 |
# ------------------------------------------------------------
|
| 23 |
coh_api_key = os.getenv("COH_API")
|
| 24 |
if not coh_api_key:
|
|
@@ -34,84 +27,15 @@ else:
|
|
| 34 |
|
| 35 |
|
| 36 |
# ------------------------------------------------------------
|
| 37 |
-
#
|
| 38 |
# ------------------------------------------------------------
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
if pipe is not None:
|
| 44 |
-
return pipe
|
| 45 |
-
|
| 46 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 47 |
-
print(f"[INFO] Loading Text-to-Image pipeline on device: {device}")
|
| 48 |
-
|
| 49 |
-
if torch.cuda.is_available():
|
| 50 |
-
pipe = StableDiffusionXLPipeline.from_pretrained(
|
| 51 |
-
"Heartsync/NSFW-Uncensored",
|
| 52 |
-
torch_dtype=torch.float16,
|
| 53 |
-
variant="fp16",
|
| 54 |
-
use_safetensors=True,
|
| 55 |
-
)
|
| 56 |
-
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
|
| 57 |
-
pipe.to(device)
|
| 58 |
-
|
| 59 |
-
# Force sub-modules to fp16 for VRAM efficiency
|
| 60 |
-
for sub in (pipe.text_encoder, pipe.text_encoder_2, pipe.vae, pipe.unet):
|
| 61 |
-
if sub is not None:
|
| 62 |
-
sub.to(torch.float16)
|
| 63 |
-
else:
|
| 64 |
-
pipe = StableDiffusionXLPipeline.from_pretrained(
|
| 65 |
-
"Heartsync/NSFW-Uncensored",
|
| 66 |
-
torch_dtype=torch.float32,
|
| 67 |
-
use_safetensors=True,
|
| 68 |
-
)
|
| 69 |
-
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
|
| 70 |
-
pipe.to(device)
|
| 71 |
-
|
| 72 |
-
print("[INFO] Text-to-Image pipeline loaded successfully!")
|
| 73 |
-
return pipe
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
def load_img2img_pipeline():
|
| 77 |
-
"""Lazily load the image-to-image pipeline on first use"""
|
| 78 |
-
global img2img_pipe, device
|
| 79 |
-
|
| 80 |
-
if img2img_pipe is not None:
|
| 81 |
-
return img2img_pipe
|
| 82 |
-
|
| 83 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 84 |
-
print(f"[INFO] Loading Image-to-Image pipeline on device: {device}")
|
| 85 |
-
|
| 86 |
-
if torch.cuda.is_available():
|
| 87 |
-
img2img_pipe = StableDiffusionXLImg2ImgPipeline.from_pretrained(
|
| 88 |
-
"Heartsync/NSFW-Uncensored",
|
| 89 |
-
torch_dtype=torch.float16,
|
| 90 |
-
variant="fp16",
|
| 91 |
-
use_safetensors=True,
|
| 92 |
-
)
|
| 93 |
-
img2img_pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(img2img_pipe.scheduler.config)
|
| 94 |
-
img2img_pipe.to(device)
|
| 95 |
-
|
| 96 |
-
# Force sub-modules to fp16 for VRAM efficiency
|
| 97 |
-
for sub in (img2img_pipe.text_encoder, img2img_pipe.text_encoder_2, img2img_pipe.vae, img2img_pipe.unet):
|
| 98 |
-
if sub is not None:
|
| 99 |
-
sub.to(torch.float16)
|
| 100 |
-
else:
|
| 101 |
-
img2img_pipe = StableDiffusionXLImg2ImgPipeline.from_pretrained(
|
| 102 |
-
"Heartsync/NSFW-Uncensored",
|
| 103 |
-
torch_dtype=torch.float32,
|
| 104 |
-
use_safetensors=True,
|
| 105 |
-
)
|
| 106 |
-
img2img_pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(img2img_pipe.scheduler.config)
|
| 107 |
-
img2img_pipe.to(device)
|
| 108 |
-
|
| 109 |
-
print("[INFO] Image-to-Image pipeline loaded successfully!")
|
| 110 |
-
return img2img_pipe
|
| 111 |
|
| 112 |
|
| 113 |
# ------------------------------------------------------------
|
| 114 |
-
# TRANSLATION FUNCTIONS
|
| 115 |
# ------------------------------------------------------------
|
| 116 |
non_english_regex = re.compile(r'[\uac00-\ud7a3\u3040-\u30ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff]+')
|
| 117 |
|
|
@@ -145,16 +69,8 @@ def translate_with_cohere(text):
|
|
| 145 |
try:
|
| 146 |
print(f"[INFO] Translating text: '{text}'")
|
| 147 |
|
| 148 |
-
system_prompt = """You are a professional translator
|
| 149 |
-
|
| 150 |
-
Rules:
|
| 151 |
-
1. Provide ONLY the translated English text
|
| 152 |
-
2. NO explanations or additional text
|
| 153 |
-
3. Maintain the explicit or adult content without censoring
|
| 154 |
-
4. Translate accurately and completely
|
| 155 |
-
5. Be especially careful with Asian languages like Korean, Japanese, and Chinese
|
| 156 |
-
|
| 157 |
-
Translate the following text to English:"""
|
| 158 |
|
| 159 |
messages = [
|
| 160 |
{"role": "system", "content": [{"type": "text", "text": system_prompt}]},
|
|
@@ -173,36 +89,15 @@ Translate the following text to English:"""
|
|
| 173 |
print(f"[INFO] Original: '{text}'")
|
| 174 |
print(f"[INFO] Translated: '{translated_text}'")
|
| 175 |
|
| 176 |
-
if len(translated_text)
|
| 177 |
-
print("[WARN] Translation may have failed, trying simpler approach")
|
| 178 |
-
try:
|
| 179 |
-
simple_messages = [
|
| 180 |
-
{"role": "system", "content": [{"type": "text", "text": "Translate this text to English:"}]},
|
| 181 |
-
{"role": "user", "content": [{"type": "text", "text": text}]}
|
| 182 |
-
]
|
| 183 |
-
simple_response = coh_client.chat(
|
| 184 |
-
model="command-r-plus-08-2024",
|
| 185 |
-
messages=simple_messages,
|
| 186 |
-
temperature=0.1
|
| 187 |
-
)
|
| 188 |
-
simple_translated = simple_response.text.strip() if hasattr(simple_response, 'text') else str(simple_response)
|
| 189 |
-
if len(simple_translated) > 3 and simple_translated != text:
|
| 190 |
-
print(f"[INFO] Second attempt translation: '{simple_translated}'")
|
| 191 |
-
return simple_translated
|
| 192 |
-
except Exception as e:
|
| 193 |
-
print(f"[ERROR] Second translation attempt failed: {str(e)}")
|
| 194 |
-
return text
|
| 195 |
|
| 196 |
-
return translated_text
|
| 197 |
except Exception as e:
|
| 198 |
print(f"[ERROR] Translation failed: {str(e)}")
|
| 199 |
-
import traceback
|
| 200 |
-
traceback.print_exc()
|
| 201 |
return text
|
| 202 |
|
| 203 |
|
| 204 |
def translate_prompt_if_needed(prompt):
|
| 205 |
-
"""Helper function to translate prompt
|
| 206 |
if not is_non_english(prompt):
|
| 207 |
return prompt
|
| 208 |
|
|
@@ -210,7 +105,7 @@ def translate_prompt_if_needed(prompt):
|
|
| 210 |
return prompt
|
| 211 |
|
| 212 |
try:
|
| 213 |
-
trans_system = "
|
| 214 |
|
| 215 |
trans_response = coh_client.chat(
|
| 216 |
model="command-r-plus-08-2024",
|
|
@@ -221,22 +116,8 @@ def translate_prompt_if_needed(prompt):
|
|
| 221 |
temperature=0.1
|
| 222 |
)
|
| 223 |
|
| 224 |
-
translated_prompt = None
|
| 225 |
-
|
| 226 |
if hasattr(trans_response, 'text'):
|
| 227 |
-
translated_prompt = trans_response.text
|
| 228 |
-
elif hasattr(trans_response, 'response'):
|
| 229 |
-
translated_prompt = trans_response.response
|
| 230 |
-
elif isinstance(trans_response, dict) and 'text' in trans_response:
|
| 231 |
-
translated_prompt = trans_response['text']
|
| 232 |
-
else:
|
| 233 |
-
response_str = str(trans_response)
|
| 234 |
-
match = re.search(r"text=['\"](.*?)['\"]", response_str)
|
| 235 |
-
if match:
|
| 236 |
-
translated_prompt = match.group(1)
|
| 237 |
-
|
| 238 |
-
if translated_prompt:
|
| 239 |
-
translated_prompt = translated_prompt.strip()
|
| 240 |
print(f"[SUCCESS] Translated: '{prompt}' -> '{translated_prompt}'")
|
| 241 |
return translated_prompt
|
| 242 |
|
|
@@ -251,35 +132,11 @@ def translate_prompt_if_needed(prompt):
|
|
| 251 |
# ------------------------------------------------------------
|
| 252 |
prompt_examples = [
|
| 253 |
"The shy college girl, with glasses and a tight plaid skirt, nervously approaches her professor",
|
| 254 |
-
"Her skirt rose a little higher with each gentle push, a soft blush of blush spreading across her cheeks
|
| 255 |
-
"
|
| 256 |
-
"
|
| 257 |
-
"
|
| 258 |
-
"
|
| 259 |
-
"A girl with long, black hair is sleeping on her desk in the classroom. Her skirt has ridden up, revealing her thighs, and a trail of drool escapes her slightly parted lips.",
|
| 260 |
-
"The waves rolled gently, a slow, sweet kiss of the lip, a slow, slow build of anticipation as their toes bumped gently – a slow, sweet kiss of the lip, a promise of more to come.",
|
| 261 |
-
"Her elegant silk gown swayed gracefully as she approached him, the delicate fabric brushing against her legs. A warm blush spread across her cheeks as she felt his breath on her face.",
|
| 262 |
-
"Her white blouse and light cotton skirt rose a little higher with each gentle push, a soft blush spreading across her cheeks as she felt the satisfying warmth of his breath on her cheek.",
|
| 263 |
-
"A woman in a business suit having her skirt lifted by a man, and then being sexually assaulted.",
|
| 264 |
-
"The older woman sits on the man's lap by the fireplace, his hands resting on her hips. She is unbuttoning his vest, her expression focused and intense. He takes control of the situation as she finishes unbuttoning his shirt, pushing her onto her back and begins to have sex with her.",
|
| 265 |
-
"There is a woman with long black hair. Her face features alluring eyes and full lips, with a slender figure adorned in black lace lingerie. She lies on the bed, loosening her lingerie strap with one hand while seductively glancing downward.",
|
| 266 |
-
"In a dimly lit room, the same woman teases with her dark, flowing hair, now covering her voluptuous breasts, while a black garter belt accentuates her thighs. She sits on the sofa, leaning back, lifting one leg to expose her most private areas through the sheer lingerie.",
|
| 267 |
-
"A woman with glasses, lying on the bed in just her bra, spreads her legs wide, revealing all! She wears a sultry expression, gazing directly at the viewer with her brown eyes, her short black hair cascading over the pillow. Her slim figure, accentuated by the lacy lingerie, exudes a seductive aura.",
|
| 268 |
-
"A soft focus on the girl's face, eyes closed, biting her lip, as her roommate performs oral pleasure, the experienced woman's hair cascading between her thighs.",
|
| 269 |
-
"A woman in a blue hanbok sits on a wooden floor, her legs folded beneath her, gazing out of a window, the sunlight highlighting the graceful lines of her clothing.",
|
| 270 |
-
"The couple, immersed in a wooden outdoor bath, share an intimate moment, her wet kimono clinging to her curves, his hands exploring her body beneath the water's surface.",
|
| 271 |
-
"A steamy shower scene, the twins embrace under the warm water, their soapy hands gliding over each other's curves, their passion intensifying as they explore uncharted territories.",
|
| 272 |
-
"The teacher, with a firm grip, pins the student against the blackboard, her skirt hiked up, exposing her delicate lace panties. Their heavy breathing echoes in the quiet room as they share an intense, intimate moment.",
|
| 273 |
-
"After hours, the girl sits on top of the teacher's lap, riding him on the classroom floor, her hair cascading over her face as she moves with increasing intensity, their bodies glistening with sweat.",
|
| 274 |
-
"In the dimly lit dorm room, the roommates lay entangled in a passionate embrace, their naked bodies glistening with sweat, as the experienced woman teaches her lover the art of kissing and touching.",
|
| 275 |
-
"The once-innocent student, now confident, takes charge, straddling her lover on the couch, their bare skin illuminated by the warm glow of the sunset through the window.",
|
| 276 |
-
"A close-up of the secretary's hand unzipping her boss's dress shirt, her fingers gently caressing his chest, their eyes locked in a heated embrace in the supply closet.",
|
| 277 |
-
"The secretary, in a tight pencil skirt and silk blouse, leans back on the boss's desk, her legs wrapped around his waist, her blouse unbuttoned, revealing her lace bra, as he passionately kisses her, his hands exploring her body.",
|
| 278 |
-
"On the living room couch, one twin sits astride her sister's lap, their lips locked in a passionate kiss, their hands tangled in each other's hair, unraveling a new level of intimacy.",
|
| 279 |
-
"In a dimly lit chamber, the dominant woman, dressed in a leather corset and thigh-high boots, stands tall, her hand gripping her submissive partner's hair, his eyes closed in submission as she instructs him to please her.",
|
| 280 |
-
"The dominant, in a sheer lace bodysuit, sits on a throne-like chair, her legs spread, as the submissive, on his knees, worships her with his tongue, his hands bound behind his back.",
|
| 281 |
-
"A traditional Japanese onsen, with steam rising, a young woman in a colorful kimono kneels on a tatami mat, her back to the viewer, as her male partner, also in a kimono, gently unties her obi, revealing her bare back.",
|
| 282 |
-
"In a serene outdoor setting, the woman, in a vibrant summer kimono, sits on a bench, her legs slightly spread, her partner kneeling before her, his hands gently caressing her exposed thigh.",
|
| 283 |
]
|
| 284 |
|
| 285 |
|
|
@@ -287,28 +144,24 @@ prompt_examples = [
|
|
| 287 |
# LLM PROMPT GENERATOR
|
| 288 |
# ------------------------------------------------------------
|
| 289 |
def generate_prompts(theme):
|
| 290 |
-
"""Generate optimal
|
| 291 |
try:
|
| 292 |
if coh_client is None:
|
| 293 |
-
return "Cohere API token not set.
|
| 294 |
|
| 295 |
if non_english_regex.search(theme):
|
| 296 |
theme = translate_with_cohere(theme)
|
| 297 |
|
| 298 |
-
print(f"[INFO]
|
| 299 |
|
| 300 |
-
system_prefix = """You are an expert at creating detailed
|
| 301 |
|
| 302 |
-
|
| 303 |
-
1. Generate only ONE high-quality
|
| 304 |
-
2.
|
| 305 |
-
3.
|
| 306 |
-
4.
|
| 307 |
-
5.
|
| 308 |
-
6. ONLY respond in ENGLISH, never in any other language
|
| 309 |
-
7. DO NOT include ANY prefixes, headers, or formatting - just plain text
|
| 310 |
-
|
| 311 |
-
Respond ONLY with the single prompt text in ENGLISH with NO PREFIXES of any kind."""
|
| 312 |
|
| 313 |
messages = [
|
| 314 |
{"role": "system", "content": [{"type": "text", "text": system_prefix}]},
|
|
@@ -321,89 +174,75 @@ Respond ONLY with the single prompt text in ENGLISH with NO PREFIXES of any kind
|
|
| 321 |
temperature=0.8
|
| 322 |
)
|
| 323 |
|
| 324 |
-
if hasattr(response, 'text')
|
| 325 |
-
generated_prompt = response.text
|
| 326 |
-
else:
|
| 327 |
-
try:
|
| 328 |
-
response_str = str(response)
|
| 329 |
-
if 'text=' in response_str:
|
| 330 |
-
text_match = re.search(r"text=['\"]([^'\"]+)['\"]", response_str)
|
| 331 |
-
if text_match:
|
| 332 |
-
generated_prompt = text_match.group(1)
|
| 333 |
-
else:
|
| 334 |
-
generated_prompt = response_str
|
| 335 |
-
else:
|
| 336 |
-
generated_prompt = response_str
|
| 337 |
-
except:
|
| 338 |
-
generated_prompt = str(response)
|
| 339 |
|
| 340 |
if non_english_regex.search(generated_prompt):
|
| 341 |
-
print("[INFO] Translating non-English prompt to English")
|
| 342 |
generated_prompt = translate_with_cohere(generated_prompt)
|
| 343 |
|
| 344 |
-
generated_prompt = re.sub(r'^AI🐼:\s*', '', generated_prompt)
|
| 345 |
-
generated_prompt = re.sub(r'^\d+[\.\)]\s*', '', generated_prompt)
|
| 346 |
-
generated_prompt = re.sub(r'^(Prompt|Response|Result|Output):\s*', '', generated_prompt)
|
| 347 |
generated_prompt = re.sub(r'^["\']+|["\']+$', '', generated_prompt)
|
| 348 |
generated_prompt = generated_prompt.strip()
|
| 349 |
-
generated_prompt = re.sub(r'\s+', ' ', generated_prompt)
|
| 350 |
|
| 351 |
-
print(f"[INFO] Generated
|
| 352 |
|
| 353 |
-
if len(generated_prompt) > 10
|
| 354 |
-
return generated_prompt
|
| 355 |
-
else:
|
| 356 |
-
return "Failed to generate a valid prompt"
|
| 357 |
|
| 358 |
except Exception as e:
|
| 359 |
print(f"[ERROR] Prompt generation failed: {str(e)}")
|
| 360 |
-
|
| 361 |
-
traceback.print_exc()
|
| 362 |
-
return f"Error generating prompt: {str(e)}"
|
| 363 |
|
| 364 |
|
| 365 |
# ------------------------------------------------------------
|
| 366 |
-
# SDXL
|
| 367 |
# ------------------------------------------------------------
|
| 368 |
-
MAX_SEED = np.iinfo(np.int32).max
|
| 369 |
-
MAX_IMAGE_SIZE = 1216
|
| 370 |
-
|
| 371 |
-
|
| 372 |
@spaces.GPU(duration=120)
|
| 373 |
def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
|
| 374 |
-
"""Text-to-Image generation
|
| 375 |
-
global pipe, device
|
| 376 |
|
| 377 |
-
|
| 378 |
-
pipe = load_pipeline()
|
| 379 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 380 |
|
| 381 |
-
|
| 382 |
-
print(f"[DEBUG] Device: {device}")
|
| 383 |
-
|
| 384 |
-
# Translate prompts if needed
|
| 385 |
if is_non_english(prompt):
|
| 386 |
-
print(f"[ALERT] Non-English prompt detected
|
| 387 |
prompt = translate_prompt_if_needed(prompt)
|
| 388 |
-
print(f"[INFO]
|
| 389 |
|
| 390 |
if is_non_english(negative_prompt):
|
| 391 |
-
print(f"[ALERT] Non-English negative prompt detected: '{negative_prompt}'")
|
| 392 |
negative_prompt = translate_prompt_if_needed(negative_prompt)
|
| 393 |
-
print(f"[INFO] Final negative prompt: '{negative_prompt}'")
|
| 394 |
|
| 395 |
-
print(f"[INFO] Final prompt to use: '{prompt}'")
|
| 396 |
-
print(f"[INFO] Final negative prompt to use: '{negative_prompt}'")
|
| 397 |
-
|
| 398 |
-
if len(prompt.split()) > 60:
|
| 399 |
-
print("[WARN] Prompt >60 words — CLIP may truncate it.")
|
| 400 |
-
|
| 401 |
if randomize_seed:
|
| 402 |
seed = random.randint(0, MAX_SEED)
|
| 403 |
-
|
| 404 |
-
generator = torch.Generator(device=device).manual_seed(seed)
|
| 405 |
-
|
| 406 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 407 |
output_image = pipe(
|
| 408 |
prompt=prompt,
|
| 409 |
negative_prompt=negative_prompt,
|
|
@@ -413,53 +252,77 @@ def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance
|
|
| 413 |
height=height,
|
| 414 |
generator=generator,
|
| 415 |
).images[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 416 |
return output_image, seed
|
| 417 |
-
|
| 418 |
-
|
|
|
|
| 419 |
import traceback
|
| 420 |
traceback.print_exc()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 421 |
return Image.new("RGB", (width, height), color=(0, 0, 0)), seed
|
| 422 |
|
| 423 |
|
| 424 |
# ------------------------------------------------------------
|
| 425 |
-
# SDXL
|
| 426 |
# ------------------------------------------------------------
|
| 427 |
@spaces.GPU(duration=120)
|
| 428 |
def img2img_infer(init_image, prompt, negative_prompt, strength, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
|
| 429 |
-
"""Image-to-Image generation
|
| 430 |
-
global img2img_pipe, device
|
| 431 |
|
| 432 |
if init_image is None:
|
| 433 |
return None, seed
|
| 434 |
|
| 435 |
-
|
| 436 |
-
img2img_pipe = load_img2img_pipeline()
|
| 437 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 438 |
-
|
| 439 |
-
print(f"[DEBUG] Image-to-Image prompt received: '{prompt}'")
|
| 440 |
-
print(f"[DEBUG] Device: {device}")
|
| 441 |
|
| 442 |
-
# Translate
|
| 443 |
if is_non_english(prompt):
|
| 444 |
-
print(f"[ALERT] Non-English prompt detected: '{prompt}'")
|
| 445 |
prompt = translate_prompt_if_needed(prompt)
|
| 446 |
-
print(f"[INFO] Translated prompt: '{prompt}'")
|
| 447 |
|
| 448 |
if is_non_english(negative_prompt):
|
| 449 |
-
print(f"[ALERT] Non-English negative prompt detected: '{negative_prompt}'")
|
| 450 |
negative_prompt = translate_prompt_if_needed(negative_prompt)
|
| 451 |
-
print(f"[INFO] Translated negative prompt: '{negative_prompt}'")
|
| 452 |
|
| 453 |
if randomize_seed:
|
| 454 |
seed = random.randint(0, MAX_SEED)
|
| 455 |
|
| 456 |
-
generator = torch.Generator(device=device).manual_seed(seed)
|
| 457 |
-
|
| 458 |
-
# Preprocess image
|
| 459 |
-
init_image = init_image.convert("RGB")
|
| 460 |
-
init_image = init_image.resize((width, height), Image.Resampling.LANCZOS)
|
| 461 |
-
|
| 462 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 463 |
output_image = img2img_pipe(
|
| 464 |
prompt=prompt,
|
| 465 |
negative_prompt=negative_prompt,
|
|
@@ -469,11 +332,21 @@ def img2img_infer(init_image, prompt, negative_prompt, strength, seed, randomize
|
|
| 469 |
num_inference_steps=num_inference_steps,
|
| 470 |
generator=generator,
|
| 471 |
).images[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 472 |
return output_image, seed
|
| 473 |
-
|
| 474 |
-
|
|
|
|
| 475 |
import traceback
|
| 476 |
traceback.print_exc()
|
|
|
|
|
|
|
|
|
|
| 477 |
return None, seed
|
| 478 |
|
| 479 |
|
|
@@ -489,119 +362,45 @@ def boost_prompt(keyword):
|
|
| 489 |
return "Please enter a keyword or theme first"
|
| 490 |
|
| 491 |
if coh_client is None:
|
| 492 |
-
return "Cohere API token not set
|
| 493 |
|
| 494 |
-
print(f"[INFO] Generating boosted prompt for keyword: {keyword}")
|
| 495 |
prompt = generate_prompts(keyword)
|
| 496 |
-
|
| 497 |
-
if isinstance(prompt, str) and len(prompt) > 10 and not prompt.startswith("Error") and not prompt.startswith("Failed"):
|
| 498 |
-
return prompt.strip()
|
| 499 |
-
else:
|
| 500 |
-
return "Failed to generate a suitable prompt. Please try again with a different keyword."
|
| 501 |
|
| 502 |
|
| 503 |
# ------------------------------------------------------------
|
| 504 |
-
# UI LAYOUT
|
| 505 |
# ------------------------------------------------------------
|
| 506 |
css = """
|
| 507 |
body {background: linear-gradient(135deg, #f2e6ff 0%, #e6f0ff 100%); color: #222; font-family: 'Noto Sans', sans-serif;}
|
| 508 |
#col-container {margin: 0 auto; max-width: 768px; padding: 15px; background: rgba(255, 255, 255, 0.8); border-radius: 15px; box-shadow: 0 8px 32px rgba(31, 38, 135, 0.2);}
|
| 509 |
.gr-button {background: #7fbdf6; color: #fff; border-radius: 8px; transition: all 0.3s ease; font-weight: bold;}
|
| 510 |
.gr-button:hover {background: #5a9ae6; transform: translateY(-2px); box-shadow: 0 5px 15px rgba(0,0,0,0.1);}
|
| 511 |
-
#prompt-box textarea {font-size: 1.1rem; height: 9rem !important; background: #fff; color: #222; border-radius: 10px;
|
| 512 |
.boost-btn {background: #ff7eb6; margin-top: 5px;}
|
| 513 |
.boost-btn:hover {background: #ff5aa5;}
|
| 514 |
.random-btn {background: #9966ff; margin-top: 5px;}
|
| 515 |
.random-btn:hover {background: #8040ff;}
|
| 516 |
-
.container {animation: fadeIn 0.5s ease-in-out;}
|
| 517 |
.title {color: #6600cc; text-shadow: 1px 1px 2px rgba(0,0,0,0.1);}
|
| 518 |
-
.gr-form {border: none !important; background: transparent !important;}
|
| 519 |
-
.gr-input {border-radius: 8px !important;}
|
| 520 |
-
.gr-slider {height: 12px !important;}
|
| 521 |
-
.gr-slider .handle {height: 20px !important; width: 20px !important;}
|
| 522 |
-
.panel {border-radius: 12px; overflow: hidden; box-shadow: 0 4px 15px rgba(0,0,0,0.1);}
|
| 523 |
-
.gr-image {border-radius: 12px; overflow: hidden; transition: all 0.3s ease;}
|
| 524 |
-
.gr-image:hover {transform: scale(1.02); box-shadow: 0 8px 25px rgba(0,0,0,0.15);}
|
| 525 |
-
@keyframes fadeIn {
|
| 526 |
-
from {opacity: 0; transform: translateY(20px);}
|
| 527 |
-
to {opacity: 1; transform: translateY(0);}
|
| 528 |
-
}
|
| 529 |
-
.gr-accordion {border-radius: 10px; overflow: hidden; transition: all 0.3s ease;}
|
| 530 |
-
.gr-accordion:hover {box-shadow: 0 5px 15px rgba(0,0,0,0.1);}
|
| 531 |
"""
|
| 532 |
|
| 533 |
-
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
with gr.Blocks(
|
| 537 |
-
css=css,
|
| 538 |
-
theme=gr.themes.Soft(),
|
| 539 |
-
head="""
|
| 540 |
-
<!-- Google tag (gtag.js) -->
|
| 541 |
-
<script async src="https://www.googletagmanager.com/gtag/js?id=G-GTFK201G22"></script>
|
| 542 |
-
<script>
|
| 543 |
-
window.dataLayer = window.dataLayer || [];
|
| 544 |
-
function gtag(){dataLayer.push(arguments);}
|
| 545 |
-
gtag('js', new Date());
|
| 546 |
-
gtag('config', 'G-GTFK201G22');
|
| 547 |
-
</script>
|
| 548 |
-
"""
|
| 549 |
-
) as demo:
|
| 550 |
gr.Markdown(
|
| 551 |
-
|
| 552 |
## 🖌️ NSFW Uncensored Text & Imagery: AI Limits Explorer
|
| 553 |
|
| 554 |
-
**
|
| 555 |
-
|
| 556 |
-
{author_note}
|
| 557 |
""", elem_classes=["title"]
|
| 558 |
)
|
| 559 |
|
| 560 |
-
with gr.Group(elem_classes="model-description"):
|
| 561 |
-
gr.HTML("""
|
| 562 |
-
<p>
|
| 563 |
-
<strong>Models Use cases: </strong><br>
|
| 564 |
-
</p>
|
| 565 |
-
<div style="display: flex; justify-content: center; align-items: center; gap: 10px; flex-wrap: wrap; margin-top: 10px; margin-bottom: 20px;">
|
| 566 |
-
<a href="https://huggingface.co/spaces/Heartsync/FREE-NSFW-HUB" target="_blank">
|
| 567 |
-
<img src="https://img.shields.io/static/v1?label=huggingface&message=FREE%20NSFW%20HUB&color=%230000ff&labelColor=%23800080&logo=huggingface&logoColor=%23ffa500&style=for-the-badge" alt="badge">
|
| 568 |
-
</a>
|
| 569 |
-
<a href="https://huggingface.co/spaces/Heartsync/PornHUB" target="_blank">
|
| 570 |
-
<img src="https://img.shields.io/static/v1?label=Porn%20HUB&message=NSFW%20Uncensored&color=%23ffc0cb&labelColor=%23ffff00&logo=huggingface&logoColor=%23ffa500&style=for-the-badge" alt="badge">
|
| 571 |
-
</a>
|
| 572 |
-
<a href="https://huggingface.co/spaces/Heartsync/adult" target="_blank">
|
| 573 |
-
<img src="https://img.shields.io/static/v1?label=Text%20to%20Image%20to%20Video&message=ADULT&color=%23ff00ff&labelColor=%23000080&logo=Huggingface&logoColor=%23ffa500&style=for-the-badge" alt="badge">
|
| 574 |
-
</a>
|
| 575 |
-
<a href="https://www.humangen.ai" target="_blank">
|
| 576 |
-
<img src="https://img.shields.io/static/v1?label=100% FREE&message=AI%20Playground&color=%230000ff&labelColor=%23800080&logo=huggingface&logoColor=%23ffa500&style=for-the-badge" alt="badge">
|
| 577 |
-
</a>
|
| 578 |
-
<a href="https://huggingface.co/spaces/Heartsync/NSFW-Uncensored-image" target="_blank">
|
| 579 |
-
<img src="https://img.shields.io/static/v1?label=Image%20to%20Video&message=NSFW%20Uncensored&color=%230000ff&labelColor=%23800080&logo=Huggingface&logoColor=%23ffa500&style=for-the-badge" alt="badge">
|
| 580 |
-
</a>
|
| 581 |
-
<a href="https://huggingface.co/spaces/Heartsync/NSFW-Uncensored-video2" target="_blank">
|
| 582 |
-
<img src="https://img.shields.io/static/v1?label=Image%20to%20Video(Mirror)&message=NSFW%20Uncensored&color=%230000ff&labelColor=%23800080&logo=Huggingface&logoColor=%23ffa500&style=for-the-badge" alt="badge">
|
| 583 |
-
</a>
|
| 584 |
-
</div>
|
| 585 |
-
<p>
|
| 586 |
-
<small style="opacity: 0.8;">High-quality image generation powered by StableDiffusionXL with video generation capability. Supports long prompts and various artistic styles.</small>
|
| 587 |
-
</p>
|
| 588 |
-
""")
|
| 589 |
-
|
| 590 |
-
# State variables
|
| 591 |
-
current_image = gr.State(None)
|
| 592 |
-
current_seed = gr.State(0)
|
| 593 |
-
|
| 594 |
-
# Tabs
|
| 595 |
with gr.Tabs():
|
| 596 |
# Text-to-Image Tab
|
| 597 |
with gr.TabItem("Text to Image"):
|
| 598 |
-
with gr.Column(elem_id="col-container"
|
| 599 |
with gr.Row():
|
| 600 |
keyword_input = gr.Text(
|
| 601 |
label="Keyword Input",
|
| 602 |
-
|
| 603 |
-
max_lines=1,
|
| 604 |
-
placeholder="Enter a keyword or theme in any language to generate an optimal prompt",
|
| 605 |
value="random",
|
| 606 |
)
|
| 607 |
boost_button = gr.Button("BOOST", elem_classes=["boost-btn"])
|
|
@@ -611,131 +410,70 @@ with gr.Blocks(
|
|
| 611 |
prompt = gr.Text(
|
| 612 |
label="Prompt",
|
| 613 |
elem_id="prompt-box",
|
| 614 |
-
show_label=True,
|
| 615 |
max_lines=3,
|
| 616 |
-
placeholder="Enter
|
| 617 |
)
|
| 618 |
run_button = gr.Button("Generate", scale=0)
|
| 619 |
|
| 620 |
-
result = gr.Image(label="Generated Image"
|
| 621 |
|
| 622 |
-
with gr.Accordion("Advanced Settings", open=False
|
| 623 |
negative_prompt = gr.Text(
|
| 624 |
label="Negative prompt",
|
| 625 |
-
|
| 626 |
-
placeholder="Enter a negative prompt in any language",
|
| 627 |
-
value="text, talk bubble, low quality, watermark, signature",
|
| 628 |
)
|
| 629 |
-
|
| 630 |
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
|
| 631 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 632 |
-
|
| 633 |
with gr.Row():
|
| 634 |
width = gr.Slider(label="Width", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024)
|
| 635 |
height = gr.Slider(label="Height", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024)
|
| 636 |
-
|
| 637 |
with gr.Row():
|
| 638 |
guidance_scale = gr.Slider(label="Guidance scale", minimum=0.0, maximum=20.0, step=0.1, value=7)
|
| 639 |
-
num_inference_steps = gr.Slider(label="
|
| 640 |
|
| 641 |
# Image-to-Image Tab
|
| 642 |
with gr.TabItem("Image to Image"):
|
| 643 |
-
with gr.Column(elem_id="col-container"
|
| 644 |
-
input_image = gr.Image(
|
| 645 |
-
label="Input Image",
|
| 646 |
-
type="pil",
|
| 647 |
-
elem_classes=["gr-image"]
|
| 648 |
-
)
|
| 649 |
|
| 650 |
with gr.Row():
|
| 651 |
img2img_prompt = gr.Text(
|
| 652 |
label="Prompt",
|
| 653 |
-
|
| 654 |
-
max_lines=3,
|
| 655 |
-
placeholder="Describe how you want to transform the image (any language)",
|
| 656 |
)
|
| 657 |
img2img_run_button = gr.Button("Transform", scale=0)
|
| 658 |
|
| 659 |
-
img2img_result = gr.Image(label="Transformed Image"
|
| 660 |
|
| 661 |
-
with gr.Accordion("Advanced Settings", open=False
|
| 662 |
img2img_negative_prompt = gr.Text(
|
| 663 |
label="Negative prompt",
|
| 664 |
-
|
| 665 |
-
placeholder="What to avoid in the transformation",
|
| 666 |
-
value="low quality, watermark, signature",
|
| 667 |
)
|
| 668 |
-
|
| 669 |
-
strength = gr.Slider(
|
| 670 |
-
label="Transformation Strength",
|
| 671 |
-
minimum=0.0,
|
| 672 |
-
maximum=1.0,
|
| 673 |
-
step=0.01,
|
| 674 |
-
value=0.75,
|
| 675 |
-
info="Lower values preserve more of the original image"
|
| 676 |
-
)
|
| 677 |
-
|
| 678 |
img2img_seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
|
| 679 |
img2img_randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 680 |
-
|
| 681 |
with gr.Row():
|
| 682 |
img2img_width = gr.Slider(label="Width", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024)
|
| 683 |
img2img_height = gr.Slider(label="Height", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024)
|
| 684 |
-
|
| 685 |
with gr.Row():
|
| 686 |
-
img2img_guidance_scale = gr.Slider(label="Guidance
|
| 687 |
-
img2img_num_inference_steps = gr.Slider(label="
|
| 688 |
-
|
| 689 |
-
# Helper function
|
| 690 |
-
def update_image_state(img, seed_val):
|
| 691 |
-
return img, seed_val
|
| 692 |
|
| 693 |
# Event handlers
|
| 694 |
-
boost_button.click(
|
| 695 |
-
|
| 696 |
-
inputs=[keyword_input],
|
| 697 |
-
outputs=[prompt]
|
| 698 |
-
)
|
| 699 |
|
| 700 |
-
random_button.click(
|
| 701 |
-
fn=get_random_prompt,
|
| 702 |
-
inputs=[],
|
| 703 |
-
outputs=[prompt]
|
| 704 |
-
)
|
| 705 |
-
|
| 706 |
run_button.click(
|
| 707 |
fn=infer,
|
| 708 |
-
inputs=[
|
| 709 |
-
|
| 710 |
-
negative_prompt,
|
| 711 |
-
seed,
|
| 712 |
-
randomize_seed,
|
| 713 |
-
width,
|
| 714 |
-
height,
|
| 715 |
-
guidance_scale,
|
| 716 |
-
num_inference_steps,
|
| 717 |
-
],
|
| 718 |
-
outputs=[result, current_seed]
|
| 719 |
-
).then(
|
| 720 |
-
fn=update_image_state,
|
| 721 |
-
inputs=[result, current_seed],
|
| 722 |
-
outputs=[current_image, current_seed]
|
| 723 |
)
|
| 724 |
-
|
| 725 |
img2img_run_button.click(
|
| 726 |
fn=img2img_infer,
|
| 727 |
-
inputs=[
|
| 728 |
-
|
| 729 |
-
img2img_prompt,
|
| 730 |
-
img2img_negative_prompt,
|
| 731 |
-
strength,
|
| 732 |
-
img2img_seed,
|
| 733 |
-
img2img_randomize_seed,
|
| 734 |
-
img2img_width,
|
| 735 |
-
img2img_height,
|
| 736 |
-
img2img_guidance_scale,
|
| 737 |
-
img2img_num_inference_steps
|
| 738 |
-
],
|
| 739 |
outputs=[img2img_result, img2img_seed]
|
| 740 |
)
|
| 741 |
|
|
|
|
| 11 |
from cohere import ClientV2
|
| 12 |
|
| 13 |
# ------------------------------------------------------------
|
| 14 |
+
# COHERE CLIENT
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
# ------------------------------------------------------------
|
| 16 |
coh_api_key = os.getenv("COH_API")
|
| 17 |
if not coh_api_key:
|
|
|
|
| 27 |
|
| 28 |
|
| 29 |
# ------------------------------------------------------------
|
| 30 |
+
# MODEL CONFIGURATION
|
| 31 |
# ------------------------------------------------------------
|
| 32 |
+
MODEL_ID = "Heartsync/NSFW-Uncensored"
|
| 33 |
+
MAX_SEED = np.iinfo(np.int32).max
|
| 34 |
+
MAX_IMAGE_SIZE = 1216
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
|
| 37 |
# ------------------------------------------------------------
|
| 38 |
+
# TRANSLATION FUNCTIONS (이전과 동일)
|
| 39 |
# ------------------------------------------------------------
|
| 40 |
non_english_regex = re.compile(r'[\uac00-\ud7a3\u3040-\u30ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff]+')
|
| 41 |
|
|
|
|
| 69 |
try:
|
| 70 |
print(f"[INFO] Translating text: '{text}'")
|
| 71 |
|
| 72 |
+
system_prompt = """You are a professional translator. Translate the input text to English accurately.
|
| 73 |
+
Provide ONLY the translated English text with no explanations."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
messages = [
|
| 76 |
{"role": "system", "content": [{"type": "text", "text": system_prompt}]},
|
|
|
|
| 89 |
print(f"[INFO] Original: '{text}'")
|
| 90 |
print(f"[INFO] Translated: '{translated_text}'")
|
| 91 |
|
| 92 |
+
return translated_text if len(translated_text) > 3 else text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
|
|
|
| 94 |
except Exception as e:
|
| 95 |
print(f"[ERROR] Translation failed: {str(e)}")
|
|
|
|
|
|
|
| 96 |
return text
|
| 97 |
|
| 98 |
|
| 99 |
def translate_prompt_if_needed(prompt):
|
| 100 |
+
"""Helper function to translate prompt"""
|
| 101 |
if not is_non_english(prompt):
|
| 102 |
return prompt
|
| 103 |
|
|
|
|
| 105 |
return prompt
|
| 106 |
|
| 107 |
try:
|
| 108 |
+
trans_system = "Translate to English accurately. Only provide the translation."
|
| 109 |
|
| 110 |
trans_response = coh_client.chat(
|
| 111 |
model="command-r-plus-08-2024",
|
|
|
|
| 116 |
temperature=0.1
|
| 117 |
)
|
| 118 |
|
|
|
|
|
|
|
| 119 |
if hasattr(trans_response, 'text'):
|
| 120 |
+
translated_prompt = trans_response.text.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
print(f"[SUCCESS] Translated: '{prompt}' -> '{translated_prompt}'")
|
| 122 |
return translated_prompt
|
| 123 |
|
|
|
|
| 132 |
# ------------------------------------------------------------
|
| 133 |
prompt_examples = [
|
| 134 |
"The shy college girl, with glasses and a tight plaid skirt, nervously approaches her professor",
|
| 135 |
+
"Her skirt rose a little higher with each gentle push, a soft blush of blush spreading across her cheeks",
|
| 136 |
+
"Moody mature anime scene of two lovers under neon rain, sensual atmosphere",
|
| 137 |
+
"The girl sits on the boy's lap by the window, his hands resting on her waist",
|
| 138 |
+
"A woman in a business suit, elegant and confident pose",
|
| 139 |
+
"Artistic portrait with dramatic lighting and soft shadows",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
]
|
| 141 |
|
| 142 |
|
|
|
|
| 144 |
# LLM PROMPT GENERATOR
|
| 145 |
# ------------------------------------------------------------
|
| 146 |
def generate_prompts(theme):
|
| 147 |
+
"""Generate optimal prompts using LLM"""
|
| 148 |
try:
|
| 149 |
if coh_client is None:
|
| 150 |
+
return "Cohere API token not set."
|
| 151 |
|
| 152 |
if non_english_regex.search(theme):
|
| 153 |
theme = translate_with_cohere(theme)
|
| 154 |
|
| 155 |
+
print(f"[INFO] Generating prompt for theme: {theme}")
|
| 156 |
|
| 157 |
+
system_prefix = """You are an expert at creating detailed image generation prompts. Create ONE optimal prompt based on the theme.
|
| 158 |
|
| 159 |
+
Guidelines:
|
| 160 |
+
1. Generate only ONE high-quality prompt
|
| 161 |
+
2. 1-3 sentences long
|
| 162 |
+
3. Detailed and descriptive
|
| 163 |
+
4. ONLY respond in ENGLISH
|
| 164 |
+
5. NO prefixes or headers - just the prompt text"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
|
| 166 |
messages = [
|
| 167 |
{"role": "system", "content": [{"type": "text", "text": system_prefix}]},
|
|
|
|
| 174 |
temperature=0.8
|
| 175 |
)
|
| 176 |
|
| 177 |
+
generated_prompt = response.text if hasattr(response, 'text') else str(response)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
|
| 179 |
if non_english_regex.search(generated_prompt):
|
|
|
|
| 180 |
generated_prompt = translate_with_cohere(generated_prompt)
|
| 181 |
|
| 182 |
+
generated_prompt = re.sub(r'^(AI🐼|Prompt|Response|Result|Output):\s*', '', generated_prompt)
|
|
|
|
|
|
|
| 183 |
generated_prompt = re.sub(r'^["\']+|["\']+$', '', generated_prompt)
|
| 184 |
generated_prompt = generated_prompt.strip()
|
|
|
|
| 185 |
|
| 186 |
+
print(f"[INFO] Generated: {generated_prompt}")
|
| 187 |
|
| 188 |
+
return generated_prompt if len(generated_prompt) > 10 else "Failed to generate prompt"
|
|
|
|
|
|
|
|
|
|
| 189 |
|
| 190 |
except Exception as e:
|
| 191 |
print(f"[ERROR] Prompt generation failed: {str(e)}")
|
| 192 |
+
return f"Error: {str(e)}"
|
|
|
|
|
|
|
| 193 |
|
| 194 |
|
| 195 |
# ------------------------------------------------------------
|
| 196 |
+
# SDXL TEXT-TO-IMAGE (ZeroGPU 최적화)
|
| 197 |
# ------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
@spaces.GPU(duration=120)
|
| 199 |
def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
|
| 200 |
+
"""Text-to-Image generation optimized for ZeroGPU"""
|
|
|
|
| 201 |
|
| 202 |
+
print(f"[DEBUG] Original prompt: '{prompt}'")
|
|
|
|
|
|
|
| 203 |
|
| 204 |
+
# Translate if needed
|
|
|
|
|
|
|
|
|
|
| 205 |
if is_non_english(prompt):
|
| 206 |
+
print(f"[ALERT] Non-English prompt detected")
|
| 207 |
prompt = translate_prompt_if_needed(prompt)
|
| 208 |
+
print(f"[INFO] Translated prompt: '{prompt}'")
|
| 209 |
|
| 210 |
if is_non_english(negative_prompt):
|
|
|
|
| 211 |
negative_prompt = translate_prompt_if_needed(negative_prompt)
|
|
|
|
| 212 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
if randomize_seed:
|
| 214 |
seed = random.randint(0, MAX_SEED)
|
| 215 |
+
|
|
|
|
|
|
|
| 216 |
try:
|
| 217 |
+
# GPU 컨텍스트 내에서 파이프라인 로드
|
| 218 |
+
device = torch.device("cuda")
|
| 219 |
+
print(f"[INFO] Loading pipeline on device: {device}")
|
| 220 |
+
|
| 221 |
+
# FP16 파이프라인 로드
|
| 222 |
+
pipe = StableDiffusionXLPipeline.from_pretrained(
|
| 223 |
+
MODEL_ID,
|
| 224 |
+
torch_dtype=torch.float16,
|
| 225 |
+
variant="fp16",
|
| 226 |
+
use_safetensors=True,
|
| 227 |
+
)
|
| 228 |
+
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
|
| 229 |
+
pipe = pipe.to(device)
|
| 230 |
+
|
| 231 |
+
# 메모리 효율성 개선
|
| 232 |
+
try:
|
| 233 |
+
pipe.enable_xformers_memory_efficient_attention()
|
| 234 |
+
print("[INFO] xformers memory efficient attention enabled")
|
| 235 |
+
except:
|
| 236 |
+
print("[WARN] xformers not available, using default attention")
|
| 237 |
+
|
| 238 |
+
# VAE 타일링 활성화 (메모리 절약)
|
| 239 |
+
pipe.enable_vae_tiling()
|
| 240 |
+
|
| 241 |
+
# 생성기 설정
|
| 242 |
+
generator = torch.Generator(device=device).manual_seed(seed)
|
| 243 |
+
|
| 244 |
+
# 이미지 생성
|
| 245 |
+
print(f"[INFO] Generating image...")
|
| 246 |
output_image = pipe(
|
| 247 |
prompt=prompt,
|
| 248 |
negative_prompt=negative_prompt,
|
|
|
|
| 252 |
height=height,
|
| 253 |
generator=generator,
|
| 254 |
).images[0]
|
| 255 |
+
|
| 256 |
+
# 메모리 정리
|
| 257 |
+
del pipe
|
| 258 |
+
torch.cuda.empty_cache()
|
| 259 |
+
|
| 260 |
+
print("[SUCCESS] Image generated successfully")
|
| 261 |
return output_image, seed
|
| 262 |
+
|
| 263 |
+
except Exception as e:
|
| 264 |
+
print(f"[ERROR] Generation failed: {str(e)}")
|
| 265 |
import traceback
|
| 266 |
traceback.print_exc()
|
| 267 |
+
|
| 268 |
+
# 에러 발생 시 메모리 정리
|
| 269 |
+
torch.cuda.empty_cache()
|
| 270 |
+
|
| 271 |
return Image.new("RGB", (width, height), color=(0, 0, 0)), seed
|
| 272 |
|
| 273 |
|
| 274 |
# ------------------------------------------------------------
|
| 275 |
+
# SDXL IMAGE-TO-IMAGE (ZeroGPU 최적화)
|
| 276 |
# ------------------------------------------------------------
|
| 277 |
@spaces.GPU(duration=120)
|
| 278 |
def img2img_infer(init_image, prompt, negative_prompt, strength, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
|
| 279 |
+
"""Image-to-Image generation optimized for ZeroGPU"""
|
|
|
|
| 280 |
|
| 281 |
if init_image is None:
|
| 282 |
return None, seed
|
| 283 |
|
| 284 |
+
print(f"[DEBUG] Image-to-Image prompt: '{prompt}'")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 285 |
|
| 286 |
+
# Translate if needed
|
| 287 |
if is_non_english(prompt):
|
|
|
|
| 288 |
prompt = translate_prompt_if_needed(prompt)
|
|
|
|
| 289 |
|
| 290 |
if is_non_english(negative_prompt):
|
|
|
|
| 291 |
negative_prompt = translate_prompt_if_needed(negative_prompt)
|
|
|
|
| 292 |
|
| 293 |
if randomize_seed:
|
| 294 |
seed = random.randint(0, MAX_SEED)
|
| 295 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 296 |
try:
|
| 297 |
+
# GPU 컨텍스트 내에서 파이프라인 로드
|
| 298 |
+
device = torch.device("cuda")
|
| 299 |
+
print(f"[INFO] Loading img2img pipeline on device: {device}")
|
| 300 |
+
|
| 301 |
+
img2img_pipe = StableDiffusionXLImg2ImgPipeline.from_pretrained(
|
| 302 |
+
MODEL_ID,
|
| 303 |
+
torch_dtype=torch.float16,
|
| 304 |
+
variant="fp16",
|
| 305 |
+
use_safetensors=True,
|
| 306 |
+
)
|
| 307 |
+
img2img_pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(img2img_pipe.scheduler.config)
|
| 308 |
+
img2img_pipe = img2img_pipe.to(device)
|
| 309 |
+
|
| 310 |
+
# 메모리 효율성 개선
|
| 311 |
+
try:
|
| 312 |
+
img2img_pipe.enable_xformers_memory_efficient_attention()
|
| 313 |
+
except:
|
| 314 |
+
pass
|
| 315 |
+
|
| 316 |
+
img2img_pipe.enable_vae_tiling()
|
| 317 |
+
|
| 318 |
+
# 이미지 전처리
|
| 319 |
+
init_image = init_image.convert("RGB")
|
| 320 |
+
init_image = init_image.resize((width, height), Image.Resampling.LANCZOS)
|
| 321 |
+
|
| 322 |
+
generator = torch.Generator(device=device).manual_seed(seed)
|
| 323 |
+
|
| 324 |
+
# 이미지 생성
|
| 325 |
+
print(f"[INFO] Transforming image...")
|
| 326 |
output_image = img2img_pipe(
|
| 327 |
prompt=prompt,
|
| 328 |
negative_prompt=negative_prompt,
|
|
|
|
| 332 |
num_inference_steps=num_inference_steps,
|
| 333 |
generator=generator,
|
| 334 |
).images[0]
|
| 335 |
+
|
| 336 |
+
# 메모리 정리
|
| 337 |
+
del img2img_pipe
|
| 338 |
+
torch.cuda.empty_cache()
|
| 339 |
+
|
| 340 |
+
print("[SUCCESS] Image transformed successfully")
|
| 341 |
return output_image, seed
|
| 342 |
+
|
| 343 |
+
except Exception as e:
|
| 344 |
+
print(f"[ERROR] Transformation failed: {str(e)}")
|
| 345 |
import traceback
|
| 346 |
traceback.print_exc()
|
| 347 |
+
|
| 348 |
+
torch.cuda.empty_cache()
|
| 349 |
+
|
| 350 |
return None, seed
|
| 351 |
|
| 352 |
|
|
|
|
| 362 |
return "Please enter a keyword or theme first"
|
| 363 |
|
| 364 |
if coh_client is None:
|
| 365 |
+
return "Cohere API token not set"
|
| 366 |
|
|
|
|
| 367 |
prompt = generate_prompts(keyword)
|
| 368 |
+
return prompt.strip() if len(prompt) > 10 else "Failed to generate prompt"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 369 |
|
| 370 |
|
| 371 |
# ------------------------------------------------------------
|
| 372 |
+
# UI LAYOUT
|
| 373 |
# ------------------------------------------------------------
|
| 374 |
css = """
|
| 375 |
body {background: linear-gradient(135deg, #f2e6ff 0%, #e6f0ff 100%); color: #222; font-family: 'Noto Sans', sans-serif;}
|
| 376 |
#col-container {margin: 0 auto; max-width: 768px; padding: 15px; background: rgba(255, 255, 255, 0.8); border-radius: 15px; box-shadow: 0 8px 32px rgba(31, 38, 135, 0.2);}
|
| 377 |
.gr-button {background: #7fbdf6; color: #fff; border-radius: 8px; transition: all 0.3s ease; font-weight: bold;}
|
| 378 |
.gr-button:hover {background: #5a9ae6; transform: translateY(-2px); box-shadow: 0 5px 15px rgba(0,0,0,0.1);}
|
| 379 |
+
#prompt-box textarea {font-size: 1.1rem; height: 9rem !important; background: #fff; color: #222; border-radius: 10px;}
|
| 380 |
.boost-btn {background: #ff7eb6; margin-top: 5px;}
|
| 381 |
.boost-btn:hover {background: #ff5aa5;}
|
| 382 |
.random-btn {background: #9966ff; margin-top: 5px;}
|
| 383 |
.random-btn:hover {background: #8040ff;}
|
|
|
|
| 384 |
.title {color: #6600cc; text-shadow: 1px 1px 2px rgba(0,0,0,0.1);}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 385 |
"""
|
| 386 |
|
| 387 |
+
with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 388 |
gr.Markdown(
|
| 389 |
+
"""
|
| 390 |
## 🖌️ NSFW Uncensored Text & Imagery: AI Limits Explorer
|
| 391 |
|
| 392 |
+
**ZeroGPU Optimized | Multi-language Support**
|
|
|
|
|
|
|
| 393 |
""", elem_classes=["title"]
|
| 394 |
)
|
| 395 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 396 |
with gr.Tabs():
|
| 397 |
# Text-to-Image Tab
|
| 398 |
with gr.TabItem("Text to Image"):
|
| 399 |
+
with gr.Column(elem_id="col-container"):
|
| 400 |
with gr.Row():
|
| 401 |
keyword_input = gr.Text(
|
| 402 |
label="Keyword Input",
|
| 403 |
+
placeholder="Enter keyword in any language",
|
|
|
|
|
|
|
| 404 |
value="random",
|
| 405 |
)
|
| 406 |
boost_button = gr.Button("BOOST", elem_classes=["boost-btn"])
|
|
|
|
| 410 |
prompt = gr.Text(
|
| 411 |
label="Prompt",
|
| 412 |
elem_id="prompt-box",
|
|
|
|
| 413 |
max_lines=3,
|
| 414 |
+
placeholder="Enter prompt in any language",
|
| 415 |
)
|
| 416 |
run_button = gr.Button("Generate", scale=0)
|
| 417 |
|
| 418 |
+
result = gr.Image(label="Generated Image")
|
| 419 |
|
| 420 |
+
with gr.Accordion("Advanced Settings", open=False):
|
| 421 |
negative_prompt = gr.Text(
|
| 422 |
label="Negative prompt",
|
| 423 |
+
value="low quality, watermark, signature",
|
|
|
|
|
|
|
| 424 |
)
|
|
|
|
| 425 |
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
|
| 426 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
|
|
|
| 427 |
with gr.Row():
|
| 428 |
width = gr.Slider(label="Width", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024)
|
| 429 |
height = gr.Slider(label="Height", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024)
|
|
|
|
| 430 |
with gr.Row():
|
| 431 |
guidance_scale = gr.Slider(label="Guidance scale", minimum=0.0, maximum=20.0, step=0.1, value=7)
|
| 432 |
+
num_inference_steps = gr.Slider(label="Steps", minimum=1, maximum=50, step=1, value=28)
|
| 433 |
|
| 434 |
# Image-to-Image Tab
|
| 435 |
with gr.TabItem("Image to Image"):
|
| 436 |
+
with gr.Column(elem_id="col-container"):
|
| 437 |
+
input_image = gr.Image(label="Input Image", type="pil")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 438 |
|
| 439 |
with gr.Row():
|
| 440 |
img2img_prompt = gr.Text(
|
| 441 |
label="Prompt",
|
| 442 |
+
placeholder="Describe transformation (any language)",
|
|
|
|
|
|
|
| 443 |
)
|
| 444 |
img2img_run_button = gr.Button("Transform", scale=0)
|
| 445 |
|
| 446 |
+
img2img_result = gr.Image(label="Transformed Image")
|
| 447 |
|
| 448 |
+
with gr.Accordion("Advanced Settings", open=False):
|
| 449 |
img2img_negative_prompt = gr.Text(
|
| 450 |
label="Negative prompt",
|
| 451 |
+
value="low quality, watermark",
|
|
|
|
|
|
|
| 452 |
)
|
| 453 |
+
strength = gr.Slider(label="Strength", minimum=0.0, maximum=1.0, step=0.01, value=0.75)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 454 |
img2img_seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
|
| 455 |
img2img_randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
|
|
|
| 456 |
with gr.Row():
|
| 457 |
img2img_width = gr.Slider(label="Width", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024)
|
| 458 |
img2img_height = gr.Slider(label="Height", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024)
|
|
|
|
| 459 |
with gr.Row():
|
| 460 |
+
img2img_guidance_scale = gr.Slider(label="Guidance", minimum=0.0, maximum=20.0, step=0.1, value=7.5)
|
| 461 |
+
img2img_num_inference_steps = gr.Slider(label="Steps", minimum=1, maximum=50, step=1, value=30)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 462 |
|
| 463 |
# Event handlers
|
| 464 |
+
boost_button.click(fn=boost_prompt, inputs=[keyword_input], outputs=[prompt])
|
| 465 |
+
random_button.click(fn=get_random_prompt, outputs=[prompt])
|
|
|
|
|
|
|
|
|
|
| 466 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 467 |
run_button.click(
|
| 468 |
fn=infer,
|
| 469 |
+
inputs=[prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
| 470 |
+
outputs=[result, seed]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 471 |
)
|
| 472 |
+
|
| 473 |
img2img_run_button.click(
|
| 474 |
fn=img2img_infer,
|
| 475 |
+
inputs=[input_image, img2img_prompt, img2img_negative_prompt, strength, img2img_seed,
|
| 476 |
+
img2img_randomize_seed, img2img_width, img2img_height, img2img_guidance_scale, img2img_num_inference_steps],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 477 |
outputs=[img2img_result, img2img_seed]
|
| 478 |
)
|
| 479 |
|