multimodalart HF Staff commited on
Commit
c655dc4
·
verified ·
1 Parent(s): 064cdcd

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +679 -0
app.py ADDED
@@ -0,0 +1,679 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import random
4
+ import torch
5
+ import spaces
6
+
7
+ from PIL import Image
8
+ from diffusers import FlowMatchEulerDiscreteScheduler, QwenImageEditPlusPipeline
9
+
10
+
11
+ MAX_SEED = np.iinfo(np.int32).max
12
+
13
+ # --- Model Loading ---
14
+ dtype = torch.bfloat16
15
+ device = "cuda" if torch.cuda.is_available() else "cpu"
16
+
17
+ pipe = QwenImageEditPlusPipeline.from_pretrained(
18
+ "Qwen/Qwen-Image-Edit-2511",
19
+ torch_dtype=dtype
20
+ ).to(device)
21
+
22
+ # Load the lightning LoRA for fast inference
23
+ pipe.load_lora_weights(
24
+ "lightx2v/Qwen-Image-Edit-2511-Lightning",
25
+ weight_name="Qwen-Image-Edit-2511-Lightning-4steps-V1.0-bf16.safetensors",
26
+ adapter_name="lightning"
27
+ )
28
+
29
+ # Load the multi-angles LoRA
30
+ pipe.load_lora_weights(
31
+ "fal/Qwen-Image-Edit-2511-Multiple-Angles-LoRA",
32
+ weight_name="qwen-image-edit-2511-multiple-angles-lora.safetensors",
33
+ adapter_name="angles"
34
+ )
35
+
36
+ pipe.set_adapters(["lightning", "angles"], adapter_weights=[1.0, 1.0])
37
+
38
+ # --- Prompt Building ---
39
+
40
+ # Azimuth mappings (8 positions)
41
+ AZIMUTH_MAP = {
42
+ 0: "front view",
43
+ 45: "front-right quarter view",
44
+ 90: "right side view",
45
+ 135: "back-right quarter view",
46
+ 180: "back view",
47
+ 225: "back-left quarter view",
48
+ 270: "left side view",
49
+ 315: "front-left quarter view"
50
+ }
51
+
52
+ # Elevation mappings (4 positions)
53
+ ELEVATION_MAP = {
54
+ -30: "low-angle shot",
55
+ 0: "eye-level shot",
56
+ 30: "elevated shot",
57
+ 60: "high-angle shot"
58
+ }
59
+
60
+ # Distance mappings (3 positions)
61
+ DISTANCE_MAP = {
62
+ 0.6: "close-up",
63
+ 1.0: "medium shot",
64
+ 1.8: "wide shot"
65
+ }
66
+
67
+
68
+ def snap_to_nearest(value, options):
69
+ """Snap a value to the nearest option in a list."""
70
+ return min(options, key=lambda x: abs(x - value))
71
+
72
+
73
+ def build_camera_prompt(azimuth: float, elevation: float, distance: float) -> str:
74
+ """
75
+ Build a camera prompt from azimuth, elevation, and distance values.
76
+
77
+ Args:
78
+ azimuth: Horizontal rotation in degrees (0-360)
79
+ elevation: Vertical angle in degrees (-30 to 60)
80
+ distance: Distance factor (0.6 to 1.8)
81
+
82
+ Returns:
83
+ Formatted prompt string for the LoRA
84
+ """
85
+ # Snap to nearest valid values
86
+ azimuth_snapped = snap_to_nearest(azimuth, list(AZIMUTH_MAP.keys()))
87
+ elevation_snapped = snap_to_nearest(elevation, list(ELEVATION_MAP.keys()))
88
+ distance_snapped = snap_to_nearest(distance, list(DISTANCE_MAP.keys()))
89
+
90
+ azimuth_name = AZIMUTH_MAP[azimuth_snapped]
91
+ elevation_name = ELEVATION_MAP[elevation_snapped]
92
+ distance_name = DISTANCE_MAP[distance_snapped]
93
+
94
+ return f"<sks> {azimuth_name} {elevation_name} {distance_name}"
95
+
96
+
97
+ @spaces.GPU
98
+ def infer_camera_edit(
99
+ image: Image.Image,
100
+ azimuth: float = 0.0,
101
+ elevation: float = 0.0,
102
+ distance: float = 1.0,
103
+ seed: int = 0,
104
+ randomize_seed: bool = True,
105
+ guidance_scale: float = 1.0,
106
+ num_inference_steps: int = 4,
107
+ height: int = 1024,
108
+ width: int = 1024,
109
+ ):
110
+ """
111
+ Edit the camera angle of an image using Qwen Image Edit 2511 with multi-angles LoRA.
112
+ """
113
+ progress = gr.Progress(track_tqdm=True)
114
+
115
+ prompt = build_camera_prompt(azimuth, elevation, distance)
116
+ print(f"Generated Prompt: {prompt}")
117
+
118
+ if randomize_seed:
119
+ seed = random.randint(0, MAX_SEED)
120
+ generator = torch.Generator(device=device).manual_seed(seed)
121
+
122
+ if image is None:
123
+ raise gr.Error("Please upload an image first.")
124
+
125
+ pil_image = image.convert("RGB") if isinstance(image, Image.Image) else Image.open(image).convert("RGB")
126
+
127
+ result = pipe(
128
+ image=[pil_image],
129
+ prompt=prompt,
130
+ height=height if height != 0 else None,
131
+ width=width if width != 0 else None,
132
+ num_inference_steps=num_inference_steps,
133
+ generator=generator,
134
+ guidance_scale=guidance_scale,
135
+ num_images_per_prompt=1,
136
+ ).images[0]
137
+
138
+ return result, seed, prompt
139
+
140
+
141
+ def update_dimensions_on_upload(image):
142
+ """Compute recommended dimensions preserving aspect ratio."""
143
+ if image is None:
144
+ return 1024, 1024
145
+
146
+ original_width, original_height = image.size
147
+
148
+ if original_width > original_height:
149
+ new_width = 1024
150
+ aspect_ratio = original_height / original_width
151
+ new_height = int(new_width * aspect_ratio)
152
+ else:
153
+ new_height = 1024
154
+ aspect_ratio = original_width / original_height
155
+ new_width = int(new_height * aspect_ratio)
156
+
157
+ new_width = (new_width // 8) * 8
158
+ new_height = (new_height // 8) * 8
159
+
160
+ return new_width, new_height
161
+
162
+
163
+ # --- 3D Camera Control Component ---
164
+ class CameraControl3D(gr.HTML):
165
+ """
166
+ A 3D camera control component using Three.js.
167
+ Outputs: { azimuth: number, elevation: number, distance: number }
168
+ """
169
+ def __init__(self, value=None, **kwargs):
170
+ if value is None:
171
+ value = {"azimuth": 0, "elevation": 0, "distance": 1.0}
172
+
173
+ html_template = """
174
+ <div id="camera-control-wrapper" style="width: 100%; height: 450px; position: relative; background: #1a1a1a; border-radius: 12px; overflow: hidden;">
175
+ <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>
176
+ </div>
177
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
178
+ """
179
+
180
+ js_on_load = """
181
+ (() => {
182
+ const wrapper = element.querySelector('#camera-control-wrapper');
183
+ const promptOverlay = element.querySelector('#prompt-overlay');
184
+
185
+ // Wait for THREE to load
186
+ const initScene = () => {
187
+ if (typeof THREE === 'undefined') {
188
+ setTimeout(initScene, 100);
189
+ return;
190
+ }
191
+
192
+ // Scene setup
193
+ const scene = new THREE.Scene();
194
+ scene.background = new THREE.Color(0x1a1a1a);
195
+
196
+ const camera = new THREE.PerspectiveCamera(50, wrapper.clientWidth / wrapper.clientHeight, 0.1, 1000);
197
+ camera.position.set(6, 4, 6);
198
+ camera.lookAt(0, 0.75, 0);
199
+
200
+ const renderer = new THREE.WebGLRenderer({ antialias: true });
201
+ renderer.setSize(wrapper.clientWidth, wrapper.clientHeight);
202
+ renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
203
+ wrapper.insertBefore(renderer.domElement, promptOverlay);
204
+
205
+ // Lighting
206
+ scene.add(new THREE.AmbientLight(0xffffff, 0.4));
207
+ const dirLight = new THREE.DirectionalLight(0xffffff, 0.8);
208
+ dirLight.position.set(5, 10, 5);
209
+ scene.add(dirLight);
210
+
211
+ // Grid
212
+ scene.add(new THREE.GridHelper(8, 16, 0x333333, 0x222222));
213
+
214
+ // Constants
215
+ const CENTER = new THREE.Vector3(0, 0.75, 0);
216
+ const BASE_DISTANCE = 2.5;
217
+ const AZIMUTH_RADIUS = 2.8;
218
+ const ELEVATION_RADIUS = 2.0;
219
+
220
+ // State
221
+ let azimuthAngle = props.value?.azimuth || 0;
222
+ let elevationAngle = props.value?.elevation || 0;
223
+ let distanceFactor = props.value?.distance || 1.0;
224
+
225
+ // Mappings
226
+ const azimuthSteps = [0, 45, 90, 135, 180, 225, 270, 315];
227
+ const elevationSteps = [-30, 0, 30, 60];
228
+ const distanceSteps = [0.6, 1.0, 1.8];
229
+
230
+ const azimuthNames = {
231
+ 0: 'front view', 45: 'front-right quarter view', 90: 'right side view',
232
+ 135: 'back-right quarter view', 180: 'back view', 225: 'back-left quarter view',
233
+ 270: 'left side view', 315: 'front-left quarter view'
234
+ };
235
+ const elevationNames = { '-30': 'low-angle shot', '0': 'eye-level shot', '30': 'elevated shot', '60': 'high-angle shot' };
236
+ const distanceNames = { '0.6': 'close-up', '1': 'medium shot', '1.8': 'wide shot' };
237
+
238
+ function snapToNearest(value, steps) {
239
+ return steps.reduce((prev, curr) => Math.abs(curr - value) < Math.abs(prev - value) ? curr : prev);
240
+ }
241
+
242
+ // Target image plane
243
+ const imgCanvas = document.createElement('canvas');
244
+ imgCanvas.width = 256;
245
+ imgCanvas.height = 256;
246
+ const ctx = imgCanvas.getContext('2d');
247
+ ctx.fillStyle = '#3a3a4a';
248
+ ctx.fillRect(0, 0, 256, 256);
249
+ ctx.fillStyle = '#ffcc99';
250
+ ctx.beginPath();
251
+ ctx.arc(128, 128, 80, 0, Math.PI * 2);
252
+ ctx.fill();
253
+ ctx.fillStyle = '#333';
254
+ ctx.beginPath();
255
+ ctx.arc(100, 110, 10, 0, Math.PI * 2);
256
+ ctx.arc(156, 110, 10, 0, Math.PI * 2);
257
+ ctx.fill();
258
+ ctx.strokeStyle = '#333';
259
+ ctx.lineWidth = 3;
260
+ ctx.beginPath();
261
+ ctx.arc(128, 130, 35, 0.2, Math.PI - 0.2);
262
+ ctx.stroke();
263
+
264
+ const texture = new THREE.CanvasTexture(imgCanvas);
265
+ const targetPlane = new THREE.Mesh(
266
+ new THREE.PlaneGeometry(1.2, 1.2),
267
+ new THREE.MeshBasicMaterial({ map: texture, side: THREE.DoubleSide })
268
+ );
269
+ targetPlane.position.copy(CENTER);
270
+ scene.add(targetPlane);
271
+
272
+ // Camera model
273
+ const cameraGroup = new THREE.Group();
274
+ const bodyMat = new THREE.MeshStandardMaterial({ color: 0x333333, metalness: 0.8, roughness: 0.2 });
275
+ const body = new THREE.Mesh(new THREE.BoxGeometry(0.3, 0.22, 0.38), bodyMat);
276
+ cameraGroup.add(body);
277
+ const lens = new THREE.Mesh(
278
+ new THREE.CylinderGeometry(0.09, 0.11, 0.18, 16),
279
+ new THREE.MeshStandardMaterial({ color: 0x111111, metalness: 0.9, roughness: 0.1 })
280
+ );
281
+ lens.rotation.x = Math.PI / 2;
282
+ lens.position.z = 0.26;
283
+ cameraGroup.add(lens);
284
+ scene.add(cameraGroup);
285
+
286
+ // GREEN: Azimuth ring
287
+ const azimuthRing = new THREE.Mesh(
288
+ new THREE.TorusGeometry(AZIMUTH_RADIUS, 0.04, 16, 64),
289
+ new THREE.MeshStandardMaterial({ color: 0x00ff88, emissive: 0x00ff88, emissiveIntensity: 0.3 })
290
+ );
291
+ azimuthRing.rotation.x = Math.PI / 2;
292
+ azimuthRing.position.y = 0.05;
293
+ scene.add(azimuthRing);
294
+
295
+ const azimuthHandle = new THREE.Mesh(
296
+ new THREE.SphereGeometry(0.18, 16, 16),
297
+ new THREE.MeshStandardMaterial({ color: 0x00ff88, emissive: 0x00ff88, emissiveIntensity: 0.5 })
298
+ );
299
+ azimuthHandle.userData.type = 'azimuth';
300
+ scene.add(azimuthHandle);
301
+
302
+ // PINK: Elevation arc
303
+ const arcPoints = [];
304
+ for (let i = 0; i <= 32; i++) {
305
+ const angle = THREE.MathUtils.degToRad(-30 + (90 * i / 32));
306
+ arcPoints.push(new THREE.Vector3(0, ELEVATION_RADIUS * Math.sin(angle) + CENTER.y, ELEVATION_RADIUS * Math.cos(angle)));
307
+ }
308
+ const arcCurve = new THREE.CatmullRomCurve3(arcPoints);
309
+ const elevationArc = new THREE.Mesh(
310
+ new THREE.TubeGeometry(arcCurve, 32, 0.04, 8, false),
311
+ new THREE.MeshStandardMaterial({ color: 0xff69b4, emissive: 0xff69b4, emissiveIntensity: 0.3 })
312
+ );
313
+ scene.add(elevationArc);
314
+
315
+ const elevationHandle = new THREE.Mesh(
316
+ new THREE.SphereGeometry(0.18, 16, 16),
317
+ new THREE.MeshStandardMaterial({ color: 0xff69b4, emissive: 0xff69b4, emissiveIntensity: 0.5 })
318
+ );
319
+ elevationHandle.userData.type = 'elevation';
320
+ scene.add(elevationHandle);
321
+
322
+ // ORANGE: Distance line & handle
323
+ const distanceLineGeo = new THREE.BufferGeometry();
324
+ const distanceLine = new THREE.Line(distanceLineGeo, new THREE.LineBasicMaterial({ color: 0xffa500 }));
325
+ scene.add(distanceLine);
326
+
327
+ const distanceHandle = new THREE.Mesh(
328
+ new THREE.SphereGeometry(0.18, 16, 16),
329
+ new THREE.MeshStandardMaterial({ color: 0xffa500, emissive: 0xffa500, emissiveIntensity: 0.5 })
330
+ );
331
+ distanceHandle.userData.type = 'distance';
332
+ scene.add(distanceHandle);
333
+
334
+ function updatePositions() {
335
+ const distance = BASE_DISTANCE * distanceFactor;
336
+ const azRad = THREE.MathUtils.degToRad(azimuthAngle);
337
+ const elRad = THREE.MathUtils.degToRad(elevationAngle);
338
+
339
+ const camX = distance * Math.sin(azRad) * Math.cos(elRad);
340
+ const camY = distance * Math.sin(elRad) + CENTER.y;
341
+ const camZ = distance * Math.cos(azRad) * Math.cos(elRad);
342
+
343
+ cameraGroup.position.set(camX, camY, camZ);
344
+ cameraGroup.lookAt(CENTER);
345
+
346
+ azimuthHandle.position.set(AZIMUTH_RADIUS * Math.sin(azRad), 0.05, AZIMUTH_RADIUS * Math.cos(azRad));
347
+ elevationHandle.position.set(0, ELEVATION_RADIUS * Math.sin(elRad) + CENTER.y, ELEVATION_RADIUS * Math.cos(elRad));
348
+
349
+ const orangeDist = distance + 0.5;
350
+ distanceHandle.position.set(
351
+ orangeDist * Math.sin(azRad) * Math.cos(elRad),
352
+ orangeDist * Math.sin(elRad) + CENTER.y,
353
+ orangeDist * Math.cos(azRad) * Math.cos(elRad)
354
+ );
355
+ distanceLineGeo.setFromPoints([CENTER.clone(), distanceHandle.position.clone()]);
356
+
357
+ // Update prompt
358
+ const azSnap = snapToNearest(azimuthAngle, azimuthSteps);
359
+ const elSnap = snapToNearest(elevationAngle, elevationSteps);
360
+ const distSnap = snapToNearest(distanceFactor, distanceSteps);
361
+ const distKey = distSnap === 1 ? '1' : distSnap.toFixed(1);
362
+ const prompt = '<sks> ' + azimuthNames[azSnap] + ' ' + elevationNames[String(elSnap)] + ' ' + distanceNames[distKey];
363
+ promptOverlay.textContent = prompt;
364
+ }
365
+
366
+ function updatePropsAndTrigger() {
367
+ const azSnap = snapToNearest(azimuthAngle, azimuthSteps);
368
+ const elSnap = snapToNearest(elevationAngle, elevationSteps);
369
+ const distSnap = snapToNearest(distanceFactor, distanceSteps);
370
+
371
+ props.value = { azimuth: azSnap, elevation: elSnap, distance: distSnap };
372
+ trigger('change', props.value);
373
+ }
374
+
375
+ // Raycasting
376
+ const raycaster = new THREE.Raycaster();
377
+ const mouse = new THREE.Vector2();
378
+ let isDragging = false;
379
+ let dragTarget = null;
380
+ let dragStartMouse = new THREE.Vector2();
381
+ let dragStartDistance = 1.0;
382
+ const intersection = new THREE.Vector3();
383
+
384
+ const canvas = renderer.domElement;
385
+
386
+ canvas.addEventListener('mousedown', (e) => {
387
+ const rect = canvas.getBoundingClientRect();
388
+ mouse.x = ((e.clientX - rect.left) / rect.width) * 2 - 1;
389
+ mouse.y = -((e.clientY - rect.top) / rect.height) * 2 + 1;
390
+
391
+ raycaster.setFromCamera(mouse, camera);
392
+ const intersects = raycaster.intersectObjects([azimuthHandle, elevationHandle, distanceHandle]);
393
+
394
+ if (intersects.length > 0) {
395
+ isDragging = true;
396
+ dragTarget = intersects[0].object;
397
+ dragTarget.material.emissiveIntensity = 1.0;
398
+ dragTarget.scale.setScalar(1.3);
399
+ dragStartMouse.copy(mouse);
400
+ dragStartDistance = distanceFactor;
401
+ canvas.style.cursor = 'grabbing';
402
+ }
403
+ });
404
+
405
+ canvas.addEventListener('mousemove', (e) => {
406
+ const rect = canvas.getBoundingClientRect();
407
+ mouse.x = ((e.clientX - rect.left) / rect.width) * 2 - 1;
408
+ mouse.y = -((e.clientY - rect.top) / rect.height) * 2 + 1;
409
+
410
+ if (isDragging && dragTarget) {
411
+ raycaster.setFromCamera(mouse, camera);
412
+
413
+ if (dragTarget.userData.type === 'azimuth') {
414
+ const plane = new THREE.Plane(new THREE.Vector3(0, 1, 0), -0.05);
415
+ if (raycaster.ray.intersectPlane(plane, intersection)) {
416
+ azimuthAngle = THREE.MathUtils.radToDeg(Math.atan2(intersection.x, intersection.z));
417
+ if (azimuthAngle < 0) azimuthAngle += 360;
418
+ }
419
+ } else if (dragTarget.userData.type === 'elevation') {
420
+ const plane = new THREE.Plane(new THREE.Vector3(1, 0, 0), 0);
421
+ if (raycaster.ray.intersectPlane(plane, intersection)) {
422
+ const relY = intersection.y - CENTER.y;
423
+ const relZ = intersection.z;
424
+ elevationAngle = THREE.MathUtils.clamp(THREE.MathUtils.radToDeg(Math.atan2(relY, relZ)), -30, 60);
425
+ }
426
+ } else if (dragTarget.userData.type === 'distance') {
427
+ const deltaY = mouse.y - dragStartMouse.y;
428
+ distanceFactor = THREE.MathUtils.clamp(dragStartDistance - deltaY * 1.5, 0.6, 1.8);
429
+ }
430
+ updatePositions();
431
+ } else {
432
+ raycaster.setFromCamera(mouse, camera);
433
+ const intersects = raycaster.intersectObjects([azimuthHandle, elevationHandle, distanceHandle]);
434
+ [azimuthHandle, elevationHandle, distanceHandle].forEach(h => {
435
+ h.material.emissiveIntensity = 0.5;
436
+ h.scale.setScalar(1);
437
+ });
438
+ if (intersects.length > 0) {
439
+ intersects[0].object.material.emissiveIntensity = 0.8;
440
+ intersects[0].object.scale.setScalar(1.1);
441
+ canvas.style.cursor = 'grab';
442
+ } else {
443
+ canvas.style.cursor = 'default';
444
+ }
445
+ }
446
+ });
447
+
448
+ const onMouseUp = () => {
449
+ if (dragTarget) {
450
+ dragTarget.material.emissiveIntensity = 0.5;
451
+ dragTarget.scale.setScalar(1);
452
+
453
+ // Snap and animate
454
+ const targetAz = snapToNearest(azimuthAngle, azimuthSteps);
455
+ const targetEl = snapToNearest(elevationAngle, elevationSteps);
456
+ const targetDist = snapToNearest(distanceFactor, distanceSteps);
457
+
458
+ const startAz = azimuthAngle, startEl = elevationAngle, startDist = distanceFactor;
459
+ const startTime = Date.now();
460
+
461
+ function animateSnap() {
462
+ const t = Math.min((Date.now() - startTime) / 200, 1);
463
+ const ease = 1 - Math.pow(1 - t, 3);
464
+
465
+ let azDiff = targetAz - startAz;
466
+ if (azDiff > 180) azDiff -= 360;
467
+ if (azDiff < -180) azDiff += 360;
468
+ azimuthAngle = startAz + azDiff * ease;
469
+ if (azimuthAngle < 0) azimuthAngle += 360;
470
+ if (azimuthAngle >= 360) azimuthAngle -= 360;
471
+
472
+ elevationAngle = startEl + (targetEl - startEl) * ease;
473
+ distanceFactor = startDist + (targetDist - startDist) * ease;
474
+
475
+ updatePositions();
476
+ if (t < 1) requestAnimationFrame(animateSnap);
477
+ else updatePropsAndTrigger();
478
+ }
479
+ animateSnap();
480
+ }
481
+ isDragging = false;
482
+ dragTarget = null;
483
+ canvas.style.cursor = 'default';
484
+ };
485
+
486
+ canvas.addEventListener('mouseup', onMouseUp);
487
+ canvas.addEventListener('mouseleave', onMouseUp);
488
+
489
+ // Initial update
490
+ updatePositions();
491
+
492
+ // Render loop
493
+ function render() {
494
+ requestAnimationFrame(render);
495
+ renderer.render(scene, camera);
496
+ }
497
+ render();
498
+
499
+ // Handle resize
500
+ new ResizeObserver(() => {
501
+ camera.aspect = wrapper.clientWidth / wrapper.clientHeight;
502
+ camera.updateProjectionMatrix();
503
+ renderer.setSize(wrapper.clientWidth, wrapper.clientHeight);
504
+ }).observe(wrapper);
505
+
506
+ // Store update function for external calls
507
+ wrapper._updateFromProps = (newVal) => {
508
+ if (newVal && typeof newVal === 'object') {
509
+ azimuthAngle = newVal.azimuth ?? azimuthAngle;
510
+ elevationAngle = newVal.elevation ?? elevationAngle;
511
+ distanceFactor = newVal.distance ?? distanceFactor;
512
+ updatePositions();
513
+ }
514
+ };
515
+ };
516
+
517
+ initScene();
518
+ })();
519
+ """
520
+
521
+ super().__init__(
522
+ value=value,
523
+ html_template=html_template,
524
+ js_on_load=js_on_load,
525
+ **kwargs
526
+ )
527
+
528
+
529
+ # --- UI ---
530
+ css = '''
531
+ #col-container { max-width: 1200px; margin: 0 auto; }
532
+ .dark .progress-text { color: white !important; }
533
+ #camera-3d-control { min-height: 450px; }
534
+ .slider-row { display: flex; gap: 10px; align-items: center; }
535
+ '''
536
+
537
+ with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
538
+ gr.Markdown("""
539
+ # 🎬 Qwen Image Edit 2511 — 3D Camera Control
540
+
541
+ Control camera angles using the **3D viewport** or **sliders**.
542
+ Using [fal's Qwen-Image-Edit-2511-Multiple-Angles-LoRA](https://huggingface.co/fal/Qwen-Image-Edit-2511-Multiple-Angles-LoRA) for precise camera control.
543
+ """)
544
+
545
+ with gr.Row():
546
+ # Left column: Input image and controls
547
+ with gr.Column(scale=1):
548
+ image = gr.Image(label="Input Image", type="pil", height=300)
549
+
550
+ gr.Markdown("### 🎮 3D Camera Control")
551
+ gr.Markdown("*Drag the colored handles: 🟢 Azimuth, 🩷 Elevation, 🟠 Distance*")
552
+
553
+ camera_3d = CameraControl3D(
554
+ value={"azimuth": 0, "elevation": 0, "distance": 1.0},
555
+ elem_id="camera-3d-control"
556
+ )
557
+
558
+ gr.Markdown("### 🎚️ Slider Controls")
559
+
560
+ azimuth_slider = gr.Slider(
561
+ label="Azimuth (Horizontal Rotation)",
562
+ minimum=0,
563
+ maximum=315,
564
+ step=45,
565
+ value=0,
566
+ info="0°=front, 90°=right, 180°=back, 270°=left"
567
+ )
568
+
569
+ elevation_slider = gr.Slider(
570
+ label="Elevation (Vertical Angle)",
571
+ minimum=-30,
572
+ maximum=60,
573
+ step=30,
574
+ value=0,
575
+ info="-30°=low angle, 0°=eye level, 60°=high angle"
576
+ )
577
+
578
+ distance_slider = gr.Slider(
579
+ label="Distance",
580
+ minimum=0.6,
581
+ maximum=1.8,
582
+ step=0.6,
583
+ value=1.0,
584
+ info="0.6=close-up, 1.0=medium, 1.8=wide"
585
+ )
586
+
587
+ prompt_preview = gr.Textbox(
588
+ label="Generated Prompt",
589
+ value="<sks> front view eye-level shot medium shot",
590
+ interactive=False
591
+ )
592
+
593
+ run_btn = gr.Button("🚀 Generate", variant="primary", size="lg")
594
+
595
+ # Right column: Output
596
+ with gr.Column(scale=1):
597
+ result = gr.Image(label="Output Image", height=500)
598
+
599
+ with gr.Accordion("⚙️ Advanced Settings", open=False):
600
+ seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
601
+ randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
602
+ guidance_scale = gr.Slider(label="Guidance Scale", minimum=1.0, maximum=10.0, step=0.1, value=1.0)
603
+ num_inference_steps = gr.Slider(label="Inference Steps", minimum=1, maximum=20, step=1, value=4)
604
+ height = gr.Slider(label="Height", minimum=256, maximum=2048, step=8, value=1024)
605
+ width = gr.Slider(label="Width", minimum=256, maximum=2048, step=8, value=1024)
606
+
607
+ # --- Event Handlers ---
608
+
609
+ def update_prompt_from_sliders(azimuth, elevation, distance):
610
+ """Update prompt preview when sliders change."""
611
+ prompt = build_camera_prompt(azimuth, elevation, distance)
612
+ return prompt
613
+
614
+ def sync_3d_to_sliders(camera_value):
615
+ """Sync 3D control changes to sliders."""
616
+ if camera_value and isinstance(camera_value, dict):
617
+ az = camera_value.get('azimuth', 0)
618
+ el = camera_value.get('elevation', 0)
619
+ dist = camera_value.get('distance', 1.0)
620
+ prompt = build_camera_prompt(az, el, dist)
621
+ return az, el, dist, prompt
622
+ return gr.update(), gr.update(), gr.update(), gr.update()
623
+
624
+ def sync_sliders_to_3d(azimuth, elevation, distance):
625
+ """Sync slider changes to 3D control."""
626
+ return {"azimuth": azimuth, "elevation": elevation, "distance": distance}
627
+
628
+ # Slider -> Prompt preview
629
+ for slider in [azimuth_slider, elevation_slider, distance_slider]:
630
+ slider.change(
631
+ fn=update_prompt_from_sliders,
632
+ inputs=[azimuth_slider, elevation_slider, distance_slider],
633
+ outputs=[prompt_preview]
634
+ )
635
+
636
+ # 3D control -> Sliders + Prompt
637
+ camera_3d.change(
638
+ fn=sync_3d_to_sliders,
639
+ inputs=[camera_3d],
640
+ outputs=[azimuth_slider, elevation_slider, distance_slider, prompt_preview]
641
+ )
642
+
643
+ # Sliders -> 3D control
644
+ for slider in [azimuth_slider, elevation_slider, distance_slider]:
645
+ slider.release(
646
+ fn=sync_sliders_to_3d,
647
+ inputs=[azimuth_slider, elevation_slider, distance_slider],
648
+ outputs=[camera_3d]
649
+ )
650
+
651
+ # Generate button
652
+ run_btn.click(
653
+ fn=infer_camera_edit,
654
+ inputs=[image, azimuth_slider, elevation_slider, distance_slider, seed, randomize_seed, guidance_scale, num_inference_steps, height, width],
655
+ outputs=[result, seed, prompt_preview]
656
+ )
657
+
658
+ # Image upload -> update dimensions
659
+ image.upload(
660
+ fn=update_dimensions_on_upload,
661
+ inputs=[image],
662
+ outputs=[width, height]
663
+ )
664
+
665
+ # Examples
666
+ gr.Examples(
667
+ examples=[
668
+ ["example1.jpg", 90, 0, 1.0],
669
+ ["example2.jpg", 0, 30, 0.6],
670
+ ["example3.jpg", 180, -30, 1.8],
671
+ ],
672
+ inputs=[image, azimuth_slider, elevation_slider, distance_slider],
673
+ outputs=[result, seed, prompt_preview],
674
+ fn=lambda img, az, el, dist: infer_camera_edit(img, az, el, dist),
675
+ cache_examples=False,
676
+ )
677
+
678
+ if __name__ == "__main__":
679
+ demo.launch()