Spaces:
Paused
Paused
Upload app.py with huggingface_hub
Browse files524 unmodified lines525526527528529530531532533534535536537538539540541542543544545546547548549550551524 unmodified linesreturn Nonedef _mf_dl_and_upload(job_id, dl_url, fname, base_status):tmp_base=f"/tmp/mf_{job_id}"os.makedirs(tmp_base, exist_ok=True)tmp_path=os.path.join(tmp_base, fname)try:cmd=["aria2c","-x","16","-s","16","-k","1M","-o",tmp_path,dl_url,"--allow-overwrite=true","--auto-file-renaming=false","--console-log-level=warn"]proc=subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)except FileNotFoundError:cmd=["curl","-L","-o",tmp_path,dl_url]proc=subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)while proc.poll() is None:try:cur=os.path.getsize(tmp_path) if os.path.exists(tmp_path) else 0jobs[job_id]=dict(jobs.get(job_id,{}))jobs[job_id]["status"]=f"{base_status} {cur//1024//1024}MB"except Exception:passtime.sleep(1)try: err=(proc.stderr.read() or b"")[:400].decode("utf-8","ignore")except Exception: err=""if proc.returncode!=0 or not os.path.exists(tmp_path) or os.path.getsize(tmp_path)==0:jobs[job_id]["status"]=f"{base_status} → Failed({proc.returncode}) {err.strip()[-160:]}"try: shutil.rmtree(tmp_base)except: passreturn False524 unmodified lines525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557524 unmodified linesreturn Nonedef _mf_dl_and_upload(job_id, dl_url, fname, base_status):import requeststmp_base=f"/tmp/mf_{job_id}"os.makedirs(tmp_base, exist_ok=True)tmp_path=os.path.join(tmp_base, fname)try:r=requests.get(dl_url, headers={"User-Agent":MF_UA}, stream=True, timeout=60, allow_redirects=True)if r.status_code!=200:jobs[job_id]["status"]=f"{base_status} → HTTP {r.status_code} {r.headers.get('content-type','')[:40]} len={r.headers.get('content-length','?')}"try: r.close()except: passreturn Falseclen=int(r.headers.get("content-length",0) or 0)written=0with open(tmp_path,"wb") as out:for chunk in r.iter_content(chunk_size=8*1024*1024):if not chunk: continueout.write(chunk); written+=len(chunk)if written%(64*1024*1024)==0:jobs[job_id]=dict(jobs.get(job_id,{}))jobs[job_id]["status"]=f"{base_status} {written//1024//1024}MB"+(f"/{clen//1024//1024}MB" if clen else "")try: r.close()except: passif written==0:jobs[job_id]["status"]=f"{base_status} → empty body (0 bytes)"return Falseexcept Exception as e:jobs[job_id]["status"]=f"{base_status} → requests err: {str(e)[:160]}"try: shutil.rmtree(tmp_base)except: passreturn False
app.py+23-17