cobramv12 commited on
Commit
beff9cc
Β·
verified Β·
1 Parent(s): 0e3aefb

Fix: Switch to Native Gradio SDK for ZeroGPU stability

Browse files
Files changed (2) hide show
  1. app.py +42 -99
  2. requirements.txt +7 -10
app.py CHANGED
@@ -1,9 +1,8 @@
1
- import sys
2
- import os
3
- import gc
4
- import torch
5
 
6
- # --- PARCHE DE GRADIO ---
7
  try:
8
  import gradio_client.utils as client_utils
9
  if not hasattr(client_utils, "_old_json_schema_to_python_type"):
@@ -14,135 +13,79 @@ try:
14
  client_utils._json_schema_to_python_type = patched_json_schema_to_python_type
15
  except: pass
16
 
17
- import spaces
18
- import gradio as gr
19
- from PIL import Image
20
- import tempfile
21
-
22
- # CONFIG
23
- MODELS = {
24
- "Pony Diffusion V6 XL": "cyberdelia/CyberRealisticPony",
25
- "RealVisXL V4.0": "SG161222/RealVisXL_V4.0"
26
- }
27
 
 
28
  LORAS = {
29
  "Ninguno": "",
30
- "πŸ’Ž NSFW: Real Nudity": "Lora-Daddy/Ltx2.3-real-nudity-early-alpha-30k-steps",
31
- "πŸ“œ DOCS: ID Card / Passport": "j0rdan/passport-sdxl",
32
- "πŸ”« WEAPONS: Tactical Gear": "Ostris/SDXL_LoRA_Test",
33
- "✍️ TEXT: Typography": "ntc/Typography-SDXL"
34
  }
35
 
36
- def flush():
37
- gc.collect()
38
- if torch.cuda.is_available():
39
- torch.cuda.empty_cache()
40
-
41
- # --- MOTOR DE IMAGEN ---
42
  @spaces.GPU(duration=120)
43
- def process_image(prompt, neg, model_name, lora_name, lora_id_custom, lora_scale, steps, cfg, w, h, init_img=None, strength=0.6):
44
  flush()
45
- # ImportaciΓ³n local para ahorrar RAM
46
  from diffusers import StableDiffusionXLPipeline, StableDiffusionXLImg2ImgPipeline
 
 
47
 
48
- model_id = MODELS.get(model_name)
49
-
50
- # Inyectar tags de calidad Pony
51
- if "Pony" in model_name:
52
- prompt = f"score_9, score_8_up, score_7_up, {prompt}"
53
-
54
- # Carga con bajo consumo de CPU
55
- pipe = StableDiffusionXLPipeline.from_pretrained(
56
- model_id, torch_dtype=torch.float16, variant="fp16", use_safetensors=True,
57
- low_cpu_mem_usage=True
58
- ).to("cuda")
59
-
60
- # Activar offload para ahorrar RAM
61
- pipe.enable_model_cpu_offload()
62
-
63
- lora_id = lora_id_custom if lora_id_custom else LORAS.get(lora_name)
64
- if lora_id:
65
- try:
66
- pipe.load_lora_weights(lora_id)
67
- pipe.fuse_lora(lora_scale=lora_scale)
68
  except: pass
69
 
70
- if init_img is not None:
71
  pipe_i2i = StableDiffusionXLImg2ImgPipeline.from_pipe(pipe)
72
- res = pipe_i2i(prompt=prompt, negative_prompt=neg, image=init_img, strength=strength, num_inference_steps=int(steps), guidance_scale=cfg).images[0]
73
  del pipe_i2i
74
  else:
75
- res = pipe(prompt=prompt, negative_prompt=neg, num_inference_steps=int(steps), guidance_scale=cfg, width=int(w), height=int(h)).images[0]
76
 
77
  del pipe
78
  flush()
79
  return res
80
 
81
- # --- MOTOR DE VIDEO ---
82
  @spaces.GPU(duration=250)
83
- def process_video(prompt, init_img, steps, cfg):
84
  flush()
85
  from diffusers import LTXPipeline
86
  from diffusers.utils import export_to_video
