prithivMLmods commited on
Commit
60b7d8a
·
verified ·
1 Parent(s): e590a79

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +276 -231
app.py CHANGED
@@ -9,9 +9,11 @@ from diffusers import FlowMatchEulerDiscreteScheduler
9
  from qwenimage.pipeline_qwenimage_edit_plus import QwenImageEditPlusPipeline
10
  from qwenimage.transformer_qwenimage import QwenImageTransformer2DModel
11
  from qwenimage.qwen_fa3_processor import QwenDoubleStreamAttnProcessorFA3
 
12
  from gradio.themes import Soft
13
  from gradio.themes.utils import colors, fonts, sizes
14
 
 
15
  colors.orange_red = colors.Color(
16
  name="orange_red",
17
  c50="#FFF0E5",
@@ -80,10 +82,11 @@ class OrangeRedTheme(Soft):
80
 
81
  orange_red_theme = OrangeRedTheme()
82
 
 
83
  MAX_SEED = np.iinfo(np.int32).max
 
84
  dtype = torch.bfloat16
85
  device = "cuda" if torch.cuda.is_available() else "cpu"
86
-
87
  pipe = QwenImageEditPlusPipeline.from_pretrained(
88
  "Qwen/Qwen-Image-Edit-2511",
89
  transformer=QwenImageTransformer2DModel.from_pretrained(
@@ -93,7 +96,6 @@ pipe = QwenImageEditPlusPipeline.from_pretrained(
93
  ),
94
  torch_dtype=dtype
95
  ).to(device)
96
-
97
  try:
98
  pipe.transformer.set_attn_processor(QwenDoubleStreamAttnProcessorFA3())
99
  print("Flash Attention 3 Processor set successfully.")
@@ -107,7 +109,6 @@ ADAPTER_SPECS = {
107
  "adapter_name": "multi-angle-lighting"
108
  },
109
  }
110
-
111
  loaded = False
112
 
113
  AZIMUTH_MAP = {
@@ -127,12 +128,19 @@ ELEVATION_MAP = {
127
  90: "Above"
128
  }
129
 
 
 
130
  def snap_to_nearest(value, options):
 
131
  return min(options, key=lambda x: abs(x - value))
132
 
133
  def build_lighting_prompt(azimuth: float, elevation: float) -> str:
 
 
 
134
  azimuth_snapped = snap_to_nearest(azimuth, list(AZIMUTH_MAP.keys()))
135
  elevation_snapped = snap_to_nearest(elevation, list(ELEVATION_MAP.keys()))
 
136
  if elevation_snapped == 0:
137
  return f"Light source from the {AZIMUTH_MAP[azimuth_snapped]}"
138
  else:
@@ -151,6 +159,8 @@ def infer_lighting_edit(
151
  width: int = 1024,
152
  ):
153
  global loaded
 
 
154
  if not loaded:
155
  pipe.load_lora_weights(
156
  ADAPTER_SPECS["Multi-Angle-Lighting"]["repo"],
@@ -159,20 +169,15 @@ def infer_lighting_edit(
159
  )
160
  pipe.set_adapters([ADAPTER_SPECS["Multi-Angle-Lighting"]["adapter_name"]], adapter_weights=[1.0])
161
  loaded = True
162
-
163
  prompt = build_lighting_prompt(azimuth, elevation)
164
  print(f"Generated Prompt: {prompt}")
165
-
166
  if randomize_seed:
167
  seed = random.randint(0, MAX_SEED)
168
-
169
  generator = torch.Generator(device=device).manual_seed(seed)
170
-
171
  if image is None:
172
  raise gr.Error("Please upload an image first.")
173
-
174
  pil_image = image.convert("RGB") if isinstance(image, Image.Image) else Image.open(image).convert("RGB")
175
-
176
  result = pipe(
177
  image=[pil_image],
178
  prompt=prompt,
@@ -183,7 +188,6 @@ def infer_lighting_edit(
183
  guidance_scale=guidance_scale,
184
  num_images_per_prompt=1,
185
  ).images[0]
186
-
187
  return result, seed, prompt
188
 
189
  def update_dimensions_on_upload(image):
@@ -202,61 +206,76 @@ def update_dimensions_on_upload(image):
202
  new_height = (new_height // 8) * 8
203
  return new_width, new_height
204
 
 
 
205
  class LightingControl3D(gr.HTML):
 
 
 
206
  def __init__(self, value=None, imageUrl=None, **kwargs):
207
  if value is None:
208
  value = {"azimuth": 0, "elevation": 0}
209
-
210
  html_template = """
211
  <div id="lighting-control-wrapper" style="width: 100%; height: 450px; position: relative; background: #1a1a1a; border-radius: 12px; overflow: hidden;">
212
  <div id="prompt-overlay" style="position: absolute; bottom: 10px; left: 50%; transform: translateX(-50%); background: rgba(0,0,0,0.8); padding: 8px 16px; border-radius: 8px; font-family: monospace; font-size: 12px; color: #00ff88; white-space: nowrap; z-index: 10;"></div>
213
  </div>
214
  """
215
-
216
  js_on_load = """
217
  (() => {
218
  const wrapper = element.querySelector('#lighting-control-wrapper');
219
  const promptOverlay = element.querySelector('#prompt-overlay');
220
-
 
221
  const initScene = () => {
222
  if (typeof THREE === 'undefined') {
223
  setTimeout(initScene, 100);
224
  return;
225
  }
226
-
 
227
  const scene = new THREE.Scene();
228
  scene.background = new THREE.Color(0x1a1a1a);
229
-
230
  const camera = new THREE.PerspectiveCamera(50, wrapper.clientWidth / wrapper.clientHeight, 0.1, 1000);
231
- camera.position.set(5, 3.5, 5);
232
- camera.lookAt(0, 0.8, 0);
233
-
234
  const renderer = new THREE.WebGLRenderer({ antialias: true });
235
  renderer.setSize(wrapper.clientWidth, wrapper.clientHeight);
236
  renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
237
  renderer.shadowMap.enabled = true;
238
  renderer.shadowMap.type = THREE.PCFSoftShadowMap;
239
  wrapper.insertBefore(renderer.domElement, promptOverlay);
240
-
241
- scene.add(new THREE.AmbientLight(0xffffff, 0.15));
242
-
 
 
243
  const ground = new THREE.Mesh(
244
- new THREE.PlaneGeometry(12, 12),
245
- new THREE.ShadowMaterial({ opacity: 0.35 })
246
  );
247
  ground.rotation.x = -Math.PI / 2;
248
- ground.position.y = -0.01;
249
  ground.receiveShadow = true;
250
  scene.add(ground);
251
-
252
- scene.add(new THREE.GridHelper(10, 20, 0x333333, 0x222222));
253
-
254
- const CENTER = new THREE.Vector3(0, 0.8, 0);
255
- const BASE_DISTANCE = 3.2;
256
-
 
 
 
 
 
257
  let azimuthAngle = props.value?.azimuth || 0;
258
  let elevationAngle = props.value?.elevation || 0;
259
-
 
260
  const azimuthSteps = [0, 45, 90, 135, 180, 225, 270, 315];
261
  const elevationSteps = [-90, 0, 90];
262
  const azimuthNames = {
@@ -265,11 +284,12 @@ class LightingControl3D(gr.HTML):
265
  270: 'Left', 315: 'Left Front'
266
  };
267
  const elevationNames = { '-90': 'Below', '0': '', '90': 'Above' };
268
-
269
  function snapToNearest(value, steps) {
270
  return steps.reduce((prev, curr) => Math.abs(curr - value) < Math.abs(prev - value) ? curr : prev);
271
  }
272
-
 
273
  function createPlaceholderTexture() {
274
  const canvas = document.createElement('canvas');
275
  canvas.width = 256;
@@ -293,25 +313,28 @@ class LightingControl3D(gr.HTML):
293
  ctx.stroke();
294
  return new THREE.CanvasTexture(canvas);
295
  }
296
-
 
297
  let currentTexture = createPlaceholderTexture();
298
- const planeMaterial = new THREE.MeshStandardMaterial({
299
- map: currentTexture,
300
- side: THREE.DoubleSide,
301
- roughness: 0.6,
302
- metalness: 0.05
303
- });
304
- let targetPlane = new THREE.Mesh(new THREE.PlaneGeometry(1.5, 1.5), planeMaterial);
305
  targetPlane.position.copy(CENTER);
306
  targetPlane.receiveShadow = true;
307
  scene.add(targetPlane);
308
-
 
309
  function updateTextureFromUrl(url) {
310
  if (!url) {
311
  planeMaterial.map = createPlaceholderTexture();
312
  planeMaterial.needsUpdate = true;
 
 
 
 
 
313
  return;
314
  }
 
315
  const loader = new THREE.TextureLoader();
316
  loader.crossOrigin = 'anonymous';
317
  loader.load(url, (texture) => {
@@ -319,14 +342,16 @@ class LightingControl3D(gr.HTML):
319
  texture.magFilter = THREE.LinearFilter;
320
  planeMaterial.map = texture;
321
  planeMaterial.needsUpdate = true;
322
-
323
  const img = texture.image;
324
  if (img && img.width && img.height) {
325
  const aspect = img.width / img.height;
326
- const maxSize = 1.8;
327
- let planeWidth = maxSize;
328
- let planeHeight = maxSize / aspect;
329
- if (aspect < 1) {
 
 
330
  planeHeight = maxSize;
331
  planeWidth = maxSize * aspect;
332
  }
@@ -339,205 +364,208 @@ class LightingControl3D(gr.HTML):
339
  targetPlane.receiveShadow = true;
340
  scene.add(targetPlane);
341
  }
 
 
342
  });
343
  }
344
-
345
  if (props.imageUrl) {
346
  updateTextureFromUrl(props.imageUrl);
347
  }
348
-
349
- // ── Redesigned Softbox (red housing, white soft light) ──
350
- const softboxGroup = new THREE.Group();
351
-
352
- // Main softbox body (red)
353
- const softboxGeo = new THREE.BoxGeometry(1.8, 1.2, 0.25);
354
- const softboxMat = new THREE.MeshStandardMaterial({
355
- color: 0xc41e3a, // red
356
- roughness: 0.7,
357
- metalness: 0.3
358
- });
359
- const softboxBody = new THREE.Mesh(softboxGeo, softboxMat);
360
- softboxGroup.add(softboxBody);
361
-
362
- // Diffusion panel (white glowing)
363
- const diffusionGeo = new THREE.PlaneGeometry(1.68, 1.08);
364
- const diffusionMat = new THREE.MeshStandardMaterial({
365
- color: 0xffffff,
366
- emissive: 0xffffff,
367
- emissiveIntensity: 4.5,
368
- roughness: 0.9,
369
- metalness: 0.0,
370
- side: THREE.FrontSide
371
- });
372
- const diffusion = new THREE.Mesh(diffusionGeo, diffusionMat);
373
- diffusion.position.z = 0.126;
374
- softboxGroup.add(diffusion);
375
-
376
- // Simple barn doors (red)
377
- const doorMat = softboxMat;
378
- const topDoor = new THREE.Mesh(new THREE.BoxGeometry(1.8, 0.4, 0.04), doorMat);
379
- topDoor.position.set(0, 0.6, 0.13);
380
- topDoor.rotation.x = THREE.MathUtils.degToRad(110);
381
- softboxGroup.add(topDoor);
382
-
383
- const bottomDoor = new THREE.Mesh(new THREE.BoxGeometry(1.8, 0.4, 0.04), doorMat);
384
- bottomDoor.position.set(0, -0.6, 0.13);
385
- bottomDoor.rotation.x = THREE.MathUtils.degToRad(-110);
386
- softboxGroup.add(bottomDoor);
387
-
388
- softboxGroup.position.set(0, 0, 0);
389
- scene.add(softboxGroup);
390
-
391
- // SpotLight for stronger cast shadow
392
- const spotLight = new THREE.SpotLight(0xffffff, 12, 12, Math.PI / 3.5, 0.4, 1.5);
393
- spotLight.position.set(0, 0, 0.15);
394
  spotLight.castShadow = true;
395
- spotLight.shadow.mapSize.width = 2048;
396
- spotLight.shadow.mapSize.height = 2048;
397
- spotLight.shadow.camera.near = 0.1;
398
- spotLight.shadow.camera.far = 50;
399
- spotLight.shadow.bias = -0.0005;
400
- softboxGroup.add(spotLight);
401
-
402
  const lightTarget = new THREE.Object3D();
403
  lightTarget.position.copy(CENTER);
404
  scene.add(lightTarget);
405
  spotLight.target = lightTarget;
406
-
407
- // ── Controls ──
 
 
 
 
408
  const azimuthRing = new THREE.Mesh(
409
- new THREE.TorusGeometry(2.8, 0.05, 16, 64),
410
- new THREE.MeshStandardMaterial({ color: 0x444444, emissive: 0x222222 })
411
  );
412
  azimuthRing.rotation.x = Math.PI / 2;
413
  azimuthRing.position.y = 0.05;
414
  scene.add(azimuthRing);
415
-
416
  const azimuthHandle = new THREE.Mesh(
417
- new THREE.SphereGeometry(0.22, 24, 24),
418
- new THREE.MeshStandardMaterial({ color: 0xffcc00, emissive: 0xffcc00, emissiveIntensity: 0.7 }) // Yellow
419
  );
420
  azimuthHandle.userData.type = 'azimuth';
421
  scene.add(azimuthHandle);
422
-
 
 
 
 
 
 
 
423
  const elevationArc = new THREE.Mesh(
424
- new THREE.TorusGeometry(1.8, 0.05, 16, 64, Math.PI),
425
- new THREE.MeshStandardMaterial({ color: 0x007bff, emissive: 0x007bff, emissiveIntensity: 0.4 }) // Blue arc
426
  );
427
- elevationArc.rotation.z = Math.PI / 2;
428
- elevationArc.position.set(-1.0, CENTER.y, 0);
429
  scene.add(elevationArc);
430
-
431
  const elevationHandle = new THREE.Mesh(
432
- new THREE.SphereGeometry(0.22, 24, 24),
433
- new THREE.MeshStandardMaterial({ color: 0x007bff, emissive: 0x007bff, emissiveIntensity: 0.7 }) // Blue
434
  );
435
  elevationHandle.userData.type = 'elevation';
436
  scene.add(elevationHandle);
437
-
438
- // Reset Button inside 3D view
439
- const resetBtn = document.createElement('div');
440
- resetBtn.innerHTML = 'Reset';
441
- resetBtn.style.position = 'absolute';
442
- resetBtn.style.top = '12px';
443
- resetBtn.style.right = '12px';
444
- resetBtn.style.background = 'rgba(220, 53, 69, 0.85)';
445
- resetBtn.style.color = 'white';
446
- resetBtn.style.padding = '8px 14px';
447
- resetBtn.style.borderRadius = '8px';
448
- resetBtn.style.cursor = 'pointer';
449
- resetBtn.style.zIndex = '15';
450
- resetBtn.style.fontSize = '14px';
451
- resetBtn.style.fontWeight = 'bold';
452
- resetBtn.style.boxShadow = '0 2px 8px rgba(0,0,0,0.5)';
453
- wrapper.appendChild(resetBtn);
454
-
455
- resetBtn.addEventListener('click', () => {
 
 
 
 
 
 
 
456
  azimuthAngle = 0;
457
  elevationAngle = 0;
458
  updatePositions();
459
  updatePropsAndTrigger();
460
  });
461
-
462
  function updatePositions() {
 
463
  const azRad = THREE.MathUtils.degToRad(azimuthAngle);
464
  const elRad = THREE.MathUtils.degToRad(elevationAngle);
465
-
466
- const lightX = BASE_DISTANCE * Math.sin(azRad) * Math.cos(elRad);
467
- const lightY = BASE_DISTANCE * Math.sin(elRad) + CENTER.y;
468
- const lightZ = BASE_DISTANCE * Math.cos(azRad) * Math.cos(elRad);
469
-
470
- softboxGroup.position.set(lightX, lightY, lightZ);
471
- softboxGroup.lookAt(CENTER);
472
-
473
- azimuthHandle.position.set(2.8 * Math.sin(azRad), 0.05, 2.8 * Math.cos(azRad));
474
- elevationHandle.position.set(-1.0, 1.8 * Math.sin(elRad) + CENTER.y, 1.8 * Math.cos(elRad));
475
-
 
476
  const azSnap = snapToNearest(azimuthAngle, azimuthSteps);
477
  const elSnap = snapToNearest(elevationAngle, elevationSteps);
478
- let promptText = 'Light source from';
479
  if (elSnap !== 0) {
480
- promptText += ' ' + elevationNames[String(elSnap)];
481
  } else {
482
- promptText += ' the ' + azimuthNames[azSnap];
483
  }
484
- promptOverlay.textContent = promptText;
485
  }
486
-
487
  function updatePropsAndTrigger() {
488
  const azSnap = snapToNearest(azimuthAngle, azimuthSteps);
489
  const elSnap = snapToNearest(elevationAngle, elevationSteps);
 
490
  props.value = { azimuth: azSnap, elevation: elSnap };
491
  trigger('change', props.value);
492
  }
493
-
 
494
  const raycaster = new THREE.Raycaster();
495
  const mouse = new THREE.Vector2();
496
  let isDragging = false;
497
  let dragTarget = null;
498
-
 
 
499
  const canvas = renderer.domElement;
500
-
501
  canvas.addEventListener('mousedown', (e) => {
502
  const rect = canvas.getBoundingClientRect();
503
  mouse.x = ((e.clientX - rect.left) / rect.width) * 2 - 1;
504
  mouse.y = -((e.clientY - rect.top) / rect.height) * 2 + 1;
505
-
506
  raycaster.setFromCamera(mouse, camera);
507
  const intersects = raycaster.intersectObjects([azimuthHandle, elevationHandle]);
508
-
509
  if (intersects.length > 0) {
510
  isDragging = true;
511
  dragTarget = intersects[0].object;
512
- dragTarget.material.emissiveIntensity = 1.2;
513
- dragTarget.scale.setScalar(1.4);
 
514
  canvas.style.cursor = 'grabbing';
515
  }
516
  });
517
-
518
  canvas.addEventListener('mousemove', (e) => {
519
  const rect = canvas.getBoundingClientRect();
520
  mouse.x = ((e.clientX - rect.left) / rect.width) * 2 - 1;
521
  mouse.y = -((e.clientY - rect.top) / rect.height) * 2 + 1;
522
-
523
  if (isDragging && dragTarget) {
524
  raycaster.setFromCamera(mouse, camera);
525
-
526
  if (dragTarget.userData.type === 'azimuth') {
527
  const plane = new THREE.Plane(new THREE.Vector3(0, 1, 0), -0.05);
528
- if (raycaster.ray.intersectPlane(plane, new THREE.Vector3())) {
529
- const intersect = new THREE.Vector3();
530
- raycaster.ray.intersectPlane(plane, intersect);
531
- azimuthAngle = THREE.MathUtils.radToDeg(Math.atan2(intersect.x, intersect.z));
532
  if (azimuthAngle < 0) azimuthAngle += 360;
533
  }
534
  } else if (dragTarget.userData.type === 'elevation') {
535
- const plane = new THREE.Plane(new THREE.Vector3(1, 0, 0), -1.0);
536
- if (raycaster.ray.intersectPlane(plane, new THREE.Vector3())) {
537
- const intersect = new THREE.Vector3();
538
- raycaster.ray.intersectPlane(plane, intersect);
539
- const relY = intersect.y - CENTER.y;
540
- const relZ = intersect.z;
541
  elevationAngle = THREE.MathUtils.clamp(THREE.MathUtils.radToDeg(Math.atan2(relY, relZ)), -90, 90);
542
  }
543
  }
@@ -546,43 +574,44 @@ class LightingControl3D(gr.HTML):
546
  raycaster.setFromCamera(mouse, camera);
547
  const intersects = raycaster.intersectObjects([azimuthHandle, elevationHandle]);
548
  [azimuthHandle, elevationHandle].forEach(h => {
549
- h.material.emissiveIntensity = 0.7;
550
  h.scale.setScalar(1);
551
  });
552
  if (intersects.length > 0) {
553
- intersects[0].object.material.emissiveIntensity = 1.0;
554
- intersects[0].object.scale.setScalar(1.2);
555
  canvas.style.cursor = 'grab';
556
  } else {
557
  canvas.style.cursor = 'default';
558
  }
559
  }
560
  });
561
-
562
  const onMouseUp = () => {
563
  if (dragTarget) {
564
- dragTarget.material.emissiveIntensity = 0.7;
565
  dragTarget.scale.setScalar(1);
566
-
 
567
  const targetAz = snapToNearest(azimuthAngle, azimuthSteps);
568
  const targetEl = snapToNearest(elevationAngle, elevationSteps);
569
-
570
  const startAz = azimuthAngle, startEl = elevationAngle;
571
  const startTime = Date.now();
572
-
573
  function animateSnap() {
574
  const t = Math.min((Date.now() - startTime) / 200, 1);
575
  const ease = 1 - Math.pow(1 - t, 3);
576
-
577
  let azDiff = targetAz - startAz;
578
  if (azDiff > 180) azDiff -= 360;
579
  if (azDiff < -180) azDiff += 360;
580
  azimuthAngle = startAz + azDiff * ease;
581
  if (azimuthAngle < 0) azimuthAngle += 360;
582
  if (azimuthAngle >= 360) azimuthAngle -= 360;
583
-
584
  elevationAngle = startEl + (targetEl - startEl) * ease;
585
-
586
  updatePositions();
587
  if (t < 1) requestAnimationFrame(animateSnap);
588
  else updatePropsAndTrigger();
@@ -593,75 +622,83 @@ class LightingControl3D(gr.HTML):
593
  dragTarget = null;
594
  canvas.style.cursor = 'default';
595
  };
596
-
597
  canvas.addEventListener('mouseup', onMouseUp);
598
  canvas.addEventListener('mouseleave', onMouseUp);
599
-
600
- // Touch events (simplified)
601
  canvas.addEventListener('touchstart', (e) => {
602
  e.preventDefault();
603
  const touch = e.touches[0];
604
  const rect = canvas.getBoundingClientRect();
605
  mouse.x = ((touch.clientX - rect.left) / rect.width) * 2 - 1;
606
  mouse.y = -((touch.clientY - rect.top) / rect.height) * 2 + 1;
607
-
608
  raycaster.setFromCamera(mouse, camera);
609
  const intersects = raycaster.intersectObjects([azimuthHandle, elevationHandle]);
610
-
611
  if (intersects.length > 0) {
612
  isDragging = true;
613
  dragTarget = intersects[0].object;
614
- dragTarget.material.emissiveIntensity = 1.2;
615
- dragTarget.scale.setScalar(1.4);
 
616
  }
617
  }, { passive: false });
618
-
619
  canvas.addEventListener('touchmove', (e) => {
620
  e.preventDefault();
621
  const touch = e.touches[0];
622
  const rect = canvas.getBoundingClientRect();
623
  mouse.x = ((touch.clientX - rect.left) / rect.width) * 2 - 1;
624
  mouse.y = -((touch.clientY - rect.top) / rect.height) * 2 + 1;
625
-
626
  if (isDragging && dragTarget) {
627
  raycaster.setFromCamera(mouse, camera);
628
- // same logic as mousemove...
629
  if (dragTarget.userData.type === 'azimuth') {
630
  const plane = new THREE.Plane(new THREE.Vector3(0, 1, 0), -0.05);
631
- const intersect = new THREE.Vector3();
632
- if (raycaster.ray.intersectPlane(plane, intersect)) {
633
- azimuthAngle = THREE.MathUtils.radToDeg(Math.atan2(intersect.x, intersect.z));
634
  if (azimuthAngle < 0) azimuthAngle += 360;
635
  }
636
  } else if (dragTarget.userData.type === 'elevation') {
637
- const plane = new THREE.Plane(new THREE.Vector3(1, 0, 0), -1.0);
638
- const intersect = new THREE.Vector3();
639
- if (raycaster.ray.intersectPlane(plane, intersect)) {
640
- const relY = intersect.y - CENTER.y;
641
- const relZ = intersect.z;
642
  elevationAngle = THREE.MathUtils.clamp(THREE.MathUtils.radToDeg(Math.atan2(relY, relZ)), -90, 90);
643
  }
644
  }
645
  updatePositions();
646
  }
647
  }, { passive: false });
648
-
649
- canvas.addEventListener('touchend', onMouseUp, { passive: false });
650
-
 
 
 
 
 
 
 
 
651
  updatePositions();
652
-
 
653
  function render() {
654
  requestAnimationFrame(render);
655
  renderer.render(scene, camera);
656
  }
657
  render();
658
-
 
659
  new ResizeObserver(() => {
660
  camera.aspect = wrapper.clientWidth / wrapper.clientHeight;
661
  camera.updateProjectionMatrix();
662
  renderer.setSize(wrapper.clientWidth, wrapper.clientHeight);
663
  }).observe(wrapper);
664
-
 
665
  wrapper._updateFromProps = (newVal) => {
666
  if (newVal && typeof newVal === 'object') {
667
  azimuthAngle = newVal.azimuth ?? azimuthAngle;
@@ -669,9 +706,10 @@ class LightingControl3D(gr.HTML):
669
  updatePositions();
670
  }
671
  };
672
-
673
  wrapper._updateTexture = updateTextureFromUrl;
674
-
 
675
  let lastImageUrl = props.imageUrl;
676
  let lastValue = JSON.stringify(props.value);
677
  setInterval(() => {
@@ -690,11 +728,11 @@ class LightingControl3D(gr.HTML):
690
  }
691
  }, 100);
692
  };
693
-
694
  initScene();
695
  })();
696
  """
697
-
698
  super().__init__(
699
  value=value,
700
  html_template=html_template,
@@ -703,6 +741,8 @@ class LightingControl3D(gr.HTML):
703
  **kwargs
704
  )
705
 
 
 
706
  css = '''
707
  #col-container { max-width: 1200px; margin: 0 auto; }
708
  .dark .progress-text { color: white !important; }
@@ -710,25 +750,25 @@ css = '''
710
  .slider-row { display: flex; gap: 10px; align-items: center; }
711
  #main-title h1 {font-size: 2.4em !important;}
712
  '''
713
-
714
  with gr.Blocks(css=css) as demo:
715
  gr.Markdown("# **Qwen-Image-Edit-2511-3D-Lighting-Control**", elem_id="main-title")
716
  gr.Markdown("Control lighting directions using the **3D viewport** or **sliders**. Use [Multi-Angle-Lighting](https://huggingface.co/dx8152/Qwen-Edit-2509-Multi-Angle-Lighting) LoRA for precise lighting control, paired with [Rapid-AIO-V19](https://huggingface.co/prithivMLmods/Qwen-Image-Edit-Rapid-AIO-V19).")
 
717
  with gr.Row():
718
  with gr.Column(scale=1):
719
  image = gr.Image(label="Input Image", type="pil", height=300)
720
-
721
  gr.Markdown("### 3D Lighting Control")
722
- gr.Markdown("*Drag the handles: 🟡 Azimuth (yellow), 🔵 Elevation (blue)*")
723
-
724
  lighting_3d = LightingControl3D(
725
  value={"azimuth": 0, "elevation": 0},
726
  elem_id="lighting-3d-control"
727
  )
728
  run_btn = gr.Button("Generate Image", variant="primary", size="lg")
729
-
730
  gr.Markdown("### Slider Controls")
731
-
732
  azimuth_slider = gr.Slider(
733
  label="Azimuth (Horizontal Rotation)",
734
  minimum=0,
@@ -737,7 +777,7 @@ with gr.Blocks(css=css) as demo:
737
  value=0,
738
  info="0°=front, 90°=right, 180°=rear, 270°=left"
739
  )
740
-
741
  elevation_slider = gr.Slider(
742
  label="Elevation (Vertical Angle)",
743
  minimum=-90,
@@ -746,6 +786,7 @@ with gr.Blocks(css=css) as demo:
746
  value=0,
747
  info="-90°=from below, 0°=horizontal, 90°=from above"
748
  )
 
749
  with gr.Row():
750
  prompt_preview = gr.Textbox(
751
  label="Generated Prompt",
@@ -753,10 +794,10 @@ with gr.Blocks(css=css) as demo:
753
  interactive=True,
754
  lines=1,
755
  )
756
-
757
  with gr.Column(scale=1):
758
  result = gr.Image(label="Output Image", height=500)
759
-
760
  with gr.Accordion("Advanced Settings", open=False):
761
  seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
762
  randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
@@ -764,23 +805,27 @@ with gr.Blocks(css=css) as demo:
764
  num_inference_steps = gr.Slider(label="Inference Steps", minimum=1, maximum=20, step=1, value=4)
765
  height = gr.Slider(label="Height", minimum=256, maximum=2048, step=8, value=1024)
766
  width = gr.Slider(label="Width", minimum=256, maximum=2048, step=8, value=1024)
767
-
768
  def update_prompt_from_sliders(azimuth, elevation):
 
769
  prompt = build_lighting_prompt(azimuth, elevation)
770
  return prompt
771
-
772
  def sync_3d_to_sliders(lighting_value):
 
773
  if lighting_value and isinstance(lighting_value, dict):
774
  az = lighting_value.get('azimuth', 0)
775
  el = lighting_value.get('elevation', 0)
776
  prompt = build_lighting_prompt(az, el)
777
  return az, el, prompt
778
  return gr.update(), gr.update(), gr.update()
779
-
780
  def sync_sliders_to_3d(azimuth, elevation):
 
781
  return {"azimuth": azimuth, "elevation": elevation}
782
-
783
  def update_3d_image(image):
 
784
  if image is None:
785
  return gr.update(imageUrl=None)
786
  import base64
@@ -790,33 +835,33 @@ with gr.Blocks(css=css) as demo:
790
  img_str = base64.b64encode(buffered.getvalue()).decode()
791
  data_url = f"data:image/png;base64,{img_str}"
792
  return gr.update(imageUrl=data_url)
793
-
794
  for slider in [azimuth_slider, elevation_slider]:
795
  slider.change(
796
  fn=update_prompt_from_sliders,
797
  inputs=[azimuth_slider, elevation_slider],
798
  outputs=[prompt_preview]
799
  )
800
-
801
  lighting_3d.change(
802
  fn=sync_3d_to_sliders,
803
  inputs=[lighting_3d],
804
  outputs=[azimuth_slider, elevation_slider, prompt_preview]
805
  )
806
-
807
  for slider in [azimuth_slider, elevation_slider]:
808
  slider.release(
809
  fn=sync_sliders_to_3d,
810
  inputs=[azimuth_slider, elevation_slider],
811
  outputs=[lighting_3d]
812
  )
813
-
814
  run_btn.click(
815
  fn=infer_lighting_edit,
816
  inputs=[image, azimuth_slider, elevation_slider, seed, randomize_seed, guidance_scale, num_inference_steps, height, width],
817
  outputs=[result, seed, prompt_preview]
818
  )
819
-
820
  image.upload(
821
  fn=update_dimensions_on_upload,
822
  inputs=[image],
@@ -826,12 +871,12 @@ with gr.Blocks(css=css) as demo:
826
  inputs=[image],
827
  outputs=[lighting_3d]
828
  )
829
-
830
  image.clear(
831
  fn=lambda: gr.update(imageUrl=None),
832
  outputs=[lighting_3d]
833
  )
834
-
835
  if __name__ == "__main__":
836
  head = '<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>'
837
  css = '.fillable{max-width: 1200px !important}'
 
9
  from qwenimage.pipeline_qwenimage_edit_plus import QwenImageEditPlusPipeline
10
  from qwenimage.transformer_qwenimage import QwenImageTransformer2DModel
11
  from qwenimage.qwen_fa3_processor import QwenDoubleStreamAttnProcessorFA3
12
+
13
  from gradio.themes import Soft
14
  from gradio.themes.utils import colors, fonts, sizes
15
 
16
+ # --- Theme Configuration ---
17
  colors.orange_red = colors.Color(
18
  name="orange_red",
19
  c50="#FFF0E5",
 
82
 
83
  orange_red_theme = OrangeRedTheme()
84
 
85
+ # --- Global Constants & Model Setup ---
86
  MAX_SEED = np.iinfo(np.int32).max
87
+
88
  dtype = torch.bfloat16
89
  device = "cuda" if torch.cuda.is_available() else "cpu"
 
90
  pipe = QwenImageEditPlusPipeline.from_pretrained(
91
  "Qwen/Qwen-Image-Edit-2511",
92
  transformer=QwenImageTransformer2DModel.from_pretrained(
 
96
  ),
97
  torch_dtype=dtype
98
  ).to(device)
 
99
  try:
100
  pipe.transformer.set_attn_processor(QwenDoubleStreamAttnProcessorFA3())
101
  print("Flash Attention 3 Processor set successfully.")
 
109
  "adapter_name": "multi-angle-lighting"
110
  },
111
  }
 
112
  loaded = False
113
 
114
  AZIMUTH_MAP = {
 
128
  90: "Above"
129
  }
130
 
131
+ # --- Helper Functions ---
132
+
133
  def snap_to_nearest(value, options):
134
+ """Snap a value to the nearest option in a list."""
135
  return min(options, key=lambda x: abs(x - value))
136
 
137
  def build_lighting_prompt(azimuth: float, elevation: float) -> str:
138
+ """
139
+ Build a lighting prompt from azimuth and elevation values.
140
+ """
141
  azimuth_snapped = snap_to_nearest(azimuth, list(AZIMUTH_MAP.keys()))
142
  elevation_snapped = snap_to_nearest(elevation, list(ELEVATION_MAP.keys()))
143
+
144
  if elevation_snapped == 0:
145
  return f"Light source from the {AZIMUTH_MAP[azimuth_snapped]}"
146
  else:
 
159
  width: int = 1024,
160
  ):
161
  global loaded
162
+ progress = gr.Progress(track_tqdm=True)
163
+
164
  if not loaded:
165
  pipe.load_lora_weights(
166
  ADAPTER_SPECS["Multi-Angle-Lighting"]["repo"],
 
169
  )
170
  pipe.set_adapters([ADAPTER_SPECS["Multi-Angle-Lighting"]["adapter_name"]], adapter_weights=[1.0])
171
  loaded = True
172
+
173
  prompt = build_lighting_prompt(azimuth, elevation)
174
  print(f"Generated Prompt: {prompt}")
 
175
  if randomize_seed:
176
  seed = random.randint(0, MAX_SEED)
 
177
  generator = torch.Generator(device=device).manual_seed(seed)
 
178
  if image is None:
179
  raise gr.Error("Please upload an image first.")
 
180
  pil_image = image.convert("RGB") if isinstance(image, Image.Image) else Image.open(image).convert("RGB")
 
181
  result = pipe(
182
  image=[pil_image],
183
  prompt=prompt,
 
188
  guidance_scale=guidance_scale,
189
  num_images_per_prompt=1,
190
  ).images[0]
 
191
  return result, seed, prompt
192
 
193
  def update_dimensions_on_upload(image):
 
206
  new_height = (new_height // 8) * 8
207
  return new_width, new_height
208
 
209
+ # --- Custom 3D Component ---
210
+
211
  class LightingControl3D(gr.HTML):
212
+ """
213
+ A 3D lighting control component using Three.js.
214
+ """
215
  def __init__(self, value=None, imageUrl=None, **kwargs):
216
  if value is None:
217
  value = {"azimuth": 0, "elevation": 0}
218
+
219
  html_template = """
220
  <div id="lighting-control-wrapper" style="width: 100%; height: 450px; position: relative; background: #1a1a1a; border-radius: 12px; overflow: hidden;">
221
  <div id="prompt-overlay" style="position: absolute; bottom: 10px; left: 50%; transform: translateX(-50%); background: rgba(0,0,0,0.8); padding: 8px 16px; border-radius: 8px; font-family: monospace; font-size: 12px; color: #00ff88; white-space: nowrap; z-index: 10;"></div>
222
  </div>
223
  """
224
+
225
  js_on_load = """
226
  (() => {
227
  const wrapper = element.querySelector('#lighting-control-wrapper');
228
  const promptOverlay = element.querySelector('#prompt-overlay');
229
+
230
+ // Wait for THREE to load
231
  const initScene = () => {
232
  if (typeof THREE === 'undefined') {
233
  setTimeout(initScene, 100);
234
  return;
235
  }
236
+
237
+ // Scene setup
238
  const scene = new THREE.Scene();
239
  scene.background = new THREE.Color(0x1a1a1a);
240
+
241
  const camera = new THREE.PerspectiveCamera(50, wrapper.clientWidth / wrapper.clientHeight, 0.1, 1000);
242
+ camera.position.set(4.5, 3, 4.5);
243
+ camera.lookAt(0, 0.75, 0);
244
+
245
  const renderer = new THREE.WebGLRenderer({ antialias: true });
246
  renderer.setSize(wrapper.clientWidth, wrapper.clientHeight);
247
  renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
248
  renderer.shadowMap.enabled = true;
249
  renderer.shadowMap.type = THREE.PCFSoftShadowMap;
250
  wrapper.insertBefore(renderer.domElement, promptOverlay);
251
+
252
+ // Lighting (Ambient)
253
+ scene.add(new THREE.AmbientLight(0xffffff, 0.1));
254
+
255
+ // Ground plane for shadows
256
  const ground = new THREE.Mesh(
257
+ new THREE.PlaneGeometry(10, 10),
258
+ new THREE.ShadowMaterial({ opacity: 0.3 })
259
  );
260
  ground.rotation.x = -Math.PI / 2;
261
+ ground.position.y = 0;
262
  ground.receiveShadow = true;
263
  scene.add(ground);
264
+
265
+ // Grid
266
+ scene.add(new THREE.GridHelper(8, 16, 0x333333, 0x222222));
267
+
268
+ // Constants
269
+ const CENTER = new THREE.Vector3(0, 0.75, 0);
270
+ const BASE_DISTANCE = 2.5;
271
+ const AZIMUTH_RADIUS = 2.4;
272
+ const ELEVATION_RADIUS = 1.8;
273
+
274
+ // State
275
  let azimuthAngle = props.value?.azimuth || 0;
276
  let elevationAngle = props.value?.elevation || 0;
277
+
278
+ // Mappings
279
  const azimuthSteps = [0, 45, 90, 135, 180, 225, 270, 315];
280
  const elevationSteps = [-90, 0, 90];
281
  const azimuthNames = {
 
284
  270: 'Left', 315: 'Left Front'
285
  };
286
  const elevationNames = { '-90': 'Below', '0': '', '90': 'Above' };
287
+
288
  function snapToNearest(value, steps) {
289
  return steps.reduce((prev, curr) => Math.abs(curr - value) < Math.abs(prev - value) ? curr : prev);
290
  }
291
+
292
+ // Create placeholder texture (smiley face)
293
  function createPlaceholderTexture() {
294
  const canvas = document.createElement('canvas');
295
  canvas.width = 256;
 
313
  ctx.stroke();
314
  return new THREE.CanvasTexture(canvas);
315
  }
316
+
317
+ // Target image plane
318
  let currentTexture = createPlaceholderTexture();
319
+ const planeMaterial = new THREE.MeshStandardMaterial({ map: currentTexture, side: THREE.DoubleSide, roughness: 0.5, metalness: 0 });
320
+ let targetPlane = new THREE.Mesh(new THREE.PlaneGeometry(1.2, 1.2), planeMaterial);
 
 
 
 
 
321
  targetPlane.position.copy(CENTER);
322
  targetPlane.receiveShadow = true;
323
  scene.add(targetPlane);
324
+
325
+ // Function to update texture from image URL
326
  function updateTextureFromUrl(url) {
327
  if (!url) {
328
  planeMaterial.map = createPlaceholderTexture();
329
  planeMaterial.needsUpdate = true;
330
+ scene.remove(targetPlane);
331
+ targetPlane = new THREE.Mesh(new THREE.PlaneGeometry(1.2, 1.2), planeMaterial);
332
+ targetPlane.position.copy(CENTER);
333
+ targetPlane.receiveShadow = true;
334
+ scene.add(targetPlane);
335
  return;
336
  }
337
+
338
  const loader = new THREE.TextureLoader();
339
  loader.crossOrigin = 'anonymous';
340
  loader.load(url, (texture) => {
 
342
  texture.magFilter = THREE.LinearFilter;
343
  planeMaterial.map = texture;
344
  planeMaterial.needsUpdate = true;
345
+
346
  const img = texture.image;
347
  if (img && img.width && img.height) {
348
  const aspect = img.width / img.height;
349
+ const maxSize = 1.5;
350
+ let planeWidth, planeHeight;
351
+ if (aspect > 1) {
352
+ planeWidth = maxSize;
353
+ planeHeight = maxSize / aspect;
354
+ } else {
355
  planeHeight = maxSize;
356
  planeWidth = maxSize * aspect;
357
  }
 
364
  targetPlane.receiveShadow = true;
365
  scene.add(targetPlane);
366
  }
367
+ }, undefined, (err) => {
368
+ console.error('Failed to load texture:', err);
369
  });
370
  }
371
+
372
  if (props.imageUrl) {
373
  updateTextureFromUrl(props.imageUrl);
374
  }
375
+
376
+ // --- LIGHT MODEL REDESIGN (Softbox) ---
377
+ const lightGroup = new THREE.Group();
378
+
379
+ // Box Geometry for Softbox
380
+ const sbGeometry = new THREE.BoxGeometry(0.6, 0.6, 0.3);
381
+
382
+ // Materials: Red body, White light face
383
+ const sbMatRed = new THREE.MeshStandardMaterial({ color: 0xff0000, roughness: 0.5 });
384
+ const sbMatWhite = new THREE.MeshStandardMaterial({ color: 0xffffff, emissive: 0xffffff, emissiveIntensity: 2 });
385
+
386
+ // In Three.js, Face 4 is +Z. When we lookAt(), +Z points to target.
387
+ // 0:+x, 1:-x, 2:+y, 3:-y, 4:+z, 5:-z
388
+ const sbMaterials = [
389
+ sbMatRed, // Right
390
+ sbMatRed, // Left
391
+ sbMatRed, // Top
392
+ sbMatRed, // Bottom
393
+ sbMatWhite, // Front (Light Face)
394
+ sbMatRed // Back
395
+ ];
396
+
397
+ const softbox = new THREE.Mesh(sbGeometry, sbMaterials);
398
+ lightGroup.add(softbox);
399
+
400
+ // Actual SpotLight source
401
+ const spotLight = new THREE.SpotLight(0xffffff, 10, 10, Math.PI / 3, 1, 1);
402
+ spotLight.position.set(0, 0, 0.1); // Slightly in front of the box center
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
403
  spotLight.castShadow = true;
404
+ spotLight.shadow.mapSize.width = 1024;
405
+ spotLight.shadow.mapSize.height = 1024;
406
+ spotLight.shadow.camera.near = 0.5;
407
+ spotLight.shadow.camera.far = 500;
408
+ spotLight.shadow.bias = -0.005;
409
+ lightGroup.add(spotLight);
410
+
411
  const lightTarget = new THREE.Object3D();
412
  lightTarget.position.copy(CENTER);
413
  scene.add(lightTarget);
414
  spotLight.target = lightTarget;
415
+
416
+ scene.add(lightGroup);
417
+
418
+ // --- HANDLES REDESIGN ---
419
+
420
+ // YELLOW: Azimuth ring
421
  const azimuthRing = new THREE.Mesh(
422
+ new THREE.TorusGeometry(AZIMUTH_RADIUS, 0.04, 16, 64),
423
+ new THREE.MeshStandardMaterial({ color: 0xffff00, emissive: 0xffff00, emissiveIntensity: 0.3 })
424
  );
425
  azimuthRing.rotation.x = Math.PI / 2;
426
  azimuthRing.position.y = 0.05;
427
  scene.add(azimuthRing);
428
+
429
  const azimuthHandle = new THREE.Mesh(
430
+ new THREE.SphereGeometry(0.18, 16, 16),
431
+ new THREE.MeshStandardMaterial({ color: 0xffff00, emissive: 0xffff00, emissiveIntensity: 0.5 })
432
  );
433
  azimuthHandle.userData.type = 'azimuth';
434
  scene.add(azimuthHandle);
435
+
436
+ // BLUE: Elevation arc
437
+ const arcPoints = [];
438
+ for (let i = 0; i <= 32; i++) {
439
+ const angle = THREE.MathUtils.degToRad(-90 + (180 * i / 32));
440
+ arcPoints.push(new THREE.Vector3(-0.8, ELEVATION_RADIUS * Math.sin(angle) + CENTER.y, ELEVATION_RADIUS * Math.cos(angle)));
441
+ }
442
+ const arcCurve = new THREE.CatmullRomCurve3(arcPoints);
443
  const elevationArc = new THREE.Mesh(
444
+ new THREE.TubeGeometry(arcCurve, 32, 0.04, 8, false),
445
+ new THREE.MeshStandardMaterial({ color: 0x0000ff, emissive: 0x0000ff, emissiveIntensity: 0.3 })
446
  );
 
 
447
  scene.add(elevationArc);
448
+
449
  const elevationHandle = new THREE.Mesh(
450
+ new THREE.SphereGeometry(0.18, 16, 16),
451
+ new THREE.MeshStandardMaterial({ color: 0x0000ff, emissive: 0x0000ff, emissiveIntensity: 0.5 })
452
  );
453
  elevationHandle.userData.type = 'elevation';
454
  scene.add(elevationHandle);
455
+
456
+ // --- RESET BUTTON REDESIGN ---
457
+ const refreshBtn = document.createElement('button');
458
+ refreshBtn.innerHTML = 'Reset View';
459
+ refreshBtn.style.position = 'absolute';
460
+ refreshBtn.style.top = '15px';
461
+ refreshBtn.style.right = '15px';
462
+ refreshBtn.style.background = 'rgba(255, 255, 255, 0.9)';
463
+ refreshBtn.style.border = 'none';
464
+ refreshBtn.style.borderRadius = '20px'; // Pill shape
465
+ refreshBtn.style.padding = '8px 16px';
466
+ refreshBtn.style.fontFamily = 'sans-serif';
467
+ refreshBtn.style.fontWeight = 'bold';
468
+ refreshBtn.style.fontSize = '12px';
469
+ refreshBtn.style.color = '#333';
470
+ refreshBtn.style.cursor = 'pointer';
471
+ refreshBtn.style.zIndex = '20';
472
+ refreshBtn.style.boxShadow = '0 2px 5px rgba(0,0,0,0.3)';
473
+ refreshBtn.style.transition = 'background 0.2s';
474
+
475
+ refreshBtn.onmouseover = () => { refreshBtn.style.background = '#ffffff'; };
476
+ refreshBtn.onmouseout = () => { refreshBtn.style.background = 'rgba(255, 255, 255, 0.9)'; };
477
+
478
+ wrapper.appendChild(refreshBtn);
479
+
480
+ refreshBtn.addEventListener('click', () => {
481
  azimuthAngle = 0;
482
  elevationAngle = 0;
483
  updatePositions();
484
  updatePropsAndTrigger();
485
  });
486
+
487
  function updatePositions() {
488
+ const distance = BASE_DISTANCE;
489
  const azRad = THREE.MathUtils.degToRad(azimuthAngle);
490
  const elRad = THREE.MathUtils.degToRad(elevationAngle);
491
+
492
+ const lightX = distance * Math.sin(azRad) * Math.cos(elRad);
493
+ const lightY = distance * Math.sin(elRad) + CENTER.y;
494
+ const lightZ = distance * Math.cos(azRad) * Math.cos(elRad);
495
+
496
+ lightGroup.position.set(lightX, lightY, lightZ);
497
+ lightGroup.lookAt(CENTER);
498
+
499
+ azimuthHandle.position.set(AZIMUTH_RADIUS * Math.sin(azRad), 0.05, AZIMUTH_RADIUS * Math.cos(azRad));
500
+ elevationHandle.position.set(-0.8, ELEVATION_RADIUS * Math.sin(elRad) + CENTER.y, ELEVATION_RADIUS * Math.cos(elRad));
501
+
502
+ // Update prompt
503
  const azSnap = snapToNearest(azimuthAngle, azimuthSteps);
504
  const elSnap = snapToNearest(elevationAngle, elevationSteps);
505
+ let prompt = 'Light source from';
506
  if (elSnap !== 0) {
507
+ prompt += ' ' + elevationNames[String(elSnap)];
508
  } else {
509
+ prompt += ' the ' + azimuthNames[azSnap];
510
  }
511
+ promptOverlay.textContent = prompt;
512
  }
513
+
514
  function updatePropsAndTrigger() {
515
  const azSnap = snapToNearest(azimuthAngle, azimuthSteps);
516
  const elSnap = snapToNearest(elevationAngle, elevationSteps);
517
+
518
  props.value = { azimuth: azSnap, elevation: elSnap };
519
  trigger('change', props.value);
520
  }
521
+
522
+ // Raycasting
523
  const raycaster = new THREE.Raycaster();
524
  const mouse = new THREE.Vector2();
525
  let isDragging = false;
526
  let dragTarget = null;
527
+ let dragStartMouse = new THREE.Vector2();
528
+ const intersection = new THREE.Vector3();
529
+
530
  const canvas = renderer.domElement;
531
+
532
  canvas.addEventListener('mousedown', (e) => {
533
  const rect = canvas.getBoundingClientRect();
534
  mouse.x = ((e.clientX - rect.left) / rect.width) * 2 - 1;
535
  mouse.y = -((e.clientY - rect.top) / rect.height) * 2 + 1;
536
+
537
  raycaster.setFromCamera(mouse, camera);
538
  const intersects = raycaster.intersectObjects([azimuthHandle, elevationHandle]);
539
+
540
  if (intersects.length > 0) {
541
  isDragging = true;
542
  dragTarget = intersects[0].object;
543
+ dragTarget.material.emissiveIntensity = 1.0;
544
+ dragTarget.scale.setScalar(1.3);
545
+ dragStartMouse.copy(mouse);
546
  canvas.style.cursor = 'grabbing';
547
  }
548
  });
549
+
550
  canvas.addEventListener('mousemove', (e) => {
551
  const rect = canvas.getBoundingClientRect();
552
  mouse.x = ((e.clientX - rect.left) / rect.width) * 2 - 1;
553
  mouse.y = -((e.clientY - rect.top) / rect.height) * 2 + 1;
554
+
555
  if (isDragging && dragTarget) {
556
  raycaster.setFromCamera(mouse, camera);
557
+
558
  if (dragTarget.userData.type === 'azimuth') {
559
  const plane = new THREE.Plane(new THREE.Vector3(0, 1, 0), -0.05);
560
+ if (raycaster.ray.intersectPlane(plane, intersection)) {
561
+ azimuthAngle = THREE.MathUtils.radToDeg(Math.atan2(intersection.x, intersection.z));
 
 
562
  if (azimuthAngle < 0) azimuthAngle += 360;
563
  }
564
  } else if (dragTarget.userData.type === 'elevation') {
565
+ const plane = new THREE.Plane(new THREE.Vector3(1, 0, 0), -0.8);
566
+ if (raycaster.ray.intersectPlane(plane, intersection)) {
567
+ const relY = intersection.y - CENTER.y;
568
+ const relZ = intersection.z;
 
 
569
  elevationAngle = THREE.MathUtils.clamp(THREE.MathUtils.radToDeg(Math.atan2(relY, relZ)), -90, 90);
570
  }
571
  }
 
574
  raycaster.setFromCamera(mouse, camera);
575
  const intersects = raycaster.intersectObjects([azimuthHandle, elevationHandle]);
576
  [azimuthHandle, elevationHandle].forEach(h => {
577
+ h.material.emissiveIntensity = 0.5;
578
  h.scale.setScalar(1);
579
  });
580
  if (intersects.length > 0) {
581
+ intersects[0].object.material.emissiveIntensity = 0.8;
582
+ intersects[0].object.scale.setScalar(1.1);
583
  canvas.style.cursor = 'grab';
584
  } else {
585
  canvas.style.cursor = 'default';
586
  }
587
  }
588
  });
589
+
590
  const onMouseUp = () => {
591
  if (dragTarget) {
592
+ dragTarget.material.emissiveIntensity = 0.5;
593
  dragTarget.scale.setScalar(1);
594
+
595
+ // Snap and animate
596
  const targetAz = snapToNearest(azimuthAngle, azimuthSteps);
597
  const targetEl = snapToNearest(elevationAngle, elevationSteps);
598
+
599
  const startAz = azimuthAngle, startEl = elevationAngle;
600
  const startTime = Date.now();
601
+
602
  function animateSnap() {
603
  const t = Math.min((Date.now() - startTime) / 200, 1);
604
  const ease = 1 - Math.pow(1 - t, 3);
605
+
606
  let azDiff = targetAz - startAz;
607
  if (azDiff > 180) azDiff -= 360;
608
  if (azDiff < -180) azDiff += 360;
609
  azimuthAngle = startAz + azDiff * ease;
610
  if (azimuthAngle < 0) azimuthAngle += 360;
611
  if (azimuthAngle >= 360) azimuthAngle -= 360;
612
+
613
  elevationAngle = startEl + (targetEl - startEl) * ease;
614
+
615
  updatePositions();
616
  if (t < 1) requestAnimationFrame(animateSnap);
617
  else updatePropsAndTrigger();
 
622
  dragTarget = null;
623
  canvas.style.cursor = 'default';
624
  };
625
+
626
  canvas.addEventListener('mouseup', onMouseUp);
627
  canvas.addEventListener('mouseleave', onMouseUp);
628
+ // Touch support
 
629
  canvas.addEventListener('touchstart', (e) => {
630
  e.preventDefault();
631
  const touch = e.touches[0];
632
  const rect = canvas.getBoundingClientRect();
633
  mouse.x = ((touch.clientX - rect.left) / rect.width) * 2 - 1;
634
  mouse.y = -((touch.clientY - rect.top) / rect.height) * 2 + 1;
635
+
636
  raycaster.setFromCamera(mouse, camera);
637
  const intersects = raycaster.intersectObjects([azimuthHandle, elevationHandle]);
638
+
639
  if (intersects.length > 0) {
640
  isDragging = true;
641
  dragTarget = intersects[0].object;
642
+ dragTarget.material.emissiveIntensity = 1.0;
643
+ dragTarget.scale.setScalar(1.3);
644
+ dragStartMouse.copy(mouse);
645
  }
646
  }, { passive: false });
647
+
648
  canvas.addEventListener('touchmove', (e) => {
649
  e.preventDefault();
650
  const touch = e.touches[0];
651
  const rect = canvas.getBoundingClientRect();
652
  mouse.x = ((touch.clientX - rect.left) / rect.width) * 2 - 1;
653
  mouse.y = -((touch.clientY - rect.top) / rect.height) * 2 + 1;
654
+
655
  if (isDragging && dragTarget) {
656
  raycaster.setFromCamera(mouse, camera);
 
657
  if (dragTarget.userData.type === 'azimuth') {
658
  const plane = new THREE.Plane(new THREE.Vector3(0, 1, 0), -0.05);
659
+ if (raycaster.ray.intersectPlane(plane, intersection)) {
660
+ azimuthAngle = THREE.MathUtils.radToDeg(Math.atan2(intersection.x, intersection.z));
 
661
  if (azimuthAngle < 0) azimuthAngle += 360;
662
  }
663
  } else if (dragTarget.userData.type === 'elevation') {
664
+ const plane = new THREE.Plane(new THREE.Vector3(1, 0, 0), -0.8);
665
+ if (raycaster.ray.intersectPlane(plane, intersection)) {
666
+ const relY = intersection.y - CENTER.y;
667
+ const relZ = intersection.z;
 
668
  elevationAngle = THREE.MathUtils.clamp(THREE.MathUtils.radToDeg(Math.atan2(relY, relZ)), -90, 90);
669
  }
670
  }
671
  updatePositions();
672
  }
673
  }, { passive: false });
674
+
675
+ canvas.addEventListener('touchend', (e) => {
676
+ e.preventDefault();
677
+ onMouseUp();
678
+ }, { passive: false });
679
+ canvas.addEventListener('touchcancel', (e) => {
680
+ e.preventDefault();
681
+ onMouseUp();
682
+ }, { passive: false });
683
+
684
+ // Initial update
685
  updatePositions();
686
+
687
+ // Render loop
688
  function render() {
689
  requestAnimationFrame(render);
690
  renderer.render(scene, camera);
691
  }
692
  render();
693
+
694
+ // Handle resize
695
  new ResizeObserver(() => {
696
  camera.aspect = wrapper.clientWidth / wrapper.clientHeight;
697
  camera.updateProjectionMatrix();
698
  renderer.setSize(wrapper.clientWidth, wrapper.clientHeight);
699
  }).observe(wrapper);
700
+
701
+ // Store update functions for external calls
702
  wrapper._updateFromProps = (newVal) => {
703
  if (newVal && typeof newVal === 'object') {
704
  azimuthAngle = newVal.azimuth ?? azimuthAngle;
 
706
  updatePositions();
707
  }
708
  };
709
+
710
  wrapper._updateTexture = updateTextureFromUrl;
711
+
712
+ // Watch for prop changes
713
  let lastImageUrl = props.imageUrl;
714
  let lastValue = JSON.stringify(props.value);
715
  setInterval(() => {
 
728
  }
729
  }, 100);
730
  };
731
+
732
  initScene();
733
  })();
734
  """
735
+
736
  super().__init__(
737
  value=value,
738
  html_template=html_template,
 
741
  **kwargs
742
  )
743
 
744
+ # --- Gradio UI Layout ---
745
+
746
  css = '''
747
  #col-container { max-width: 1200px; margin: 0 auto; }
748
  .dark .progress-text { color: white !important; }
 
750
  .slider-row { display: flex; gap: 10px; align-items: center; }
751
  #main-title h1 {font-size: 2.4em !important;}
752
  '''
 
753
  with gr.Blocks(css=css) as demo:
754
  gr.Markdown("# **Qwen-Image-Edit-2511-3D-Lighting-Control**", elem_id="main-title")
755
  gr.Markdown("Control lighting directions using the **3D viewport** or **sliders**. Use [Multi-Angle-Lighting](https://huggingface.co/dx8152/Qwen-Edit-2509-Multi-Angle-Lighting) LoRA for precise lighting control, paired with [Rapid-AIO-V19](https://huggingface.co/prithivMLmods/Qwen-Image-Edit-Rapid-AIO-V19).")
756
+
757
  with gr.Row():
758
  with gr.Column(scale=1):
759
  image = gr.Image(label="Input Image", type="pil", height=300)
760
+
761
  gr.Markdown("### 3D Lighting Control")
762
+ gr.Markdown("*Drag the colored handles: 🟡 Azimuth (Direction), 🔵 Elevation (Height)*")
763
+
764
  lighting_3d = LightingControl3D(
765
  value={"azimuth": 0, "elevation": 0},
766
  elem_id="lighting-3d-control"
767
  )
768
  run_btn = gr.Button("Generate Image", variant="primary", size="lg")
769
+
770
  gr.Markdown("### Slider Controls")
771
+
772
  azimuth_slider = gr.Slider(
773
  label="Azimuth (Horizontal Rotation)",
774
  minimum=0,
 
777
  value=0,
778
  info="0°=front, 90°=right, 180°=rear, 270°=left"
779
  )
780
+
781
  elevation_slider = gr.Slider(
782
  label="Elevation (Vertical Angle)",
783
  minimum=-90,
 
786
  value=0,
787
  info="-90°=from below, 0°=horizontal, 90°=from above"
788
  )
789
+
790
  with gr.Row():
791
  prompt_preview = gr.Textbox(
792
  label="Generated Prompt",
 
794
  interactive=True,
795
  lines=1,
796
  )
797
+
798
  with gr.Column(scale=1):
799
  result = gr.Image(label="Output Image", height=500)
800
+
801
  with gr.Accordion("Advanced Settings", open=False):
802
  seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
803
  randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
 
805
  num_inference_steps = gr.Slider(label="Inference Steps", minimum=1, maximum=20, step=1, value=4)
806
  height = gr.Slider(label="Height", minimum=256, maximum=2048, step=8, value=1024)
807
  width = gr.Slider(label="Width", minimum=256, maximum=2048, step=8, value=1024)
808
+
809
  def update_prompt_from_sliders(azimuth, elevation):
810
+ """Update prompt preview when sliders change."""
811
  prompt = build_lighting_prompt(azimuth, elevation)
812
  return prompt
813
+
814
  def sync_3d_to_sliders(lighting_value):
815
+ """Sync 3D control changes to sliders."""
816
  if lighting_value and isinstance(lighting_value, dict):
817
  az = lighting_value.get('azimuth', 0)
818
  el = lighting_value.get('elevation', 0)
819
  prompt = build_lighting_prompt(az, el)
820
  return az, el, prompt
821
  return gr.update(), gr.update(), gr.update()
822
+
823
  def sync_sliders_to_3d(azimuth, elevation):
824
+ """Sync slider changes to 3D control."""
825
  return {"azimuth": azimuth, "elevation": elevation}
826
+
827
  def update_3d_image(image):
828
+ """Update the 3D component with the uploaded image."""
829
  if image is None:
830
  return gr.update(imageUrl=None)
831
  import base64
 
835
  img_str = base64.b64encode(buffered.getvalue()).decode()
836
  data_url = f"data:image/png;base64,{img_str}"
837
  return gr.update(imageUrl=data_url)
838
+
839
  for slider in [azimuth_slider, elevation_slider]:
840
  slider.change(
841
  fn=update_prompt_from_sliders,
842
  inputs=[azimuth_slider, elevation_slider],
843
  outputs=[prompt_preview]
844
  )
845
+
846
  lighting_3d.change(
847
  fn=sync_3d_to_sliders,
848
  inputs=[lighting_3d],
849
  outputs=[azimuth_slider, elevation_slider, prompt_preview]
850
  )
851
+
852
  for slider in [azimuth_slider, elevation_slider]:
853
  slider.release(
854
  fn=sync_sliders_to_3d,
855
  inputs=[azimuth_slider, elevation_slider],
856
  outputs=[lighting_3d]
857
  )
858
+
859
  run_btn.click(
860
  fn=infer_lighting_edit,
861
  inputs=[image, azimuth_slider, elevation_slider, seed, randomize_seed, guidance_scale, num_inference_steps, height, width],
862
  outputs=[result, seed, prompt_preview]
863
  )
864
+
865
  image.upload(
866
  fn=update_dimensions_on_upload,
867
  inputs=[image],
 
871
  inputs=[image],
872
  outputs=[lighting_3d]
873
  )
874
+
875
  image.clear(
876
  fn=lambda: gr.update(imageUrl=None),
877
  outputs=[lighting_3d]
878
  )
879
+
880
  if __name__ == "__main__":
881
  head = '<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>'
882
  css = '.fillable{max-width: 1200px !important}'