2dto3d / app.py
harshasoftware's picture
Update app.py
95e2f43 verified
import gradio as gr
from diffusers import StableDiffusionPipeline
import torch
from PIL import Image
import io
from huggingface_hub import login
import os
from huggingface_hub import hf_hub_download
# Authenticate with Hugging Face
login(token=os.environ.get('your_huggingface_token_here'))
# Load the Stable Fast 3D model
# Try to download the model config to see if you have access
model_id = "stabilityai/stable-fast-3d"
try:
config_file = hf_hub_download(repo_id=model_id, filename="config.json")
print("Successfully accessed the model!")
except Exception as e:
print(f"Error accessing the model: {e}")
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
pipe = pipe.to("cuda")
def convert_2d_to_3d(input_image, prompt):
# Prepare the input image
if input_image is not None:
input_image = Image.open(io.BytesIO(input_image))
input_image = input_image.resize((512, 512))
# Generate the 3D preview
output_image = pipe(
prompt=prompt,
image=input_image,
num_inference_steps=50,
guidance_scale=7.5
).images[0]
return output_image
# Create the Gradio interface
iface = gr.Interface(
fn=convert_2d_to_3d,
inputs=[
gr.Image(type="filepath", label="Upload 2D Floor Layout"),
gr.Textbox(label="Prompt (e.g., '3D render of a modern apartment floor plan')")
],
outputs=gr.Image(type="pil", label="3D Preview"),
title="2D to 3D Floor Layout Converter",
description="Upload a 2D floor layout image and get a 3D preview using Stable Fast 3D model."
)
# Launch the app
iface.launch()