Dhenenjay commited on
Commit
5b53ecd
·
verified ·
1 Parent(s): aef5404

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +65 -37
app.py CHANGED
@@ -10,17 +10,17 @@ import gradio as gr
10
  import tempfile
11
  import time
12
 
13
- print("[E3Diff] Starting app...")
14
 
15
  # ZeroGPU support
16
  try:
17
  import spaces
18
  GPU_AVAILABLE = True
19
- print("[E3Diff] ZeroGPU available")
20
  except ImportError:
21
  GPU_AVAILABLE = False
22
  spaces = None
23
- print("[E3Diff] Running without ZeroGPU")
24
 
25
 
26
  # Lazy imports for heavy modules
@@ -30,20 +30,20 @@ _model_modules = None
30
  def get_torch():
31
  global _torch
32
  if _torch is None:
33
- print("[E3Diff] Importing torch...")
34
  import torch
35
  _torch = torch
36
- print(f"[E3Diff] PyTorch {torch.__version__} loaded")
37
  return _torch
38
 
39
  def get_model_modules():
40
  global _model_modules
41
  if _model_modules is None:
42
- print("[E3Diff] Importing model modules...")
43
  from unet import UNet
44
  from diffusion import GaussianDiffusion
45
  _model_modules = (UNet, GaussianDiffusion)
46
- print("[E3Diff] Model modules loaded")
47
  return _model_modules
48
 
49
 
@@ -87,7 +87,7 @@ def build_model(device):
87
  UNet, GaussianDiffusion = get_model_modules()
88
  from huggingface_hub import hf_hub_download
89
 
90
- print("[E3Diff] Building model architecture...")
91
 
92
  image_size = 256
93
  num_inference_steps = 1
@@ -142,18 +142,18 @@ def build_model(device):
142
  model = model.to(device)
143
 
144
  # Load weights
145
- print("[E3Diff] Downloading weights...")
146
  weights_path = hf_hub_download(
147
  repo_id="Dhenenjay/E3Diff-SAR2Optical",
148
  filename="I700000_E719_gen.pth"
149
  )
150
 
151
- print(f"[E3Diff] Loading weights from: {weights_path}")
152
  state_dict = torch.load(weights_path, map_location=device, weights_only=False)
153
  model.load_state_dict(state_dict, strict=False)
154
  model.eval()
155
 
156
- print("[E3Diff] Model ready!")
157
  return model
158
 
159
 
@@ -262,7 +262,7 @@ def process_image(image, model, device, overlap=64):
262
  x_positions = list(range(0, w_pad - tile_size + 1, step))
263
  total_tiles = len(y_positions) * len(x_positions)
264
 
265
- print(f"[E3Diff] Processing {total_tiles} tiles ({len(x_positions)}x{len(y_positions)}) at {w}x{h}...")
266
 
267
  tile_idx = 0
268
  for y in y_positions:
@@ -281,7 +281,7 @@ def process_image(image, model, device, overlap=64):
281
 
282
  tile_idx += 1
283
  if tile_idx % 10 == 0 or tile_idx == total_tiles:
284
- print(f"[E3Diff] Tile {tile_idx}/{total_tiles}")
285
 
286
  # Normalize
287
  output = output / (weights + 1e-8)
@@ -303,7 +303,7 @@ def _translate_impl(file, overlap, enhance_output):
303
 
304
  torch = get_torch()
305
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
306
- print(f"[E3Diff] Using device: {device}")
307
 
308
  # Load model (cached)
309
  if _cached_model is None:
@@ -313,11 +313,11 @@ def _translate_impl(file, overlap, enhance_output):
313
 
314
  # Load image
315
  filepath = file.name if hasattr(file, 'name') else file
316
- print(f"[E3Diff] Loading: {filepath}")
317
  image = load_sar_image(filepath)
318
 
319
  w, h = image.size
320
- print(f"[E3Diff] Input size: {w}x{h}")
321
 
322
  start = time.time()
323
  result = process_image(image, model, device, overlap=int(overlap))
@@ -331,7 +331,7 @@ def _translate_impl(file, overlap, enhance_output):
331
  tiff_path = tempfile.mktemp(suffix='.tiff')
332
  result_pil.save(tiff_path, format='TIFF', compression='lzw')
333
 
334
- print(f"[E3Diff] Complete in {elapsed:.1f}s!")
335
 
336
  info = f"Processed in {elapsed:.1f}s | Output: {result_pil.size[0]}x{result_pil.size[1]}"
337
 
