Spaces:
Running
Running
Commit ·
45e56d0
1
Parent(s): 4b62604
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import subprocess
|
| 3 |
+
import os
|
| 4 |
+
import uuid
|
| 5 |
+
import shutil
|
| 6 |
+
import tempfile
|
| 7 |
+
|
| 8 |
+
DESCRIPTION = """
|
| 9 |
+
# SHARP · 3D from a Single Photo
|
| 10 |
+
Upload any photo and get a **3D Gaussian Splat (.ply)** in seconds.
|
| 11 |
+
Powered by Apple's [SHARP](https://github.com/apple/ml-sharp) monocular view synthesis model.
|
| 12 |
+
|
| 13 |
+
**How to use:**
|
| 14 |
+
1. Upload or drag & drop an image
|
| 15 |
+
2. Click **Generate 3D Splat**
|
| 16 |
+
3. Download the `.ply` file
|
| 17 |
+
4. View it at [SuperSplat](https://playcanvas.com/supersplat/editor) ← open this in a new tab and drag your file in!
|
| 18 |
+
|
| 19 |
+
> ⚠️ Running on CPU — first run downloads the 2.6GB model and may take 5–10 min. Subsequent runs are faster.
|
| 20 |
+
"""
|
| 21 |
+
|
| 22 |
+
def generate_splat(image_path):
|
| 23 |
+
if image_path is None:
|
| 24 |
+
raise gr.Error("Please upload an image first.")
|
| 25 |
+
|
| 26 |
+
job_id = str(uuid.uuid4())
|
| 27 |
+
input_dir = os.path.join(tempfile.gettempdir(), f"sharp_in_{job_id}")
|
| 28 |
+
output_dir = os.path.join(tempfile.gettempdir(), f"sharp_out_{job_id}")
|
| 29 |
+
os.makedirs(input_dir, exist_ok=True)
|
| 30 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 31 |
+
|
| 32 |
+
try:
|
| 33 |
+
# Copy input image into the input dir
|
| 34 |
+
ext = os.path.splitext(image_path)[1] or ".jpg"
|
| 35 |
+
input_copy = os.path.join(input_dir, f"input{ext}")
|
| 36 |
+
shutil.copy(image_path, input_copy)
|
| 37 |
+
|
| 38 |
+
# Run SHARP
|
| 39 |
+
result = subprocess.run(
|
| 40 |
+
["sharp", "predict", "-i", input_dir, "-o", output_dir],
|
| 41 |
+
capture_output=True,
|
| 42 |
+
text=True,
|
| 43 |
+
timeout=600
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
if result.returncode != 0:
|
| 47 |
+
raise gr.Error(f"SHARP failed: {result.stderr[-500:]}")
|
| 48 |
+
|
| 49 |
+
# Find output .ply
|
| 50 |
+
ply_files = [f for f in os.listdir(output_dir) if f.endswith(".ply")]
|
| 51 |
+
if not ply_files:
|
| 52 |
+
raise gr.Error("No .ply file was generated. Try a different image.")
|
| 53 |
+
|
| 54 |
+
ply_path = os.path.join(output_dir, ply_files[0])
|
| 55 |
+
|
| 56 |
+
# Copy to a stable temp path for Gradio to serve
|
| 57 |
+
out_path = os.path.join(tempfile.gettempdir(), f"output_{job_id}.ply")
|
| 58 |
+
shutil.copy(ply_path, out_path)
|
| 59 |
+
|
| 60 |
+
return out_path, "✅ Done! Download your .ply file above, then open it in SuperSplat."
|
| 61 |
+
|
| 62 |
+
except subprocess.TimeoutExpired:
|
| 63 |
+
raise gr.Error("Timed out after 10 minutes. Try a smaller image.")
|
| 64 |
+
finally:
|
| 65 |
+
shutil.rmtree(input_dir, ignore_errors=True)
|
| 66 |
+
shutil.rmtree(output_dir, ignore_errors=True)
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
with gr.Blocks(
|
| 70 |
+
title="SHARP · 3D from a Single Photo",
|
| 71 |
+
theme=gr.themes.Base(
|
| 72 |
+
primary_hue="lime",
|
| 73 |
+
neutral_hue="slate",
|
| 74 |
+
font=[gr.themes.GoogleFont("Syne"), "sans-serif"],
|
| 75 |
+
),
|
| 76 |
+
css="""
|
| 77 |
+
.gradio-container { max-width: 720px !important; margin: auto; }
|
| 78 |
+
#title { text-align: center; }
|
| 79 |
+
footer { display: none !important; }
|
| 80 |
+
"""
|
| 81 |
+
) as demo:
|
| 82 |
+
gr.Markdown(DESCRIPTION, elem_id="title")
|
| 83 |
+
|
| 84 |
+
with gr.Row():
|
| 85 |
+
with gr.Column():
|
| 86 |
+
image_input = gr.Image(
|
| 87 |
+
type="filepath",
|
| 88 |
+
label="Input Image",
|
| 89 |
+
sources=["upload", "clipboard"],
|
| 90 |
+
)
|
| 91 |
+
run_btn = gr.Button("⚡ Generate 3D Splat", variant="primary", size="lg")
|
| 92 |
+
|
| 93 |
+
with gr.Column():
|
| 94 |
+
file_output = gr.File(label="Download .ply file", visible=True)
|
| 95 |
+
status_output = gr.Markdown("")
|
| 96 |
+
|
| 97 |
+
gr.Examples(
|
| 98 |
+
examples=[["data/teaser.jpg"]],
|
| 99 |
+
inputs=image_input,
|
| 100 |
+
label="Try the sample image"
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
run_btn.click(
|
| 104 |
+
fn=generate_splat,
|
| 105 |
+
inputs=image_input,
|
| 106 |
+
outputs=[file_output, status_output],
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
gr.Markdown("""
|
| 110 |
+
---
|
| 111 |
+
**After downloading your .ply:**
|
| 112 |
+
Go to [playcanvas.com/supersplat/editor](https://playcanvas.com/supersplat/editor) and drag your file in to view the 3D scene.
|
| 113 |
+
""")
|
| 114 |
+
|
| 115 |
+
if __name__ == "__main__":
|
| 116 |
+
demo.launch()
|