Text-to-Image
Diffusers
English
stable-diffusion-xl
huggingface-inference-endpoints
custom-inference
Instructions to use msgxai/msgxai-hg-api with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Diffusers
How to use msgxai/msgxai-hg-api with Diffusers:
pip install -U diffusers transformers accelerate
import torch from diffusers import DiffusionPipeline # switch to "mps" for apple devices pipe = DiffusionPipeline.from_pretrained("msgxai/msgxai-hg-api", dtype=torch.bfloat16, device_map="cuda") prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k" image = pipe(prompt).images[0] - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- Draw Things
- DiffusionBee
chore: init commit
Browse files- .python-version +1 -0
- src/Procfile +1 -0
- src/app.conf +13 -0
- src/app.py +145 -0
- src/requirements.txt +7 -0
.python-version
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
msgxai-hg-api
|
src/Procfile
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
web: uvicorn app:app --host 0.0.0.0 --port $PORT
|
src/app.conf
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cover_path_": ["hentai-waianiv6-card0.webp", "hentai-waianiv6-card1.webp", "hentai-waianiv6-card2.webp", "hentai-waianiv6-card3.webp", "hentai-waianiv6-card4.webp", "hentai-waianiv6-card5.webp", "hentai-waianiv6-card6.webp", "hentai-waianiv6-card7.webp"],
|
| 3 |
+
"cover_path": ["hentai-waianiv6-card5.webp"],
|
| 4 |
+
"model_id": "John6666/wai-ani-hentai-pony-v3-sdxl",
|
| 5 |
+
"name": "hentai-waianiv6-card",
|
| 6 |
+
"prompt": "score_9, score_8_up, score_7_up,rating_explicit,BREAK, {prompt}",
|
| 7 |
+
"negative_prompt": "source_furry, source_pony, source_cartoon,3d, blurry, incest, beastiality, children, loli, child, kids, teens, text, logo, timestamp, artist name, artist logo, watermark, web address, copyright name, copyright notice, emblem, comic, title, logo, character name, border, patreon username, signature, webpage, company name, caption, labels, comments",
|
| 8 |
+
"width": 1024,
|
| 9 |
+
"height": 768,
|
| 10 |
+
"inference_steps": 30,
|
| 11 |
+
"guidance_scale": 7,
|
| 12 |
+
"use_safetensors": true
|
| 13 |
+
}
|
src/app.py
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import random
|
| 4 |
+
import re
|
| 5 |
+
import base64
|
| 6 |
+
from io import BytesIO
|
| 7 |
+
|
| 8 |
+
from fastapi import FastAPI, HTTPException
|
| 9 |
+
from pydantic import BaseModel
|
| 10 |
+
from PIL import Image
|
| 11 |
+
|
| 12 |
+
import torch
|
| 13 |
+
from huggingface_hub import snapshot_download
|
| 14 |
+
from diffusers import (
|
| 15 |
+
AutoencoderKL,
|
| 16 |
+
StableDiffusionXLPipeline,
|
| 17 |
+
EulerAncestralDiscreteScheduler,
|
| 18 |
+
DPMSolverSDEScheduler
|
| 19 |
+
)
|
| 20 |
+
from diffusers.models.attention_processor import AttnProcessor2_0
|
| 21 |
+
|
| 22 |
+
# Global constants
|
| 23 |
+
MAX_SEED = 12211231
|
| 24 |
+
NUM_IMAGES_PER_PROMPT = 1
|
| 25 |
+
USE_TORCH_COMPILE = os.getenv("USE_TORCH_COMPILE", "0") == "1"
|
| 26 |
+
|
| 27 |
+
# Load configuration from app.conf
|
| 28 |
+
with open("app.conf", "r") as f:
|
| 29 |
+
cfg = json.load(f)
|
| 30 |
+
|
| 31 |
+
# --- Child-Content Filtering Functions ---
|
| 32 |
+
child_related_regex = re.compile(
|
| 33 |
+
r'(child|children|kid|kids|baby|babies|toddler|infant|juvenile|minor|underage|preteen|adolescent|youngster|youth|son|daughter|young|kindergarten|preschool|'
|
| 34 |
+
r'([1-9]|1[0-7])[\s_\-|\.\,]*year(s)?[\s_\-|\.\,]*old|'
|
| 35 |
+
r'little|small|tiny|short|young|new[\s_\-|\.\,]*born[\s_\-|\.\,]*(boy|girl|male|man|bro|brother|sis|sister))',
|
| 36 |
+
re.IGNORECASE
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
def remove_child_related_content(prompt: str) -> str:
|
| 40 |
+
"""Remove any child-related references from the prompt."""
|
| 41 |
+
cleaned_prompt = re.sub(child_related_regex, '', prompt)
|
| 42 |
+
return cleaned_prompt.strip()
|
| 43 |
+
|
| 44 |
+
def contains_child_related_content(prompt: str) -> bool:
|
| 45 |
+
"""Check if the prompt contains child-related content."""
|
| 46 |
+
return bool(child_related_regex.search(prompt))
|
| 47 |
+
|
| 48 |
+
# --- Model Pipeline Loading ---
|
| 49 |
+
def load_pipeline_and_scheduler():
|
| 50 |
+
clip_skip = cfg.get("clip_skip", 0)
|
| 51 |
+
|
| 52 |
+
# Download model files from Hugging Face Hub
|
| 53 |
+
ckpt_dir = snapshot_download(repo_id=cfg["model_id"])
|
| 54 |
+
|
| 55 |
+
# Load the VAE model (for decoding latents)
|
| 56 |
+
vae = AutoencoderKL.from_pretrained(os.path.join(ckpt_dir, "vae"), torch_dtype=torch.float16)
|
| 57 |
+
|
| 58 |
+
# Load the Stable Diffusion XL pipeline
|
| 59 |
+
pipe = StableDiffusionXLPipeline.from_pretrained(
|
| 60 |
+
ckpt_dir,
|
| 61 |
+
vae=vae,
|
| 62 |
+
torch_dtype=torch.float16,
|
| 63 |
+
use_safetensors=cfg.get("use_safetensors", True),
|
| 64 |
+
variant="fp16"
|
| 65 |
+
)
|
| 66 |
+
pipe = pipe.to("cuda")
|
| 67 |
+
pipe.unet.set_attn_processor(AttnProcessor2_0())
|
| 68 |
+
|
| 69 |
+
# Set up samplers/schedulers based on configuration
|
| 70 |
+
samplers = {
|
| 71 |
+
"Euler a": EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config),
|
| 72 |
+
"DPM++ SDE Karras": DPMSolverSDEScheduler.from_config(pipe.scheduler.config, use_karras_sigmas=True)
|
| 73 |
+
}
|
| 74 |
+
# Default to "DPM++ SDE Karras" if not specified
|
| 75 |
+
pipe.scheduler = samplers.get(cfg.get("sampler", "DPM++ SDE Karras"))
|
| 76 |
+
|
| 77 |
+
# Adjust the text encoder layers if needed using clip_skip
|
| 78 |
+
pipe.text_encoder.config.num_hidden_layers -= (clip_skip - 1)
|
| 79 |
+
|
| 80 |
+
if USE_TORCH_COMPILE:
|
| 81 |
+
pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)
|
| 82 |
+
print("Model Compiled!")
|
| 83 |
+
return pipe
|
| 84 |
+
|
| 85 |
+
# Load the model pipeline once at startup
|
| 86 |
+
pipe = load_pipeline_and_scheduler()
|
| 87 |
+
|
| 88 |
+
# --- Utility Function: Convert PIL Image to Base64 ---
|
| 89 |
+
def pil_image_to_base64(img: Image.Image) -> str:
|
| 90 |
+
buffered = BytesIO()
|
| 91 |
+
img.convert("RGB").save(buffered, format="WEBP", quality=90)
|
| 92 |
+
return base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 93 |
+
|
| 94 |
+
# --- FastAPI Application Setup ---
|
| 95 |
+
app = FastAPI(title="Text-to-Image API with Content Filtering")
|
| 96 |
+
|
| 97 |
+
class GenerateRequest(BaseModel):
|
| 98 |
+
prompt: str
|
| 99 |
+
|
| 100 |
+
@app.get("/")
|
| 101 |
+
async def read_root():
|
| 102 |
+
return {"message": "Text-to-Image API with content filtering is running."}
|
| 103 |
+
|
| 104 |
+
@app.post("/generate")
|
| 105 |
+
async def generate(req: GenerateRequest):
|
| 106 |
+
# Apply child-content filtering to the prompt
|
| 107 |
+
prompt_text = req.prompt
|
| 108 |
+
if contains_child_related_content(prompt_text):
|
| 109 |
+
prompt_text = remove_child_related_content(prompt_text)
|
| 110 |
+
|
| 111 |
+
# Replace placeholder in the prompt template from config
|
| 112 |
+
combined_prompt = cfg.get("prompt", "{prompt}").replace("{prompt}", prompt_text)
|
| 113 |
+
# Use negative_prompt if provided; otherwise, default to empty string
|
| 114 |
+
negative_prompt = cfg.get("negative_prompt", "")
|
| 115 |
+
width = cfg.get("width", 1024)
|
| 116 |
+
height = cfg.get("height", 768)
|
| 117 |
+
inference_steps = cfg.get("inference_steps", 30)
|
| 118 |
+
guidance_scale = cfg.get("guidance_scale", 7)
|
| 119 |
+
|
| 120 |
+
# Randomize the seed for generation
|
| 121 |
+
seed = random.randint(0, MAX_SEED)
|
| 122 |
+
generator = torch.Generator(pipe.device).manual_seed(seed)
|
| 123 |
+
|
| 124 |
+
try:
|
| 125 |
+
outputs = pipe(
|
| 126 |
+
prompt=combined_prompt,
|
| 127 |
+
negative_prompt=negative_prompt,
|
| 128 |
+
width=width,
|
| 129 |
+
height=height,
|
| 130 |
+
guidance_scale=guidance_scale,
|
| 131 |
+
num_inference_steps=inference_steps,
|
| 132 |
+
generator=generator,
|
| 133 |
+
num_images_per_prompt=NUM_IMAGES_PER_PROMPT,
|
| 134 |
+
output_type="pil"
|
| 135 |
+
)
|
| 136 |
+
# Convert the first generated image to base64
|
| 137 |
+
img_base64 = pil_image_to_base64(outputs.images[0])
|
| 138 |
+
except Exception as e:
|
| 139 |
+
raise HTTPException(status_code=500, detail=f"Image generation failed: {e}")
|
| 140 |
+
|
| 141 |
+
return {"image_base64": img_base64, "seed": seed}
|
| 142 |
+
|
| 143 |
+
if __name__ == "__main__":
|
| 144 |
+
import uvicorn
|
| 145 |
+
uvicorn.run("app:app", host="0.0.0.0", port=8000)
|
src/requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
torch
|
| 4 |
+
diffusers
|
| 5 |
+
huggingface_hub
|
| 6 |
+
pydantic
|
| 7 |
+
Pillow
|