mirror of
https://github.com/SakanaAI/doc-to-lora.git
synced 2026-07-26 17:11:02 +02:00
Clean up symlink path bugs and add robust makedirs in demo.ipynb
This commit is contained in:
parent
72e9766f62
commit
0b639c84a4
1 changed files with 72 additions and 8 deletions
80
demo.ipynb
80
demo.ipynb
|
|
@ -41,9 +41,10 @@
|
|||
"except ImportError:\n",
|
||||
" IN_COLAB = False\n",
|
||||
"\n",
|
||||
"import os\n",
|
||||
"\n",
|
||||
"if IN_COLAB:\n",
|
||||
" print(\"Running in Google Colab. Setting up workspace and website repositories...\")\n",
|
||||
" import os\n",
|
||||
" # 1. Clone the main Video2LoRA repository to absolute path\n",
|
||||
" if not os.path.exists(\"/content/Video2LoRA\"):\n",
|
||||
" os.system(\"git clone -b demo https://github.com/video2lora/Video2LoRA.git /content/Video2LoRA\")\n",
|
||||
|
|
@ -52,12 +53,48 @@
|
|||
" os.system(\"git clone https://github.com/video2lora/video2lora.github.io.git /content/video2lora.github.io\")\n",
|
||||
" # 3. Change directory to the codebase workspace\n",
|
||||
" os.chdir(\"/content/Video2LoRA\")\n",
|
||||
" # 4. Remove any existing/broken symlinks or folders to prevent conflicts\n",
|
||||
" if os.path.lexists(\"media\"):\n",
|
||||
" os.system(\"rm -rf media\")\n",
|
||||
" # 5. Create a symlink to the website's media directory so video paths resolve locally\n",
|
||||
" os.system(\"ln -s /content/video2lora.github.io/media media\")\n",
|
||||
" print(f\"Workspace directory changed to: {os.getcwd()}\")\n",
|
||||
"\n",
|
||||
"# Automatically set up the media symlink if the website repository is found nearby.\n",
|
||||
"# This ensures that both local and Colab environments can resolve paths correctly.\n",
|
||||
"if os.path.lexists(\"media\"):\n",
|
||||
" # If media exists but is a broken symlink or not a directory, remove it\n",
|
||||
" if not os.path.isdir(\"media\") or os.path.islink(\"media\"):\n",
|
||||
" print(\"Removing conflicting/broken media symlink or file...\")\n",
|
||||
" try:\n",
|
||||
" if os.path.islink(\"media\"):\n",
|
||||
" os.unlink(\"media\")\n",
|
||||
" else:\n",
|
||||
" import shutil\n",
|
||||
" shutil.rmtree(\"media\")\n",
|
||||
" except Exception as e:\n",
|
||||
" os.system(\"rm -rf media\")\n",
|
||||
"\n",
|
||||
"# Dynamically find the website's media directory\n",
|
||||
"website_media_path = None\n",
|
||||
"possible_paths = [\n",
|
||||
" \"/content/video2lora.github.io/media\",\n",
|
||||
" \"/content/Video2LoRA/video2lora.github.io/media\",\n",
|
||||
" \"video2lora.github.io/media\",\n",
|
||||
" \"../video2lora.github.io/media\",\n",
|
||||
" \"../../video2lora.github.io/media\",\n",
|
||||
"]\n",
|
||||
"for p in possible_paths:\n",
|
||||
" abs_p = os.path.abspath(p)\n",
|
||||
" if os.path.exists(abs_p) and os.path.isdir(abs_p):\n",
|
||||
" website_media_path = abs_p\n",
|
||||
" break\n",
|
||||
"\n",
|
||||
"if website_media_path:\n",
|
||||
" print(f\"Linking media directory from website repository at: {website_media_path}\")\n",
|
||||
" try:\n",
|
||||
" os.symlink(website_media_path, \"media\")\n",
|
||||
" except Exception as e:\n",
|
||||
" print(f\"Failed to create symlink using os.symlink, trying shell: {e}\")\n",
|
||||
" os.system(f\"ln -s {website_media_path} media\")\n",
|
||||
"else:\n",
|
||||
" print(\"Warning: Website media directory not found. Will download videos on demand.\")\n",
|
||||
"\n",
|
||||
"print(f\"Workspace directory: {os.getcwd()}\")\n",
|
||||
"\n",
|
||||
"# Install necessary packages\n",
|
||||
"!pip install torch torchvision torchaudio\n",
|
||||
|
|
@ -206,7 +243,34 @@
|
|||
"print(\"Checking/downloading qualitative benchmark video files from project website...\")\n",
|
||||
"for item in examples:\n",
|
||||
" video_path = item[\"video_path\"]\n",
|
||||
" os.makedirs(os.path.dirname(video_path), exist_ok=True)\n",
|
||||
" dir_name = os.path.dirname(video_path)\n",
|
||||
" \n",
|
||||
" # 1. Clean up any broken symlinks or conflicting files along the directories path\n",
|
||||
" # to avoid FileNotFoundError during folder creation.\n",
|
||||
" parts = video_path.split('/')\n",
|
||||
" current_path = \"\"\n",
|
||||
" for part in parts[:-1]: # skip the filename\n",
|
||||
" if current_path:\n",
|
||||
" current_path = os.path.join(current_path, part)\n",
|
||||
" else:\n",
|
||||
" current_path = part\n",
|
||||
" if os.path.lexists(current_path) and not os.path.isdir(current_path):\n",
|
||||
" print(f\"Removing conflict at '{current_path}' (broken link or file) to allow directory creation...\")\n",
|
||||
" try:\n",
|
||||
" if os.path.islink(current_path):\n",
|
||||
" os.unlink(current_path)\n",
|
||||
" else:\n",
|
||||
" os.remove(current_path)\n",
|
||||
" except Exception as e:\n",
|
||||
" print(f\"Failed to remove conflict at {current_path}: {e}\")\n",
|
||||
" \n",
|
||||
" # 2. Safely create directory structure\n",
|
||||
" try:\n",
|
||||
" os.makedirs(dir_name, exist_ok=True)\n",
|
||||
" except Exception as e:\n",
|
||||
" print(f\"Warning: Could not create directory {dir_name}: {e}\")\n",
|
||||
" \n",
|
||||
" # 3. Check and retrieve the video file\n",
|
||||
" if not os.path.exists(video_path):\n",
|
||||
" url = f\"https://video2lora.github.io/{video_path}\"\n",
|
||||
" print(f\"Downloading {video_path} from {url}...\")\n",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue