TNOT commited on
Commit
48e9460
·
1 Parent(s): 52db525

fix: harden micromamba download for cloud startup

Browse files
Files changed (1) hide show
  1. app.py +64 -10
app.py CHANGED
@@ -162,19 +162,73 @@ def setup_mfa_linux():
162
  mamba_root = Path("/tmp/micromamba")
163
  mamba_bin = mamba_root / "bin" / "micromamba"
164
  mfa_env = mamba_root / "envs" / "mfa"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
165
 
166
  try:
167
  # 1. 安装 micromamba(如果不存在)
168
  if not mamba_bin.exists():
169
  logger.info("下载 micromamba...")
170
- mamba_root.mkdir(parents=True, exist_ok=True)
171
-
172
- # 下载并安装 micromamba
173
- subprocess.run([
174
- "bash", "-c",
175
- f'curl -Ls https://micro.mamba.pm/api/micromamba/linux-64/latest | tar -xvj -C {mamba_root} bin/micromamba'
176
- ], check=True, capture_output=True, timeout=120)
177
- logger.info("micromamba 下载完成")
178
 
179
  # 2. 使用 micromamba 创建环境并安装 MFA
180
  mfa_bin_path = mfa_env / "bin" / "mfa"
@@ -208,7 +262,7 @@ def setup_mfa_linux():
208
  "python=3.11",
209
  "montreal-forced-aligner",
210
  "-y"
211
- ], env=env, check=True, capture_output=True, text=True, timeout=600)
212
 
213
  # 更新确保使用 CPU 版本的 kaldi
214
  subprocess.run([
@@ -216,7 +270,7 @@ def setup_mfa_linux():
216
  "-c", "conda-forge",
217
  "kalpy", "kaldi=*=cpu*",
218
  "-y"
219
- ], env=env, capture_output=True, text=True, timeout=300)
220
 
221
  logger.info("MFA 安装完成")
222
 
 
162
  mamba_root = Path("/tmp/micromamba")
163
  mamba_bin = mamba_root / "bin" / "micromamba"
164
  mfa_env = mamba_root / "envs" / "mfa"
165
+
166
+ def ensure_micromamba_downloaded() -> None:
167
+ """下载 micromamba,支持多种方式重试,降低云端网络抖动影响"""
168
+ if mamba_bin.exists():
169
+ return
170
+
171
+ mamba_root.mkdir(parents=True, exist_ok=True)
172
+
173
+ download_attempts = [
174
+ {
175
+ "name": "micro.mamba.pm (curl)",
176
+ "cmd": [
177
+ "bash", "-lc",
178
+ f"curl --retry 3 --retry-delay 2 -fLsS https://micro.mamba.pm/api/micromamba/linux-64/latest | tar -xvj -C {mamba_root} bin/micromamba"
179
+ ],
180
+ "timeout": 420,
181
+ },
182
+ {
183
+ "name": "micro.mamba.pm (wget)",
184
+ "cmd": [
185
+ "bash", "-lc",
186
+ f"wget -qO- https://micro.mamba.pm/api/micromamba/linux-64/latest | tar -xvj -C {mamba_root} bin/micromamba"
187
+ ],
188
+ "timeout": 420,
189
+ },
190
+ {
191
+ "name": "GitHub release fallback",
192
+ "cmd": [
193
+ "bash", "-lc",
194
+ f"mkdir -p {mamba_root}/bin && curl --retry 3 --retry-delay 2 -fLsS -o {mamba_bin} https://github.com/mamba-org/micromamba-releases/releases/latest/download/micromamba-linux-64 && chmod +x {mamba_bin}"
195
+ ],
196
+ "timeout": 420,
197
+ },
198
+ ]
199
+
200
+ last_error = ""
201
+ for attempt in download_attempts:
202
+ logger.info(f"下载 micromamba 尝试: {attempt['name']}")
203
+ try:
204
+ result = subprocess.run(
205
+ attempt["cmd"],
206
+ capture_output=True,
207
+ text=True,
208
+ timeout=attempt["timeout"],
209
+ check=True,
210
+ )
211
+ if mamba_bin.exists():
212
+ logger.info("micromamba 下载完成")
213
+ return
214
+ last_error = (result.stderr or result.stdout or "下载结束但未生成可执行文件")[-500:]
215
+ except subprocess.TimeoutExpired as e:
216
+ last_error = f"超时: {e}"
217
+ logger.warning(f"{attempt['name']} 下载超时")
218
+ except subprocess.CalledProcessError as e:
219
+ last_error = (e.stderr or e.stdout or str(e))[-500:]
220
+ logger.warning(f"{attempt['name']} 下载失败")
221
+ except Exception as e:
222
+ last_error = str(e)
223
+ logger.warning(f"{attempt['name']} 下载异常: {e}")
224
+
225
+ raise RuntimeError(f"micromamba 下载失败: {last_error}")
226
 
227
  try:
228
  # 1. 安装 micromamba(如果不存在)
229
  if not mamba_bin.exists():
230
  logger.info("下载 micromamba...")
231
+ ensure_micromamba_downloaded()
 
 
 
 
 
 
 
232
 
233
  # 2. 使用 micromamba 创建环境并安装 MFA
234
  mfa_bin_path = mfa_env / "bin" / "mfa"
 
262
  "python=3.11",
263
  "montreal-forced-aligner",
264
  "-y"
265
+ ], env=env, check=True, capture_output=True, text=True, timeout=1800)
266
 
267
  # 更新确保使用 CPU 版本的 kaldi
268
  subprocess.run([
 
270
  "-c", "conda-forge",
271
  "kalpy", "kaldi=*=cpu*",
272
  "-y"
273
+ ], env=env, capture_output=True, text=True, timeout=900)
274
 
275
  logger.info("MFA 安装完成")
276