File size: 10,003 Bytes
08e06c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cd2515e
 
 
08e06c9
 
 
cd2515e
 
08e06c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
"""AI KINGยฉ๏ธ generators on ZeroGPU (open models only, no paid inference).

- Text โ†’ Image and Image โ†’ Image: FLUX.1 [schnell] (Apache-2.0), 4-step distilled.
- Text/Image โ†’ Video + seamless loops: LTX-Video 0.9.5 (2B, LTXV Open Weights License,
  free for companies under $10M annual revenue). Shares FLUX's T5-XXL text encoder
  (byte-identical weights) so both fit in one ZeroGPU slot.
Set AIKING_NO_MODELS=1 to run the UI without loading models (local design work).
"""
from __future__ import annotations

import math, os, random, tempfile, time, traceback

import numpy as np
from PIL import Image, ImageOps

try:
    import spaces  # ZeroGPU
except Exception:  # local / non-Spaces
    class _S:
        @staticmethod
        def GPU(*a, **k):
            if a and callable(a[0]):
                return a[0]
            return lambda f: f
    spaces = _S()

NO_MODELS = os.environ.get("AIKING_NO_MODELS") == "1"
HUB_TOKEN = os.environ.get("AIKING_HUB_TOKEN") or None
FLUX_ID = "black-forest-labs/FLUX.1-schnell"
LTX_ID = "Lightricks/LTX-Video-0.9.5"
OUT_DIR = os.path.join(tempfile.gettempdir(), "aiking_out")
os.makedirs(OUT_DIR, exist_ok=True)

STATUS = {"image": "off", "video": "off"}
ERRORS: dict[str, str] = {}
t2i = i2i = ltx = None
LTXVideoCondition = None

STYLES = {
    "None": "",
    "๐ŸŽฌ ARRI ALEXA 35": "cinematic film still shot on ARRI ALEXA 35, anamorphic lens, shallow depth of field, natural film grain, hyperrealistic skin texture, dramatic motivated lighting",
    "๐Ÿ’ฟ Album Cover": "premium album cover art, bold centered composition, iconic silhouette, luxury finish, high contrast, crisp detail",
    "๐Ÿ‘‘ Royal Gold": "black and gold luxury aesthetic, regal, glossy reflections, crown motifs, studio rim light, hyperdetailed",
    "๐ŸŒƒ Neon Lofi": "cozy lofi aesthetic, neon city at night, rain on glass, soft bloom, warm desk lamp, cinematic anime-inspired color",
    "๐Ÿš€ Intergalactic": "cosmic nebula backdrop, starfield, iridescent rim light, epic sci-fi scale, glossy chrome accents",
    "๐Ÿ“ธ Editorial": "luxury fashion editorial photograph, softbox key light, magazine cover quality, clean background",
    "๐Ÿงธ 3D Toy": "glossy 3D render, collectible vinyl toy style, soft global illumination, studio backdrop",
    "๐ŸŽฎ Game Cinematic": "AAA video game cinematic key art, unreal engine 5 render, volumetric light, epic composition",
}
ASPECTS = {"9:16 Story": (768, 1344), "1:1 Square": (1024, 1024), "4:5 Portrait": (896, 1120), "16:9 Cinema": (1344, 768)}
VIDEO_ASPECTS = {"9:16 Story": (480, 832), "1:1 Square": (640, 640), "16:9 Cinema": (832, 480)}
VIDEO_NEG = "worst quality, inconsistent motion, blurry, jittery, distorted face, morphing, warped hands, text, watermark, logo"