@@ -347,31 +347,58 @@ else:
347
  translate_sar = _translate_impl
348
 
349
 
350
- print("[E3Diff] Building Gradio interface...")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
351
 
352
  # Create Gradio interface
353
- with gr.Blocks(title="E3Diff: SAR-to-Optical Translation") as demo:
354
- gr.Markdown("""
355
- # 🛰️ E3Diff: High-Resolution SAR-to-Optical Translation
356
-
357
- **CVPR PBVS2025 Challenge Winner** | Upload any SAR image and get a photorealistic optical translation.
358
-
359
- - Full resolution processing with seamless tiling
360
- - One-step diffusion (optimized for speed & quality)
361
- - TIFF output for commercial use
362
  """)
363
 
364
  with gr.Row():
365
  with gr.Column():
366
- input_file = gr.File(label="SAR Input (TIFF, PNG, JPG)", file_types=[".tif", ".tiff", ".png", ".jpg", ".jpeg"])
367
- overlap = gr.Slider(16, 128, value=64, step=16, label="Tile Overlap (higher=smoother)")
368
- enhance = gr.Checkbox(value=True, label="Apply enhancement")
369
- submit_btn = gr.Button("🚀 Translate to Optical", variant="primary")
 
370
 
371
  with gr.Column():
372
  output_image = gr.Image(label="Optical Output")
373
- output_file = gr.File(label="Download TIFF")
374
- info_text = gr.Textbox(label="Processing Info")
375
 
376
  submit_btn.click(
377
  fn=translate_sar,
@@ -379,12 +406,13 @@ with gr.Blocks(title="E3Diff: SAR-to-Optical Translation") as demo:
379
  outputs=[output_image, output_file, info_text]
380
  )
381
 
382
- gr.Markdown("""
383
- ---
384
- **Note:** E3Diff is a one-step diffusion model. Multiple steps degrade quality.
 
385
  """)
386
 
387
- print("[E3Diff] Launching app...")
388
 
389
  if __name__ == "__main__":
390
  demo.queue().launch(ssr_mode=False)
 
10
  import tempfile
11
  import time
12
 
13
+ print("[Axion] Starting app...")
14
 
15
  # ZeroGPU support
16
  try:
17
  import spaces
18
  GPU_AVAILABLE = True
19
+ print("[Axion] ZeroGPU available")
20
  except ImportError:
21
  GPU_AVAILABLE = False
22
  spaces = None
23
+ print("[Axion] Running without ZeroGPU")
24
 
25
 
26
  # Lazy imports for heavy modules
 
30
  def get_torch():
31
  global _torch
32
  if _torch is None:
33
+ print("[Axion] Importing torch...")
34
  import torch
35
  _torch = torch
36
+ print(f"[Axion] PyTorch {torch.__version__} loaded")
37
  return _torch
38
 
39
  def get_model_modules():
40
  global _model_modules
41
  if _model_modules is None:
42
+ print("[Axion] Importing model modules...")
43
  from unet import UNet
44
  from diffusion import GaussianDiffusion
45
  _model_modules = (UNet, GaussianDiffusion)
46
+ print("[Axion] Model modules loaded")
47
  return _model_modules
48
 
49
 
 
87
  UNet, GaussianDiffusion = get_model_modules()
88
  from huggingface_hub import hf_hub_download
89
 
90
+ print("[Axion] Building model architecture...")
91
 
92
  image_size = 256
93
  num_inference_steps = 1
 
142
  model = model.to(device)
143
 
144
  # Load weights
145
+ print("[Axion] Downloading weights...")
146
  weights_path = hf_hub_download(
147
  repo_id="Dhenenjay/E3Diff-SAR2Optical",
148
  filename="I700000_E719_gen.pth"
149
  )
150
 
151
+ print(f"[Axion] Loading weights from: {weights_path}")
152
  state_dict = torch.load(weights_path, map_location=device, weights_only=False)
153
  model.load_state_dict(state_dict, strict=False)
154
  model.eval()
155
 
156
+ print("[Axion] Model ready!")
157
  return model
158
 
159
 
 
262
  x_positions = list(range(0, w_pad - tile_size + 1, step))
263
  total_tiles = len(y_positions) * len(x_positions)
264
 
265
+ print(f"[Axion] Processing {total_tiles} tiles ({len(x_positions)}x{len(y_positions)}) at {w}x{h}...")
266
 
267
  tile_idx = 0
268
  for y in y_positions:
 
281
 
282
  tile_idx += 1
283
  if tile_idx % 10 == 0 or tile_idx == total_tiles:
