|
|
""" |
|
|
Three-View-Style-Embedder - Hugging Face Space App |
|
|
Running on ZeroGPU with automatic model downloading. |
|
|
""" |
|
|
import os |
|
|
import sys |
|
|
from pathlib import Path |
|
|
|
|
|
|
|
|
try: |
|
|
import spaces |
|
|
import huggingface_hub |
|
|
except ImportError: |
|
|
print("Installing dependencies...") |
|
|
os.system("pip install spaces huggingface_hub") |
|
|
import spaces |
|
|
from huggingface_hub import hf_hub_download |
|
|
else: |
|
|
from huggingface_hub import hf_hub_download |
|
|
|
|
|
from inference_utils import StyleEmbedderApp |
|
|
|
|
|
|
|
|
REPO_ID = "ij/Three-View-Style-Embedder-Combined" |
|
|
MODEL_FILENAME = "best_model.pt" |
|
|
EMBEDDINGS_FILENAME = "artist_embeddings.npz" |
|
|
YOLO_WEIGHTS_FILENAME = "yolov5x_anime.pt" |
|
|
|
|
|
def download_files(): |
|
|
print(f"Downloading models from {REPO_ID}...") |
|
|
|
|
|
|
|
|
try: |
|
|
model_path = hf_hub_download(repo_id=REPO_ID, filename=MODEL_FILENAME) |
|
|
print(f"Model downloaded to {model_path}") |
|
|
except Exception as e: |
|
|
raise RuntimeError(f"Failed to download model: {e}") |
|
|
|
|
|
|
|
|
try: |
|
|
embeddings_path = hf_hub_download(repo_id=REPO_ID, filename=EMBEDDINGS_FILENAME) |
|
|
print(f"Embeddings downloaded to {embeddings_path}") |
|
|
except Exception as e: |
|
|
raise RuntimeError(f"Failed to download embeddings: {e}") |
|
|
|
|
|
|
|
|
yolo_path = None |
|
|
try: |
|
|
|
|
|
if os.path.exists(YOLO_WEIGHTS_FILENAME): |
|
|
yolo_path = os.path.abspath(YOLO_WEIGHTS_FILENAME) |
|
|
print(f"Using local YOLO weights: {yolo_path}") |
|
|
else: |
|
|
|
|
|
print("Downloading YOLO weights...") |
|
|
yolo_path = hf_hub_download(repo_id=REPO_ID, filename=YOLO_WEIGHTS_FILENAME) |
|
|
print(f"YOLO weights downloaded to {yolo_path}") |
|
|
except Exception as e: |
|
|
print(f"Warning: Failed to download YOLO weights ({e}). Will attempt to use default/local if available.") |
|
|
|
|
|
yolo_path = None |
|
|
|
|
|
return model_path, embeddings_path, yolo_path |
|
|
|
|
|
def main(): |
|
|
|
|
|
model_path, embeddings_path, yolo_path = download_files() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print("Initializing Application...") |
|
|
app = StyleEmbedderApp( |
|
|
checkpoint_path=model_path, |
|
|
embeddings_path=embeddings_path, |
|
|
device='cuda', |
|
|
detector_device='cpu', |
|
|
yolo_weights=yolo_path |
|
|
) |
|
|
|
|
|
|
|
|
print("Launching UI...") |
|
|
demo = app.create_ui() |
|
|
demo.launch() |
|
|
|
|
|
if __name__ == "__main__": |
|
|
main() |
|
|
|