def _load():
    global t2i, i2i, ltx, LTXVideoCondition
    import torch
    from huggingface_hub import snapshot_download
    from diffusers import FluxImg2ImgPipeline, FluxPipeline
    flux_dir = snapshot_download(FLUX_ID, token=HUB_TOKEN, allow_patterns=[
        "model_index.json", "scheduler/*", "text_encoder/*", "text_encoder_2/*",
        "tokenizer/*", "tokenizer_2/*", "transformer/*", "vae/*"])
    t2i = FluxPipeline.from_pretrained(flux_dir, dtype=torch.bfloat16).to("cuda")
    i2i = FluxImg2ImgPipeline.from_pipe(t2i, dtype=torch.bfloat16)  # from_pipe defaults to fp32!
    STATUS["image"] = "ready"
    try:
        from diffusers import (AutoencoderKLLTXVideo, FlowMatchEulerDiscreteScheduler,
                               LTXConditionPipeline, LTXVideoTransformer3DModel)
        from diffusers.pipelines.ltx.pipeline_ltx_condition import LTXVideoCondition as _C
        LTXVideoCondition = _C
        ltx_dir = snapshot_download(LTX_ID, token=HUB_TOKEN, allow_patterns=[
            "model_index.json", "scheduler/*", "transformer/*", "vae/*", "tokenizer/*"])
        ltx = LTXConditionPipeline(
            scheduler=FlowMatchEulerDiscreteScheduler.from_pretrained(ltx_dir, subfolder="scheduler"),
            vae=AutoencoderKLLTXVideo.from_pretrained(ltx_dir, subfolder="vae", dtype=torch.bfloat16),
            text_encoder=t2i.text_encoder_2,  # identical T5-XXL v1.1 weights
            tokenizer=t2i.tokenizer_2,
            transformer=LTXVideoTransformer3DModel.from_pretrained(ltx_dir, subfolder="transformer", dtype=torch.bfloat16),
        ).to("cuda")
        STATUS["video"] = "ready"
    except Exception as e:  # keep images working even if video can't load
        ERRORS["video"] = f"{type(e).__name__}: {e}"[:500]
        STATUS["video"] = "error"
        traceback.print_exc()


if not NO_MODELS:
    try:
        _load()
    except Exception as e:
        ERRORS["image"] = f"{type(e).__name__}: {e}"[:500]
        STATUS["image"] = STATUS["image"] if STATUS["image"] == "ready" else "error"
        traceback.print_exc()


class GenError(RuntimeError):
    pass


def _seed(seed):
    try:
        s = int(seed)
    except Exception:
        s = -1
    return random.randint(0, 2**31 - 1) if s < 0 else s


CLEAN = "original unbranded design, plain clothing and products without any brand logos or third-party trademarks, no watermark"


def style_prompt(prompt: str, style: str) -> str:
    extra = STYLES.get(style, "")
    p = (prompt or "").strip().rstrip(".")
    p = f"{p}, {extra}" if extra else p
    return f"{p}, {CLEAN}"


def _save_png(img: Image.Image, tag: str) -> str:
    path = os.path.join(OUT_DIR, f"aiking-{tag}-{int(time.time()*1000)}-{random.randint(100,999)}.png")
    img.save(path)
    return path


