File size: 1,925 Bytes
8f0c86a
 
 
 
 
 
 
 
 
 
 
 
 
2da1e39
 
8f0c86a
 
 
 
2da1e39
8f0c86a
 
 
 
 
 
 
 
 
 
 
2da1e39
8f0c86a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""PP-OSR-H на Modal: serverless GPU, $30 кредита в месяц на Starter.

Подготовка (один раз):
    pip install modal
    modal token new                     # откроется браузер, логин
    modal secret create hf HF_TOKEN=hf_твой_токен

Запуск:
    modal run modal_pposr.py            # обучение
    modal run modal_pposr.py --images 30000 --steps 12000
"""
from __future__ import annotations

import os

import modal

APP = modal.App("pposr")
VOL = modal.Volume.from_name("pposr-vol", create_if_missing=True)
GPU = os.environ.get("MODAL_GPU", "T4")   # T4 дешевле; L4 может требовать карту

image = (
    modal.Image.debian_slim(python_version="3.11")
    .pip_install("torch==2.5.1", "transformers>=4.44,<5", "datasets>=2.20",
                 "pillow", "huggingface_hub>=0.24", "numpy", "pyyaml")
)

CODE_REPO = "AdminHugging/ppai-code"
MODEL_REPO = "AdminHugging/PP-OSR-Hydrogen"


@APP.function(image=image, gpu=GPU, timeout=60 * 60 * 4, volumes={"/vol": VOL},
              secrets=[modal.Secret.from_name("hf")])
def train(images: int = 30000, steps: int = 12000, hours: float = 3.5) -> str:
    import os
    import subprocess
    import sys

    os.environ["OSR_ROOT"] = "/vol/pposr"
    os.environ["OSR_IMAGES"] = str(images)
    os.environ["OSR_STEPS"] = str(steps)
    os.environ["OSR_HOURS"] = str(hours)

    from huggingface_hub import snapshot_download

    code = snapshot_download(CODE_REPO, repo_type="dataset", local_dir="/vol/code")
    print("код:", code, flush=True)

    script = os.path.join(code, "ppai", "colab_pposr.py")
    rc = subprocess.run([sys.executable, script]).returncode
    VOL.commit()
    return f"exit={rc}"


@APP.local_entrypoint()
def main(images: int = 30000, steps: int = 12000, hours: float = 3.5) -> None:
    print(train.remote(images=images, steps=steps, hours=hours))