87
-
88
- pipe = LTXPipeline.from_pretrained(
89
- "Lightricks/LTX-Video", torch_dtype=torch.bfloat16,
90
- low_cpu_mem_usage=True
91
- ).to("cuda")
92
-
93
- pipe.enable_model_cpu_offload()
94
- pipe.enable_vae_slicing()
95
-
96
- kwargs = {
97
- "prompt": f"score_9, {prompt}",
98
- "negative_prompt": "low quality, blurry, static, ugly",
99
- "num_inference_steps": int(steps),
100
- "guidance_scale": cfg,
101
- "num_frames": 33,
102
- "width": 704,
103
- "height": 480
104
- }
105
-
106
- if init_img is not None:
107
- kwargs["image"] = init_img
108
-
109
- output = pipe(**kwargs)
110
  tmp = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False)
111
- export_to_video(output.frames[0], tmp.name, fps=16)
112
-
113
  del pipe
114
  flush()
115
  return tmp.name
116
 
117
- # --- INTERFAZ ---
118
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
119
- gr.HTML("<h1 style='text-align:center;'>🌌 Omni-Studio v3.1 (Power-Optimized)</h1>")
120
-
121
  with gr.Tabs():
122
- with gr.Tab("🎨 Imagen"):
123
  with gr.Row():
124
  with gr.Column():
125
- p = gr.Textbox(label="Prompt", lines=3)
126
- n = gr.Textbox(label="Negativo", value="blurry, lowres")
127
- m = gr.Dropdown(choices=list(MODELS.keys()), value=list(MODELS.keys())[0], label="Motor")
128
  l = gr.Dropdown(choices=list(LORAS.keys()), value="Ninguno", label="LoRA")
129
- with gr.Row():
130
- w = gr.Slider(512, 1024, 832, step=64, label="Ancho")
131
- h = gr.Slider(512, 1024, 1216, step=64, label="Alto")
132
- img_in = gr.Image(label="Imagen Base (Opcional)", type="pil")
133
- str_i = gr.Slider(0.1, 0.9, 0.6, label="Fuerza Mod")
134
- btn_i = gr.Button("GENERAR IMAGEN", variant="primary")
135
- out_i = gr.Image(label="Resultado")
136
-
137
  with gr.Tab("πŸŽ₯ Video"):
138
  with gr.Row():
139
  with gr.Column():
140
- vp = gr.Textbox(label="Video Prompt", lines=3)
141
- vin = gr.Image(label="Imagen Inicial (Opcional)", type="pil")
142
- btn_v = gr.Button("GENERAR VIDEO", variant="primary")
143
- out_v = gr.Video(label="Resultado Video")
144
 
145
- btn_i.click(process_image, [p, n, m, l, gr.Textbox(visible=False), gr.Number(value=0.8, visible=False), gr.Number(value=30, visible=False), gr.Number(value=7, visible=False), w, h, img_in, str_i], out_i)
146
- btn_v.click(process_video, [vp, vin, gr.Number(value=25, visible=False), gr.Number(value=3.5, visible=False)], out_v)
147
 
148
  demo.queue().launch(show_api=False, server_name="0.0.0.0", server_port=7860)
 
1
+ import sys, os, gc, torch, spaces, tempfile
2
+ import gradio as gr
3
+ from PIL import Image
 
4
 
5
+ # PATCH GRADIO
6
  try:
7
  import gradio_client.utils as client_utils
8
  if not hasattr(client_utils, "_old_json_schema_to_python_type"):
 
13
  client_utils._json_schema_to_python_type = patched_json_schema_to_python_type
14
  except: pass
15
 
16
+ def flush():
17
+ gc.collect()
18
+ if torch.cuda.is_available(): torch.cuda.empty_cache()
 
 
 
 
 
 
 
19
 
20
+ MODELS = {"Pony Diffusion V6 XL": "cyberdelia/CyberRealisticPony"}
21
  LORAS = {
22
  "Ninguno": "",
23
+ "πŸ’Ž NSFW: Real Nudity": "Lora-Daddy/Ltx2.3-real-nudity-early-alpha-30k-steps",
24
+ "πŸ“œ DOCS: ID Card": "j0rdan/passport-sdxl",
25
+ "πŸ”« WEAPONS: Tactical": "Ostris/SDXL_LoRA_Test"
 
26
  }
27
 
 
 
 
 
 
 
