cobramv12 commited on
Commit
2ff1102
·
verified ·
1 Parent(s): fece8ef

Feature: Added image reference support to D-Processor

Browse files
Files changed (1) hide show
  1. app.py +34 -20
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import sys
2
  import os
3
 
4
- # --- INYECCIÓN ATÓMICA REFORZADA (Línea 1) ---
5
  try:
6
  import huggingface_hub
7
  class MockHfFolder:
@@ -11,14 +11,11 @@ try:
11
  def save_token(token): pass
12
  @staticmethod
13
  def delete_token(): pass
14
-
15
- # Inyectamos en todos los niveles posibles para evitar ImportError
16
  huggingface_hub.HfFolder = MockHfFolder
17
  sys.modules["huggingface_hub.HfFolder"] = MockHfFolder
18
  setattr(huggingface_hub, "HfFolder", MockHfFolder)
19
  except: pass
20
 
21
- # Parche de Audio para Python 3.13
22
  try:
23
  import audioop_lts
24
  sys.modules["audioop"] = audioop_lts
@@ -29,7 +26,7 @@ except:
29
 
30
  import gradio as gr
31
 
32
- # --- SILENCIADOR DE API (Previene el cartel rojo) ---
33
  def fake_get_api_info(self, *args, **kwargs):
34
  return {"components": [], "endpoints": []}
35
  gr.Blocks.get_api_info = fake_get_api_info
@@ -47,16 +44,12 @@ LTX_MODEL = "Lightricks/LTX-Video"
47
  LTX_NSFW_LORA = "Lora-Daddy/Ltx2.3-real-nudity-early-alpha-30k-steps"
48
  NEG_DEFAULT = "blurry, low quality, bad anatomy, deformed, ugly, watermark, text"
49
 
50
- def load_t2i(lora_id=None, lora_scale=1.0):
51
- from diffusers import StableDiffusionXLPipeline
52
- pipe = StableDiffusionXLPipeline.from_pretrained(
 
53
  BASE_MODEL, torch_dtype=torch.float16, use_safetensors=True, variant="fp16"
54
  )
55
- if lora_id and len(lora_id.strip()) > 5:
56
- try:
57
- pipe.load_lora_weights(lora_id.strip())
58
- pipe.fuse_lora(lora_scale=lora_scale)
59
- except: pass
60
  return pipe
61
 
62
  def load_video():
@@ -68,11 +61,31 @@ def load_video():
68
  return pipe
69
 
70
  @spaces.GPU(duration=100)
71
- def generate_t2i(prompt, neg, lora_id, lora_scale, w, h):
72
- pipe = load_t2i(lora_id, lora_scale).to("cuda")
73
- img = pipe(prompt=prompt, negative_prompt=neg, num_inference_steps=30,
74
- guidance_scale=7.0, width=int(w), height=int(h),
75
- generator=torch.Generator("cuda").manual_seed(42)).images[0]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
  return img
77
 
78
  @spaces.GPU(duration=200)
@@ -95,10 +108,11 @@ def generate_video(prompt, init_image, lora_scale):
95
  with gr.Blocks(title="Image Utility v2.1") as demo:
96
  gr.HTML("<h1 style='text-align:center;'>🛠 Image Processing Utility v2.1.4</h1>")
97
  with gr.Tabs():
98
- with gr.Tab("D-Processor (T2I)"):
99
  with gr.Row():
100
  with gr.Column():
101
  t2i_p = gr.Textbox(label="Input Data String", lines=3)
 
102
  t2i_n = gr.Textbox(label="Excluded Data", value=NEG_DEFAULT)
103
  t2i_lora = gr.Textbox(label="Extension ID")
104
  t2i_ls = gr.Slider(0, 1.5, 0.8, label="Extension Weight")
@@ -107,7 +121,7 @@ with gr.Blocks(title="Image Utility v2.1") as demo:
107
  t2i_h = gr.Slider(512, 1024, 1024, step=64, label="Y-Axis")
108
  t2i_btn = gr.Button("Execute Process", variant="primary")
109
  t2i_out = gr.Image(label="Output Preview")
110
- t2i_btn.click(generate_t2i, [t2i_p, t2i_n, t2i_lora, t2i_ls, t2i_w, t2i_h], t2i_out)
111
 
112
  with gr.Tab("M-Sequence (Video)"):
113
  with gr.Row():
 
1
  import sys
2
  import os
3
 