def _fit16(img: Image.Image, max_side=1024) -> Image.Image:
    img = ImageOps.exif_transpose(img).convert("RGB")
    w, h = img.size
    s = min(1.0, max_side / max(w, h))
    w, h = max(256, int(w * s) // 16 * 16), max(256, int(h * s) // 16 * 16)
    return img.resize((w, h), Image.LANCZOS)


def _cover(img: Image.Image, w: int, h: int) -> Image.Image:
    return ImageOps.fit(ImageOps.exif_transpose(img).convert("RGB"), (w, h), Image.LANCZOS)


def _need(kind):
    if STATUS.get(kind) != "ready":
        msg = ERRORS.get(kind) or ("models are disabled in this preview" if NO_MODELS else "model is still loading, try again in a minute")
        raise GenError(f"{'Image' if kind == 'image' else 'Video'} engine not ready ({msg}).")


# ---------------- GPU functions ----------------

def _t2i_dur(prompt, style, aspect, n, seed):
    return min(90, 20 + 8 * int(n or 1))


@spaces.GPU(duration=_t2i_dur)
def text_to_image(prompt, style, aspect, n, seed):
    import torch
    _need("image")
    w, h = ASPECTS.get(aspect, (1024, 1024))
    s = _seed(seed)
    imgs = t2i(prompt=style_prompt(prompt, style), width=w, height=h, num_inference_steps=4, guidance_scale=0.0,
               num_images_per_prompt=int(n or 1), max_sequence_length=256,
               generator=torch.Generator("cpu").manual_seed(s)).images
    return [_save_png(im, "img") for im in imgs], s


def _i2i_dur(image_path, prompt, style, strength, seed):
    return 40


@spaces.GPU(duration=_i2i_dur)
def image_to_image(image_path, prompt, style, strength, seed):
    import torch
    _need("image")
    src = _fit16(Image.open(image_path))
    st = float(strength)
    steps = max(4, math.ceil(4 / max(st, 0.05)))
    s = _seed(seed)
    img = i2i(prompt=style_prompt(prompt, style), image=src, strength=st, width=src.width, height=src.height,
              num_inference_steps=min(steps, 16), guidance_scale=0.0, max_sequence_length=256,
              generator=torch.Generator("cpu").manual_seed(s)).images[0]
    return _save_png(img, "edit"), s


def _frames(seconds: float) -> int:
    n = int(round(float(seconds) * 24 / 8)) * 8 + 1
    return max(25, min(n, 129))


def _vid_dur(prompt, image_path, mode, seconds, aspect, steps, seed):
    return int(min(170, 35 + int(steps) * (_frames(seconds) / 97) * 2.2))


def _write_mp4(frames, fps=24) -> str:
    import imageio.v2 as imageio
    path = os.path.join(OUT_DIR, f"aiking-video-{int(time.time()*1000)}.mp4")
    arr = (np.clip(np.asarray(frames), 0, 1) * 255).astype(np.uint8) if np.asarray(frames).dtype != np.uint8 else np.asarray(frames)
    with imageio.get_writer(path, fps=fps, codec="libx264", quality=8, pixelformat="yuv420p", macro_block_size=16,
                            ffmpeg_params=["-movflags", "+faststart"]) as wr:
        for f in arr:
            wr.append_data(f)
    return path


@spaces.GPU(duration=_vid_dur)
def make_video(prompt, image_path, mode, seconds, aspect, steps, seed):
    """mode: 'Text โ†’ Video', 'Image โ†’ Video', 'Seamless Loop'."""
    import torch
    _need("video")
    w, h = VIDEO_ASPECTS.get(aspect, (832, 480))
    n = _frames(seconds)
    s = _seed(seed)
    gen = torch.Generator("cpu").manual_seed(s)
    keyframe = None
    if image_path:
        keyframe = _cover(Image.open(image_path), w, h)
    elif mode != "Text โ†’ Video" or STATUS["image"] == "ready":
        # cinematic keyframe from FLUX first: sharper, more on-brand than raw text-to-video
        _need("image")
        kw, kh = (w * 2 // 16 * 16, h * 2 // 16 * 16)
        k = t2i(prompt=style_prompt(prompt, "๐ŸŽฌ ARRI ALEXA 35"), width=kw, height=kh, num_inference_steps=4,
                guidance_scale=0.0, max_sequence_length=256, generator=torch.Generator("cpu").manual_seed(s)).images[0]
        keyframe = k.resize((w, h), Image.LANCZOS)
    conds = None
    if keyframe is not None:
        conds = [LTXVideoCondition(image=keyframe, frame_index=0)]
        if mode == "Seamless Loop":
            conds.append(LTXVideoCondition(image=keyframe, frame_index=n - 1))
    motion = (prompt or "").strip().rstrip(".") + ". Smooth natural motion, cinematic camera, consistent identity and lighting, high detail."
    out = ltx(conditions=conds, prompt=motion, negative_prompt=VIDEO_NEG, width=w, height=h, num_frames=n,
              num_inference_steps=int(steps), guidance_scale=3.0, decode_timestep=0.05, decode_noise_scale=0.025,
              generator=gen, output_type="np").frames[0]
    return _write_mp4(out), s