linoyts HF Staff commited on
Commit
e487c51
·
verified ·
1 Parent(s): 1434615

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +88 -0
app.py ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import subprocess
3
+ import sys
4
+
5
+ # ---------------------------------------------------------------------------
6
+ # LTX-2.4 prompt enhancer as a standalone ZeroGPU Space (Gemma-4 E2B). Kept
7
+ # separate so the 5GB enhancer + torchvision live off the main video Space's
8
+ # budget; the video Spaces call this over gradio_client. Replicates diffusers'
9
+ # LTX2Pipeline.enhance_prompt exactly (system prompt by mode, greedy decoding).
10
+ # The diffusers wheel is installed only for the LTX-2.4 system-prompt constants.
11
+ # ---------------------------------------------------------------------------
12
+ from huggingface_hub import hf_hub_download
13
+
14
+ HF_TOKEN = os.environ.get("HF_TOKEN")
15
+ _whl = hf_hub_download(
16
+ "diffusers-internal-dev/ltx24-wheels",
17
+ "canon/diffusers-0.40.0.dev0-py3-none-any.whl",
18
+ repo_type="dataset",
19
+ token=HF_TOKEN,
20
+ )
21
+ _TARGET = "/tmp/ltx24_diffusers"
22
+ subprocess.run(
23
+ [sys.executable, "-m", "pip", "install", "--no-deps", "--target", _TARGET, _whl],
24
+ check=True,
25
+ )
26
+ sys.path.insert(0, _TARGET)
27
+
28
+ import gradio as gr
29
+ import spaces
30
+ import torch
31
+ from transformers import AutoModelForImageTextToText, AutoProcessor
32
+
33
+ from diffusers.pipelines.ltx2.utils import (
34
+ GEMMA4_PROMPT_ENHANCEMENT_CONFIG,
35
+ LTX2_4_I2V_DEFAULT_SYSTEM_PROMPT,
36
+ LTX2_4_T2V_DEFAULT_SYSTEM_PROMPT,
37
+ )
38
+
39
+ ENHANCER_ID = "google/gemma-4-E2B-it"
40
+ CFG = GEMMA4_PROMPT_ENHANCEMENT_CONFIG
41
+
42
+ print("[VERSION] ltx-2.4-enhancer v1 — loading", flush=True)
43
+ processor = AutoProcessor.from_pretrained(ENHANCER_ID, token=HF_TOKEN)
44
+ model = AutoModelForImageTextToText.from_pretrained(
45
+ ENHANCER_ID, torch_dtype=torch.bfloat16, token=HF_TOKEN
46
+ ).to("cuda")
47
+ model.eval()
48
+ print("[VERSION] enhancer ready", flush=True)
49
+
50
+
51
+ @spaces.GPU(duration=120)
52
+ def enhance(prompt, image=None, progress=gr.Progress()):
53
+ if not prompt or not prompt.strip():
54
+ raise gr.Error("Please enter a prompt to enhance.")
55
+ # I2V (reference-image) system prompt when an image is supplied, else T2V.
56
+ system_prompt = LTX2_4_I2V_DEFAULT_SYSTEM_PROMPT if image is not None else LTX2_4_T2V_DEFAULT_SYSTEM_PROMPT
57
+ messages = [
58
+ {"role": "system", "content": system_prompt},
59
+ {"role": "user", "content": f"{CFG.user_prompt_prefix}: {prompt}"},
60
+ ]
61
+ template = processor.tokenizer.apply_chat_template(
62
+ messages, tokenize=False, add_generation_prompt=True
63
+ )
64
+ model_inputs = processor(text=template, images=image, return_tensors="pt").to("cuda")
65
+ torch.manual_seed(10) # greedy decoding is deterministic; seed is inert but matches diffusers
66
+ with torch.no_grad():
67
+ generated = model.generate(**model_inputs, max_new_tokens=512, **CFG.generation_kwargs)
68
+ generated_ids = [seq[len(model_inputs.input_ids[i]):] for i, seq in enumerate(generated)]
69
+ return processor.tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
70
+
71
+
72
+ with gr.Blocks(title="LTX-2.4 Prompt Enhancer") as demo:
73
+ gr.Markdown(
74
+ "# ✨ LTX-2.4 Prompt Enhancer\n"
75
+ "Gemma-4 (`google/gemma-4-E2B-it`) prompt enhancer for LTX-2.4 — expands a short prompt into "
76
+ "a detailed audio-visual caption in the model's training-caption style. Add a first-frame "
77
+ "image to enhance for image-to-video. Called by the LTX-2.4 video Spaces over `gradio_client`."
78
+ )
79
+ with gr.Row():
80
+ with gr.Column():
81
+ prompt = gr.Textbox(label="Prompt", lines=3, placeholder="e.g. a red fox in a snowy forest")
82
+ image = gr.Image(label="First frame (optional — for image-to-video)", type="pil")
83
+ btn = gr.Button("Enhance", variant="primary")
84
+ out = gr.Textbox(label="Enhanced prompt", lines=12, show_copy_button=True)
85
+ btn.click(enhance, inputs=[prompt, image], outputs=[out], api_name="enhance")
86
+
87
+ if __name__ == "__main__":
88
+ demo.launch(show_error=True)