Spaces:
Running on Zero
Running on Zero
Create app2.py
#17
by Gigantos89 - opened
app2.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os, subprocess, sys, random, tempfile, torch
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
from huggingface_hub import hf_hub_download, snapshot_download
|
| 4 |
+
|
| 5 |
+
# --- Mérnöki környezet beállítása ---
|
| 6 |
+
os.environ["TORCH_COMPILE_DISABLE"] = "1"
|
| 7 |
+
os.environ["TORCHDYNAMO_DISABLE"] = "1"
|
| 8 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 9 |
+
|
| 10 |
+
# Függőségek kényszerített telepítése (PRO környezethez optimalizálva)
|
| 11 |
+
subprocess.run([sys.executable, "-m", "pip", "install", "xformers==0.0.32.post2", "--no-build-isolation"], check=False)
|
| 12 |
+
LTX_REPO_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "LTX-2")
|
| 13 |
+
if not os.path.exists(LTX_REPO_DIR):
|
| 14 |
+
subprocess.run(["git", "clone", "https://github.com/Lightricks/LTX-2.git", LTX_REPO_DIR], check=True)
|
| 15 |
+
subprocess.run(["git", "-C", LTX_REPO_DIR, "checkout", "ae855f8538843825f9015a419cf4ba5edaf5eec2"], check=True)
|
| 16 |
+
|
| 17 |
+
subprocess.run([sys.executable, "-m", "pip", "install", "-e", os.path.join(LTX_REPO_DIR, "packages", "ltx-core"), "-e", os.path.join(LTX_REPO_DIR, "packages", "ltx-pipelines")], check=True)
|
| 18 |
+
sys.path.insert(0, os.path.join(LTX_REPO_DIR, "packages", "ltx-pipelines", "src"))
|
| 19 |
+
sys.path.insert(0, os.path.join(LTX_REPO_DIR, "packages", "ltx-core", "src"))
|
| 20 |
+
|
| 21 |
+
import gradio as gr
|
| 22 |
+
from ltx_core.model.video_vae import TilingConfig, get_video_chunks_number
|
| 23 |
+
from ltx_pipelines.distilled import DistilledPipeline
|
| 24 |
+
from ltx_pipelines.utils.media_io import encode_video
|
| 25 |
+
|
| 26 |
+
# --- MODELL BETÖLTÉSE (EREDETI BF16 MINŐSÉG) ---
|
| 27 |
+
# A 46.1 GB-os teljes verziót töltjük be
|
| 28 |
+
checkpoint_path = hf_hub_download(repo_id="Lightricks/LTX-2.3", filename="ltx-2.3-22b-distilled-1.1.safetensors", token=HF_TOKEN)
|
| 29 |
+
spatial_upsampler_path = hf_hub_download(repo_id="Lightricks/LTX-2.3", filename="ltx-2.3-spatial-upscaler-x2-1.1.safetensors", token=HF_TOKEN)
|
| 30 |
+
gemma_root = snapshot_download(repo_id="google/gemma-3-12b-it-qat-q4_0-unquantized", token=HF_TOKEN)
|
| 31 |
+
|
| 32 |
+
# Pipeline inicializálás (Kvantálás nélkül a maximális részletességért)
|
| 33 |
+
pipeline = DistilledPipeline(
|
| 34 |
+
distilled_checkpoint_path=checkpoint_path,
|
| 35 |
+
spatial_upsampler_path=spatial_upsampler_path,
|
| 36 |
+
gemma_root=gemma_root,
|
| 37 |
+
loras=[],
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
@torch.inference_mode()
|
| 41 |
+
def generate_hq_video(prompt, duration=5.0, seed=-1, progress=gr.Progress(track_tqdm=True)):
|
| 42 |
+
try:
|
| 43 |
+
torch.cuda.empty_cache()
|
| 44 |
+
current_seed = random.randint(0, 2**32 - 1) if seed == -1 else int(seed)
|
| 45 |
+
|
| 46 |
+
# Frame számítás: 24 fps mellett 5 sec = 121 frame
|
| 47 |
+
num_frames = ((int(duration * 24) + 1 - 1 + 7) // 8) * 8 + 1
|
| 48 |
+
|
| 49 |
+
# A minőség fokozása érdekében kényszerített prompt-javítást használunk
|
| 50 |
+
video, _ = pipeline(
|
| 51 |
+
prompt=prompt,
|
| 52 |
+
seed=current_seed,
|
| 53 |
+
height=720,
|
| 54 |
+
width=1280,
|
| 55 |
+
num_frames=num_frames,
|
| 56 |
+
frame_rate=24.0,
|
| 57 |
+
images=[], # Text-to-Video mód
|
| 58 |
+
enhance_prompt=True,
|
| 59 |
+
tiling_config=TilingConfig.default()
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
out_file = tempfile.mktemp(suffix=".mp4")
|
| 63 |
+
# Kódolás hang nélkül, magas bitrátával
|
| 64 |
+
encode_video(
|
| 65 |
+
video,
|
| 66 |
+
24.0,
|
| 67 |
+
None, # Audio kikapcsolva a kérésnek megfelelően
|
| 68 |
+
out_file,
|
| 69 |
+
get_video_chunks_number(num_frames, TilingConfig.default())
|
| 70 |
+
)
|
| 71 |
+
return out_file, current_seed
|
| 72 |
+
except Exception as e:
|
| 73 |
+
print(f"Manufacturing Error: {e}")
|
| 74 |
+
return None, seed
|
| 75 |
+
|
| 76 |
+
# --- Gradio Felület (Mérnöki QC Dashboard) ---
|
| 77 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 78 |
+
gr.Markdown("# LTX-2.3 BF16 High-Fidelity (720p, No Audio)")
|
| 79 |
+
with gr.Row():
|
| 80 |
+
with gr.Column():
|
| 81 |
+
p = gr.Textbox(label="Szöveges utasítás", value="Cinematic motion, extreme detail, 8k, realistic")
|
| 82 |
+
d = gr.Slider(label="Időtartam", minimum=1, maximum=5, value=5)
|
| 83 |
+
s = gr.Number(label="Seed (-1 a véletlenhez)", value=-1)
|
| 84 |
+
btn = gr.Button("GYÁRTÁS INDÍTÁSA", variant="primary")
|
| 85 |
+
output_video = gr.Video(label="QC Eredmény")
|
| 86 |
+
|
| 87 |
+
# API végpont rögzítése a local script számára
|
| 88 |
+
btn.click(generate_hq_video, [p, d, s], [output_video, s], api_name="generate_video")
|
| 89 |
+
|
| 90 |
+
if __name__ == "__main__":
|
| 91 |
+
demo.launch()
|