lijn14 commited on
Commit ·
190c9ce
1
Parent(s): d572bbd
修改clone地址
Browse files
notebooks/EasyTranslate_Production.ipynb
CHANGED
|
@@ -67,22 +67,81 @@
|
|
| 67 |
"metadata": {},
|
| 68 |
"outputs": [],
|
| 69 |
"source": [
|
| 70 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
"REPO_DIR = Path(\"/content/UCAS-EasyTranslate\") if IN_COLAB else Path(\".\").resolve()\n",
|
| 72 |
"\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
"if IN_COLAB:\n",
|
| 74 |
-
"
|
| 75 |
-
"
|
|
|
|
| 76 |
" else:\n",
|
| 77 |
-
"
|
| 78 |
-
"
|
| 79 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
"else:\n",
|
| 81 |
-
"
|
|
|
|
|
|
|
| 82 |
"\n",
|
| 83 |
-
"os.chdir(
|
| 84 |
-
"sys.path.insert(0, str(
|
| 85 |
-
"print(f\"Repository directory: {
|
| 86 |
]
|
| 87 |
},
|
| 88 |
{
|
|
|
|
| 67 |
"metadata": {},
|
| 68 |
"outputs": [],
|
| 69 |
"source": [
|
| 70 |
+
"# You can override this in Colab before running this cell:\n",
|
| 71 |
+
"# os.environ[\"EASYTRANSLATE_REPO_URL\"] = \"https://github.com/<org>/<repo>.git\"\n",
|
| 72 |
+
"REPO_URL = os.environ.get(\n",
|
| 73 |
+
" \"EASYTRANSLATE_REPO_URL\",\n",
|
| 74 |
+
" \"https://huggingface.co/sdfjliom/UCAS-EasyTranslate\",\n",
|
| 75 |
+
")\n",
|
| 76 |
"REPO_DIR = Path(\"/content/UCAS-EasyTranslate\") if IN_COLAB else Path(\".\").resolve()\n",
|
| 77 |
"\n",
|
| 78 |
+
"\n",
|
| 79 |
+
"def _is_repo_root(path: Path) -> bool:\n",
|
| 80 |
+
" return (path / \"src\" / \"easytranslate\").exists() and (path / \"setup.py\").exists()\n",
|
| 81 |
+
"\n",
|
| 82 |
+
"\n",
|
| 83 |
+
"def _find_repo_root(start_path: Path) -> Path | None:\n",
|
| 84 |
+
" p = start_path.resolve()\n",
|
| 85 |
+
" for candidate in [p] + list(p.parents):\n",
|
| 86 |
+
" if _is_repo_root(candidate):\n",
|
| 87 |
+
" return candidate\n",
|
| 88 |
+
" return None\n",
|
| 89 |
+
"\n",
|
| 90 |
+
"\n",
|
| 91 |
+
"resolved_repo: Path | None = None\n",
|
| 92 |
+
"\n",
|
| 93 |
"if IN_COLAB:\n",
|
| 94 |
+
" # 1) If already present, use it directly\n",
|
| 95 |
+
" if _is_repo_root(REPO_DIR):\n",
|
| 96 |
+
" resolved_repo = REPO_DIR\n",
|
| 97 |
" else:\n",
|
| 98 |
+
" # 2) Try clone (public repo expected). If private/auth fails, continue to fallbacks.\n",
|
| 99 |
+
" try:\n",
|
| 100 |
+
" subprocess.run(\n",
|
| 101 |
+
" [\"git\", \"clone\", REPO_URL, str(REPO_DIR)],\n",
|
| 102 |
+
" check=True,\n",
|
| 103 |
+
" stdout=subprocess.PIPE,\n",
|
| 104 |
+
" stderr=subprocess.STDOUT,\n",
|
| 105 |
+
" text=True,\n",
|
| 106 |
+
" )\n",
|
| 107 |
+
" resolved_repo = REPO_DIR\n",
|
| 108 |
+
" print(f\"Cloned repository to: {REPO_DIR}\")\n",
|
| 109 |
+
" except subprocess.CalledProcessError as e:\n",
|
| 110 |
+
" print(\"Clone failed. This usually means repo is private or URL is incorrect.\")\n",
|
| 111 |
+
" print(\"Git output:\")\n",
|
| 112 |
+
" print(e.stdout)\n",
|
| 113 |
+
"\n",
|
| 114 |
+
" # 3) Fallback: try to locate an existing copy (e.g., from Drive or current working dir)\n",
|
| 115 |
+
" if resolved_repo is None:\n",
|
| 116 |
+
" candidate_paths = [\n",
|
| 117 |
+
" Path.cwd(),\n",
|
| 118 |
+
" Path(\"/content\"),\n",
|
| 119 |
+
" Path(\"/content/drive/MyDrive/UCAS-EasyTranslate\"),\n",
|
| 120 |
+
" Path(\"/content/drive/MyDrive/Colab Notebooks/UCAS-EasyTranslate\"),\n",
|
| 121 |
+
" ]\n",
|
| 122 |
+
" for candidate in candidate_paths:\n",
|
| 123 |
+
" root = _find_repo_root(candidate)\n",
|
| 124 |
+
" if root is not None:\n",
|
| 125 |
+
" resolved_repo = root\n",
|
| 126 |
+
" print(f\"Using existing repository at: {resolved_repo}\")\n",
|
| 127 |
+
" break\n",
|
| 128 |
+
"\n",
|
| 129 |
+
" if resolved_repo is None:\n",
|
| 130 |
+
" raise FileNotFoundError(\n",
|
| 131 |
+
" \"Cannot locate UCAS-EasyTranslate repository. \"\n",
|
| 132 |
+
" \"Please do one of these: \"\n",
|
| 133 |
+
" \"(1) set a public/correct EASYTRANSLATE_REPO_URL, \"\n",
|
| 134 |
+
" \"(2) clone repo to /content/UCAS-EasyTranslate, \"\n",
|
| 135 |
+
" \"(3) place repo in Google Drive and mount Drive.\"\n",
|
| 136 |
+
" )\n",
|
| 137 |
"else:\n",
|
| 138 |
+
" # Local: use nearest project root from current path\n",
|
| 139 |
+
" resolved_repo = _find_repo_root(Path.cwd()) or Path.cwd()\n",
|
| 140 |
+
" print(f\"Using local repository at: {resolved_repo}\")\n",
|
| 141 |
"\n",
|
| 142 |
+
"os.chdir(resolved_repo)\n",
|
| 143 |
+
"sys.path.insert(0, str(Path(resolved_repo) / \"src\"))\n",
|
| 144 |
+
"print(f\"Repository directory: {resolved_repo}\")"
|
| 145 |
]
|
| 146 |
},
|
| 147 |
{
|