4
+ # --- INYECCIÓN ATÓMICA REFORZADA ---
5
  try:
6
  import huggingface_hub
7
  class MockHfFolder:
 
11
  def save_token(token): pass
12
  @staticmethod
13
  def delete_token(): pass
 
 
14
  huggingface_hub.HfFolder = MockHfFolder
15
  sys.modules["huggingface_hub.HfFolder"] = MockHfFolder
16
  setattr(huggingface_hub, "HfFolder", MockHfFolder)
17
  except: pass
18
 
 
19
  try:
20
  import audioop_lts
21
  sys.modules["audioop"] = audioop_lts
 
26
 
27
  import gradio as gr
28
 
29
+ # --- SILENCIADOR DE API ---
30
  def fake_get_api_info(self, *args, **kwargs):
31
  return {"components": [], "endpoints": []}
32
  gr.Blocks.get_api_info = fake_get_api_info
 
44
  LTX_NSFW_LORA = "Lora-Daddy/Ltx2.3-real-nudity-early-alpha-30k-steps"
45
  NEG_DEFAULT = "blurry, low quality, bad anatomy, deformed, ugly, watermark, text"
46
 
47
+ def load_t2i(is_img2img=False):
48
+ from diffusers import StableDiffusionXLPipeline, StableDiffusionXLImg2ImgPipeline
49
+ cls = StableDiffusionXLImg2ImgPipeline if is_img2img else StableDiffusionXLPipeline
50
+ pipe = cls.from_pretrained(
51
  BASE_MODEL, torch_dtype=torch.float16, use_safetensors=True, variant="fp16"
52
  )
 
 
 
 
 
53
  return pipe
54
 
55
  def load_video():
 
61
  return pipe
62
 
63
  @spaces.GPU(duration=100)
64
+ def generate_t2i(prompt, neg, lora_id, lora_scale, w, h, init_img):
65
+ is_img2img = init_img is not None
66
+ pipe = load_t2i(is_img2img).to("cuda")
67
+
68
+ if lora_id and len(lora_id.strip()) > 5:
69
+ try:
70
+ pipe.load_lora_weights(lora_id.strip())
71
+ pipe.fuse_lora(lora_scale=lora_scale)
72
+ except: pass
73
+
74
+ kwargs = {
75
+ "prompt": prompt, "negative_prompt": neg, "num_inference_steps": 30,
76
+ "guidance_scale": 7.0, "generator": torch.Generator("cuda").manual_seed(42)
77
+ }
78
+
79
+ if is_img2img:
80
+ if isinstance(init_img, dict):
81
+ init_img = init_img["composite"] if "composite" in init_image else init_img["background"]
82
+ kwargs["image"] = Image.fromarray(init_img).convert("RGB").resize((int(w), int(h)))
83
+ kwargs["strength"] = 0.6 # Balance entre original y prompt
84
+ else:
85
+ kwargs["width"] = int(w)
86
+ kwargs["height"] = int(h)
87
+
88
+ img = pipe(**kwargs).images[0]
89
  return img
90
 
91
  @spaces.GPU(duration=200)
 
108
  with gr.Blocks(title="Image Utility v2.1") as demo:
109
  gr.HTML("<h1 style='text-align:center;'>🛠 Image Processing Utility v2.1.4</h1>")
110
  with gr.Tabs():
111
+ with gr.Tab("D-Processor (Image/T2I)"):
112
  with gr.Row():
113
  with gr.Column():
114
  t2i_p = gr.Textbox(label="Input Data String", lines=3)
115
+ t2i_img = gr.Image(label="Base Reference (Optional)", type="numpy", sources=["upload", "clipboard"])
116
  t2i_n = gr.Textbox(label="Excluded Data", value=NEG_DEFAULT)
117
  t2i_lora = gr.Textbox(label="Extension ID")
118
  t2i_ls = gr.Slider(0, 1.5, 0.8, label="Extension Weight")
 
121
  t2i_h = gr.Slider(512, 1024, 1024, step=64, label="Y-Axis")
122
  t2i_btn = gr.Button("Execute Process", variant="primary")
123
  t2i_out = gr.Image(label="Output Preview")
124
+ t2i_btn.click(generate_t2i, [t2i_p, t2i_n, t2i_lora, t2i_ls, t2i_w, t2i_h, t2i_img], t2i_out)
125
 
126
  with gr.Tab("M-Sequence (Video)"):
127
  with gr.Row():