284
+ print(f"[Axion] Tile {tile_idx}/{total_tiles}")
285
 
286
  # Normalize
287
  output = output / (weights + 1e-8)
 
303
 
304
  torch = get_torch()
305
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
306
+ print(f"[Axion] Using device: {device}")
307
 
308
  # Load model (cached)
309
  if _cached_model is None:
 
313
 
314
  # Load image
315
  filepath = file.name if hasattr(file, 'name') else file
316
+ print(f"[Axion] Loading: {filepath}")
317
  image = load_sar_image(filepath)
318
 
319
  w, h = image.size
320
+ print(f"[Axion] Input size: {w}x{h}")
321
 
322
  start = time.time()
323
  result = process_image(image, model, device, overlap=int(overlap))
 
331
  tiff_path = tempfile.mktemp(suffix='.tiff')
332
  result_pil.save(tiff_path, format='TIFF', compression='lzw')
333
 
334
+ print(f"[Axion] Complete in {elapsed:.1f}s!")
335
 
336
  info = f"Processed in {elapsed:.1f}s | Output: {result_pil.size[0]}x{result_pil.size[1]}"
337
 
 
347
  translate_sar = _translate_impl
348
 
349
 
350
+ print("[Axion] Building Gradio interface...")
351
+
352
+ # Custom CSS for dark minimal theme
353
+ custom_css = """
354
+ .gradio-container {
355
+ background: linear-gradient(180deg, #0a0a0a 0%, #1a1a1a 100%) !important;
356
+ }
357
+ .main-title {
358
+ font-family: 'Helvetica Neue', Arial, sans-serif !important;
359
+ font-size: 3.5rem !important;
360
+ font-weight: 200 !important;
361
+ color: #ffffff !important;
362
+ text-align: center !important;
363
+ margin-bottom: 0.5rem !important;
364
+ letter-spacing: -0.02em !important;
365
+ }
366
+ .subtitle {
367
+ font-family: 'Helvetica Neue', Arial, sans-serif !important;
368
+ font-size: 1.1rem !important;
369
+ font-weight: 300 !important;
370
+ color: #888888 !important;
371
+ text-align: center !important;
372
+ margin-bottom: 2rem !important;
373
+ }
374
+ .dark-panel {
375
+ background: rgba(30, 30, 30, 0.6) !important;
376
+ border: 1px solid #333 !important;
377
+ border-radius: 12px !important;
378
+ }
379
+ """
380
 
381
  # Create Gradio interface
382
+ with gr.Blocks(title="Axion - SAR to Optical", css=custom_css) as demo:
383
+ gr.HTML("""
384
+ <div style="text-align: center; padding: 40px 20px 20px 20px; background: linear-gradient(180deg, #0a0a0a 0%, #1a1a1a 100%);">
385
+ <h1 style="font-family: 'Helvetica Neue', Arial, sans-serif; font-size: 3.2rem; font-weight: 200; color: #ffffff; margin-bottom: 0.5rem; letter-spacing: -0.02em;">SAR to Optical Image Translation</h1>
386
+ <p style="font-family: 'Helvetica Neue', Arial, sans-serif; font-size: 1.1rem; font-weight: 300; color: #888888;">Transform radar imagery into crystal-clear optical views using our foundation model</p>
387
+ </div>
 
 
 
388
  """)
389
 
390
  with gr.Row():
391
  with gr.Column():
392
+ input_file = gr.File(label="Upload SAR Image", file_types=[".tif", ".tiff", ".png", ".jpg", ".jpeg"])
393
+ with gr.Row():
394
+ overlap = gr.Slider(16, 128, value=64, step=16, label="Tile Overlap")
395
+ enhance = gr.Checkbox(value=True, label="Enhance Output")
396
+ submit_btn = gr.Button("Translate", variant="primary")
397
 
398
  with gr.Column():
399
  output_image = gr.Image(label="Optical Output")
400
+ output_file = gr.File(label="Download")
401
+ info_text = gr.Textbox(label="Info", show_label=False)
402
 
403
  submit_btn.click(
404
  fn=translate_sar,
 
406
  outputs=[output_image, output_file, info_text]
407
  )
408
 
409
+ gr.HTML("""
410
+ <div style="text-align: center; padding: 20px; color: #555; font-size: 0.85rem;">
411
+ Powered by <strong style="color: #888;">Axion</strong>
412
+ </div>
413
  """)
414
 
415
+ print("[Axion] Launching app...")
416
 
417
  if __name__ == "__main__":
418
  demo.queue().launch(ssr_mode=False)