Spaces:
Runtime error
Runtime error
File size: 2,229 Bytes
4878904 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
import gradio as gr
import torch
from PIL import Image
import numpy as np
import os
from pathlib import Path
import tempfile
from huggingface_hub import hf_hub_download
# Set environment variable to use CPU
os.environ["SF3D_USE_CPU"] = "1"
# Import the main pipeline
from stable_fast_3d import StableFast3D
# Initialize the model
model = StableFast3D()
def process_image(image, prompt):
# Convert image to PIL if it's not already
if isinstance(image, np.ndarray):
image = Image.fromarray(image)
# Create temporary directory for output
with tempfile.TemporaryDirectory() as tmpdir:
output_path = Path(tmpdir) / "output.glb"
# Process the image
model.process_image(
image=image,
prompt=prompt,
output_path=str(output_path)
)
# Return the GLB file
return str(output_path)
def convert_2d_to_3d(image, prompt=None):
"""
Convert a 2D image to a 3D model using Stable Diffusion and advanced 3D reconstruction.
Args:
image (PIL.Image): Input 2D image
prompt (str, optional): Text prompt to guide the 3D generation
Returns:
str: Path to the generated GLB file
"""
# TODO: Implement the actual 2D to 3D conversion logic
# For now, return a placeholder message
return "3D conversion will be implemented soon!"
# Create Gradio interface
with gr.Blocks() as demo:
gr.Markdown("# Stable Fast 3D - Convert 2D Images to 3D Models")
gr.Markdown("Upload a 2D image and get a 3D model in return. Optionally provide a text prompt to guide the generation.")
with gr.Row():
with gr.Column():
input_image = gr.Image(type="pil", label="Input Image")
text_prompt = gr.Textbox(label="Text Prompt (optional)", placeholder="Enter a description to guide the 3D generation...")
convert_btn = gr.Button("Convert to 3D")
with gr.Column():
output = gr.Text(label="Output")
convert_btn.click(
fn=convert_2d_to_3d,
inputs=[input_image, text_prompt],
outputs=output
)
# Launch the app
if __name__ == "__main__":
demo.launch() |