28
  @spaces.GPU(duration=120)
29
+ def generate(prompt, lora_name, w, h, init_img=None, strength=0.6):
30
  flush()
 
31
  from diffusers import StableDiffusionXLPipeline, StableDiffusionXLImg2ImgPipeline
32
+ p = f"score_9, score_8_up, score_7_up, {prompt}"
33
+ pipe = StableDiffusionXLPipeline.from_pretrained("cyberdelia/CyberRealisticPony", torch_dtype=torch.float16, variant="fp16", low_cpu_mem_usage=True).to("cuda")
34
 
35
+ lid = LORAS.get(lora_name)
36
+ if lid:
37
+ try: pipe.load_lora_weights(lid)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  except: pass
39
 
40
+ if init_img:
41
  pipe_i2i = StableDiffusionXLImg2ImgPipeline.from_pipe(pipe)
42
+ res = pipe_i2i(prompt=p, image=init_img, strength=strength, num_inference_steps=25).images[0]
43
  del pipe_i2i
44
  else:
45
+ res = pipe(prompt=p, num_inference_steps=30, width=int(w), height=int(h)).images[0]
46
 
47
  del pipe
48
  flush()
49
  return res
50
 
 
51
  @spaces.GPU(duration=250)
52
+ def video(prompt, init_img):
53
  flush()
54
  from diffusers import LTXPipeline
55
  from diffusers.utils import export_to_video
56
+ pipe = LTXPipeline.from_pretrained("Lightricks/LTX-Video", torch_dtype=torch.bfloat16, low_cpu_mem_usage=True).to("cuda")
57
+ kw = {"prompt": prompt, "num_inference_steps": 20, "num_frames": 25, "width": 704, "height": 480}
58
+ if init_img: kw["image"] = init_img
59
+ out = pipe(**kw)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  tmp = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False)
61
+ export_to_video(out.frames[0], tmp.name, fps=16)
 
62
  del pipe
63
  flush()
64
  return tmp.name
65
 
66
+ with gr.Blocks() as demo:
67
+ gr.HTML("<h1 style='text-align:center;'>🌌 Omni-Studio v3.2</h1>")
 
 
68
  with gr.Tabs():
69
+ with gr.Tab("πŸ–Ό Imagen"):
70
  with gr.Row():
71
  with gr.Column():
72
+ p = gr.Textbox(label="Prompt")
 
 
73
  l = gr.Dropdown(choices=list(LORAS.keys()), value="Ninguno", label="LoRA")
74
+ w = gr.Slider(512, 1024, 832, step=64)
75
+ h = gr.Slider(512, 1024, 1216, step=64)
76
+ img = gr.Image(label="Base", type="pil")
77
+ st = gr.Slider(0.1, 0.9, 0.6, label="Mod Strength")
78
+ btn = gr.Button("GENERAR")
79
+ out = gr.Image(label="Resultado")
 
 
80
  with gr.Tab("πŸŽ₯ Video"):
81
  with gr.Row():
82
  with gr.Column():
83
+ vp = gr.Textbox(label="Prompt")
84
+ vi = gr.Image(label="Base", type="pil")
85
+ vbtn = gr.Button("GENERAR VIDEO")
86
+ vout = gr.Video(label="Resultado")
87
 
88
+ btn.click(generate, [p, l, w, h, img, st], out)
89
+ vbtn.click(video, [vp, vi], vout)
90
 
91
  demo.queue().launch(show_api=False, server_name="0.0.0.0", server_port=7860)
requirements.txt CHANGED
@@ -1,14 +1,11 @@
1
- spaces
2
  gradio==4.44.1
3
- fastapi==0.112.2
4
- starlette==0.38.2
5
- huggingface-hub==0.24.2
6
- audioop-lts
7
  diffusers>=0.31.0
8
- transformers>=4.44.0
9
- accelerate>=0.33.0
10
  peft
11
- torch
12
  sentencepiece
13
- imageio[ffmpeg]
14
- av
 
 
 
1
  gradio==4.44.1
2
+ spaces
3
+ torch
 
 
4
  diffusers>=0.31.0
5
+ transformers
6
+ accelerate
7
  peft
 
8
  sentencepiece
9
+ safetensors
10
+ numpy
11
+ pillow