verymehari commited on
Commit
17b4a27
·
verified ·
1 Parent(s): 9b2bba2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -5
app.py CHANGED
@@ -4,6 +4,7 @@ import os
4
  import uuid
5
  import shutil
6
  import tempfile
 
7
 
8
  DESCRIPTION = """# SHARP · 3D from a Single Photo
9
  Upload any photo and get a **3D Gaussian Splat (.ply)** in seconds.
@@ -15,37 +16,68 @@ Powered by Apple's [SHARP](https://github.com/apple/ml-sharp) monocular view syn
15
  3. Download the `.ply` file
16
  4. View it at [SuperSplat](https://playcanvas.com/supersplat/editor)
17
 
18
- > Running on CPU - first run downloads the 2.6GB model and may take 5-10 min."""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
  def generate_splat(image_path):
21
  if image_path is None:
22
  raise gr.Error("Please upload an image first.")
 
23
  job_id = str(uuid.uuid4())
24
  input_dir = os.path.join(tempfile.gettempdir(), f"sharp_in_{job_id}")
25
  output_dir = os.path.join(tempfile.gettempdir(), f"sharp_out_{job_id}")
26
  os.makedirs(input_dir, exist_ok=True)
27
  os.makedirs(output_dir, exist_ok=True)
 
28
  try:
29
  ext = os.path.splitext(image_path)[1] or ".jpg"
30
  shutil.copy(image_path, os.path.join(input_dir, f"input{ext}"))
 
 
31
  result = subprocess.run(
32
- ["sharp", "predict", "-i", input_dir, "-o", output_dir],
33
  capture_output=True, text=True, timeout=600
34
  )
 
35
  if result.returncode != 0:
36
- raise gr.Error(f"SHARP failed: {result.stderr[-500:]}")
 
37
  ply_files = [f for f in os.listdir(output_dir) if f.endswith(".ply")]
38
  if not ply_files:
39
  raise gr.Error("No .ply file generated. Try a different image.")
 
40
  out_path = os.path.join(tempfile.gettempdir(), f"output_{job_id}.ply")
41
  shutil.copy(os.path.join(output_dir, ply_files[0]), out_path)
42
- return out_path, "Done! Download your .ply file above, then open it in SuperSplat."
 
43
  except subprocess.TimeoutExpired:
44
  raise gr.Error("Timed out after 10 minutes.")
 
 
45
  finally:
46
  shutil.rmtree(input_dir, ignore_errors=True)
47
  shutil.rmtree(output_dir, ignore_errors=True)
48
 
 
49
  with gr.Blocks(title="SHARP 3D") as demo:
50
  gr.Markdown(DESCRIPTION)
51
  with gr.Row():
@@ -55,7 +87,12 @@ with gr.Blocks(title="SHARP 3D") as demo:
55
  with gr.Column():
56
  file_output = gr.File(label="Download .ply file")
57
  status_output = gr.Markdown("")
58
- run_btn.click(fn=generate_splat, inputs=image_input, outputs=[file_output, status_output])
 
 
 
 
 
59
 
60
  if __name__ == "__main__":
61
  demo.launch(server_name="0.0.0.0")
 
4
  import uuid
5
  import shutil
6
  import tempfile
7
+ import sys
8
 
9
  DESCRIPTION = """# SHARP · 3D from a Single Photo
10
  Upload any photo and get a **3D Gaussian Splat (.ply)** in seconds.
 
16
  3. Download the `.ply` file
17
  4. View it at [SuperSplat](https://playcanvas.com/supersplat/editor)
18
 
19
+ > Running on CPU first run downloads the ~2.6GB model and may take 510 min."""
20
+
21
+
22
+ def get_sharp_bin():
23
+ """Locate the sharp CLI binary from the current Python environment."""
24
+ scripts_dir = os.path.join(os.path.dirname(sys.executable), "sharp")
25
+ if os.path.isfile(scripts_dir):
26
+ return scripts_dir
27
+ # Standard location: same bin dir as python
28
+ candidate = os.path.join(os.path.dirname(sys.executable), "sharp")
29
+ if os.path.isfile(candidate):
30
+ return candidate
31
+ # Try finding via which
32
+ result = subprocess.run(["which", "sharp"], capture_output=True, text=True)
33
+ if result.returncode == 0:
34
+ return result.stdout.strip()
35
+ raise FileNotFoundError(
36
+ "Could not locate the `sharp` binary. "
37
+ "Make sure `git+https://github.com/apple/ml-sharp.git` is in requirements.txt."
38
+ )
39
+
40
 
41
  def generate_splat(image_path):
42
  if image_path is None:
43
  raise gr.Error("Please upload an image first.")
44
+
45
  job_id = str(uuid.uuid4())
46
  input_dir = os.path.join(tempfile.gettempdir(), f"sharp_in_{job_id}")
47
  output_dir = os.path.join(tempfile.gettempdir(), f"sharp_out_{job_id}")
48
  os.makedirs(input_dir, exist_ok=True)
49
  os.makedirs(output_dir, exist_ok=True)
50
+
51
  try:
52
  ext = os.path.splitext(image_path)[1] or ".jpg"
53
  shutil.copy(image_path, os.path.join(input_dir, f"input{ext}"))
54
+
55
+ sharp_bin = get_sharp_bin()
56
  result = subprocess.run(
57
+ [sharp_bin, "predict", "-i", input_dir, "-o", output_dir],
58
  capture_output=True, text=True, timeout=600
59
  )
60
+
61
  if result.returncode != 0:
62
+ raise gr.Error(f"SHARP failed:\n{result.stderr[-800:]}")
63
+
64
  ply_files = [f for f in os.listdir(output_dir) if f.endswith(".ply")]
65
  if not ply_files:
66
  raise gr.Error("No .ply file generated. Try a different image.")
67
+
68
  out_path = os.path.join(tempfile.gettempdir(), f"output_{job_id}.ply")
69
  shutil.copy(os.path.join(output_dir, ply_files[0]), out_path)
70
+ return out_path, "Done! Download your .ply above, then open it in SuperSplat."
71
+
72
  except subprocess.TimeoutExpired:
73
  raise gr.Error("Timed out after 10 minutes.")
74
+ except FileNotFoundError as e:
75
+ raise gr.Error(str(e))
76
  finally:
77
  shutil.rmtree(input_dir, ignore_errors=True)
78
  shutil.rmtree(output_dir, ignore_errors=True)
79
 
80
+
81
  with gr.Blocks(title="SHARP 3D") as demo:
82
  gr.Markdown(DESCRIPTION)
83
  with gr.Row():
 
87
  with gr.Column():
88
  file_output = gr.File(label="Download .ply file")
89
  status_output = gr.Markdown("")
90
+
91
+ run_btn.click(
92
+ fn=generate_splat,
93
+ inputs=image_input,
94
+ outputs=[file_output, status_output]
95
+ )
96
 
97
  if __name__ == "__main__":
98
  demo.launch(server_name="0.0.0.0")