Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,56 +2,120 @@ import shlex
|
|
| 2 |
import subprocess
|
| 3 |
import os
|
| 4 |
import sys
|
| 5 |
-
|
|
|
|
|
|
|
| 6 |
import torch
|
| 7 |
import fire
|
| 8 |
import gradio as gr
|
| 9 |
from gradio_app.gradio_3dgen import create_ui as create_3d_ui
|
| 10 |
from gradio_app.all_models import model_zoo
|
| 11 |
|
| 12 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
def setup_dependencies():
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
# Download model checkpoints
|
| 29 |
def setup_model():
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
# Application title
|
| 38 |
_TITLE = 'Text to 3D'
|
| 39 |
|
| 40 |
def launch():
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
if __name__ == '__main__':
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import subprocess
|
| 3 |
import os
|
| 4 |
import sys
|
| 5 |
+
import logging
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
from huggingface_hub import snapshot_download, HfHubError
|
| 8 |
import torch
|
| 9 |
import fire
|
| 10 |
import gradio as gr
|
| 11 |
from gradio_app.gradio_3dgen import create_ui as create_3d_ui
|
| 12 |
from gradio_app.all_models import model_zoo
|
| 13 |
|
| 14 |
+
# Set up logging
|
| 15 |
+
logging.basicConfig(
|
| 16 |
+
level=logging.INFO,
|
| 17 |
+
format='%(asctime)s - %(levelname)s - %(message)s'
|
| 18 |
+
)
|
| 19 |
+
logger = logging.getLogger(__name__)
|
| 20 |
+
|
| 21 |
def setup_dependencies():
|
| 22 |
+
"""Install required packages with error handling"""
|
| 23 |
+
try:
|
| 24 |
+
logger.info("Installing dependencies...")
|
| 25 |
+
subprocess.run(shlex.split("pip install pip==24.0"), check=True)
|
| 26 |
+
|
| 27 |
+
# Define package paths
|
| 28 |
+
packages = [
|
| 29 |
+
"package/onnxruntime_gpu-1.17.0-cp310-cp310-manylinux_2_28_x86_64.whl",
|
| 30 |
+
"package/nvdiffrast-0.3.1.torch-cp310-cp310-linux_x86_64.whl"
|
| 31 |
+
]
|
| 32 |
+
|
| 33 |
+
# Check if package files exist
|
| 34 |
+
for package in packages:
|
| 35 |
+
if not Path(package).exists():
|
| 36 |
+
raise FileNotFoundError(f"Package file not found: {package}")
|
| 37 |
+
|
| 38 |
+
logger.info(f"Installing {package}")
|
| 39 |
+
subprocess.run(
|
| 40 |
+
shlex.split(f"pip install {package} --force-reinstall --no-deps"),
|
| 41 |
+
check=True
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
logger.info("Dependencies installed successfully")
|
| 45 |
+
except subprocess.CalledProcessError as e:
|
| 46 |
+
logger.error(f"Failed to install dependencies: {str(e)}")
|
| 47 |
+
raise
|
| 48 |
+
except FileNotFoundError as e:
|
| 49 |
+
logger.error(str(e))
|
| 50 |
+
raise
|
| 51 |
|
|
|
|
| 52 |
def setup_model():
|
| 53 |
+
"""Download and set up model with error handling"""
|
| 54 |
+
try:
|
| 55 |
+
logger.info("Downloading model checkpoints...")
|
| 56 |
+
|
| 57 |
+
# Create checkpoint directory if it doesn't exist
|
| 58 |
+
ckpt_dir = Path("./ckpt")
|
| 59 |
+
ckpt_dir.mkdir(parents=True, exist_ok=True)
|
| 60 |
+
|
| 61 |
+
# Download model with retry mechanism
|
| 62 |
+
max_retries = 3
|
| 63 |
+
for attempt in range(max_retries):
|
| 64 |
+
try:
|
| 65 |
+
snapshot_download(
|
| 66 |
+
"public-data/Unique3D",
|
| 67 |
+
repo_type="model",
|
| 68 |
+
local_dir=str(ckpt_dir),
|
| 69 |
+
token=os.getenv("HF_TOKEN") # Add token if needed
|
| 70 |
+
)
|
| 71 |
+
break
|
| 72 |
+
except HfHubError as e:
|
| 73 |
+
if attempt == max_retries - 1:
|
| 74 |
+
logger.error(f"Failed to download model after {max_retries} attempts: {str(e)}")
|
| 75 |
+
raise
|
| 76 |
+
logger.warning(f"Download attempt {attempt + 1} failed, retrying...")
|
| 77 |
+
continue
|
| 78 |
+
|
| 79 |
+
logger.info("Model checkpoints downloaded successfully")
|
| 80 |
+
|
| 81 |
+
# Configure PyTorch settings
|
| 82 |
+
torch.set_float32_matmul_precision('medium')
|
| 83 |
+
torch.backends.cuda.matmul.allow_tf32 = True
|
| 84 |
+
torch.set_grad_enabled(False)
|
| 85 |
+
|
| 86 |
+
logger.info("PyTorch configured successfully")
|
| 87 |
+
except Exception as e:
|
| 88 |
+
logger.error(f"Error during model setup: {str(e)}")
|
| 89 |
+
raise
|
| 90 |
|
| 91 |
# Application title
|
| 92 |
_TITLE = 'Text to 3D'
|
| 93 |
|
| 94 |
def launch():
|
| 95 |
+
"""Launch the Gradio interface"""
|
| 96 |
+
try:
|
| 97 |
+
logger.info("Initializing models...")
|
| 98 |
+
model_zoo.init_models()
|
| 99 |
+
|
| 100 |
+
logger.info("Creating Gradio interface...")
|
| 101 |
+
with gr.Blocks(title=_TITLE) as demo:
|
| 102 |
+
with gr.Row():
|
| 103 |
+
with gr.Column(scale=1):
|
| 104 |
+
gr.Markdown('# ' + _TITLE)
|
| 105 |
+
create_3d_ui("wkl")
|
| 106 |
+
|
| 107 |
+
demo.queue().launch(share=True)
|
| 108 |
+
except Exception as e:
|
| 109 |
+
logger.error(f"Error launching application: {str(e)}")
|
| 110 |
+
raise
|
| 111 |
|
| 112 |
if __name__ == '__main__':
|
| 113 |
+
try:
|
| 114 |
+
logger.info("Starting application setup...")
|
| 115 |
+
setup_dependencies()
|
| 116 |
+
sys.path.append(os.curdir)
|
| 117 |
+
setup_model()
|
| 118 |
+
fire.Fire(launch)
|
| 119 |
+
except Exception as e:
|
| 120 |
+
logger.error(f"Application startup failed: {str(e)}")
|
| 121 |
+
sys.exit(1)
|