ouob commited on
Commit
e0096e7
·
1 Parent(s): efd9a2d

feat: pull or clone

Browse files
Files changed (3) hide show
  1. app.py +51 -23
  2. docker-compose.yml +2 -6
  3. requirements.txt +1 -0
app.py CHANGED
@@ -1,37 +1,65 @@
1
  import os
 
2
  import git
3
  import subprocess
4
- # from python-dotenv import load_dotenv
5
- from git.exc import GitCommandError
6
 
 
 
 
 
 
7
 
8
- def clone_and_run():
9
- # 設定環境變數中的倉庫地址和憑證
10
- user = os.getenv('HF_USER')
11
- repo_url = os.getenv('HF_REPO_URL')
12
- access_token = os.getenv('HF_ACCESS_TOKEN')
13
 
14
- if not repo_url or not access_token:
15
- raise EnvironmentError("請設定環境變數中的 HUGGING_FACE_REPO_URL HUGGING_FACE_ACCESS_TOKEN")
 
16
 
17
- # 構建帶有憑證的 URL
18
- auth_repo_url = repo_url.replace('https://', f'https://{user}:{access_token}@')
19
 
 
 
20
  try:
21
- # 克隆倉庫
22
- git.Repo.clone_from(auth_repo_url, 'repo', branch='main')
23
- print("倉庫克隆成功!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
- os.chdir(os.path.join(os.getcwd(), 'repo'))
26
- subprocess.run(["python", "app.py"], check=True)
27
 
28
- print("app.py 運行成功!")
 
 
 
 
29
 
30
- except GitCommandError as e:
31
- print(f"克隆倉庫時出錯:{e}")
32
- except subprocess.CalledProcessError as e:
33
- print(f"運行 app.py 時出錯:{e}")
34
 
35
  if __name__ == "__main__":
36
- # load_dotenv()
37
- clone_and_run()
 
1
  import os
2
+ import shutil
3
  import git
4
  import subprocess
 
 
5
 
6
+ REPO_DIR = 'repo'
7
+ USER = os.getenv('HF_USER')
8
+ REPO_URL = os.getenv('HF_REPO_URL')
9
+ ACCESS_TOKEN = os.getenv('HF_ACCESS_TOKEN')
10
+ AUTH_REPO_URL = REPO_URL.replace('https://', f'https://{USER}:{ACCESS_TOKEN}@')
11
 
 
 
 
 
 
12
 
13
+ def check_envs():
14
+ envs = [USER, REPO_URL, ACCESS_TOKEN]
15
+ missing_vars = [var for var in envs if var is None]
16
 
17
+ if missing_vars:
18
+ raise EnvironmentError(f"Missing environment variables: {', '.join(missing_vars)}")
19
 
20
+
21
+ def pull() -> int:
22
  try:
23
+ repo = git.Repo(REPO_DIR)
24
+ repo.remotes.origin.pull()
25
+ return 200
26
+ except git.exc.InvalidGitRepositoryError:
27
+ # 404
28
+ raise ValueError('Invalid Git repository.')
29
+
30
+ except git.exc.NoSuchPathError:
31
+ return 404
32
+
33
+ except git.ecx.GitCommandError as e:
34
+ print(f"拉取代碼時出錯:{e}")
35
+ return 500
36
+
37
+
38
+ def clone(status_code):
39
+ if status_code == 200:
40
+ return
41
+ if status_code == 500:
42
+ if os.path.exists(REPO_DIR):
43
+ shutil.rmtree(REPO_DIR)
44
+
45
+ try:
46
+ git.Repo.clone_from(AUTH_REPO_URL, REPO_DIR, branch='main')
47
+ except git.exc.GitCommandError as e:
48
+ print(f"克隆代碼時出錯:{e}")
49
+ raise e
50
+
51
+
52
+ def run():
53
+ os.chdir(os.path.join(os.getcwd(), REPO_DIR))
54
+ subprocess.run(["python", "app.py"], check=True)
55
 
 
 
56
 
57
+ def main():
58
+ check_envs()
59
+ status_code = pull()
60
+ clone(status_code)
61
+ run()
62
 
 
 
 
 
63
 
64
  if __name__ == "__main__":
65
+ main()
 
docker-compose.yml CHANGED
@@ -8,15 +8,11 @@ services:
8
  - nginx-bridge
9
  environment:
10
  - CUDA_VISIBLE_DEVICES=all
11
- # - LETSENCRYPT_HOST=vits-hat-tts.gohakka.org
12
- # - VIRTUAL_HOST=vits-hat-tts.gohakka.org
13
  - VIRTUAL_PORT=7860
14
  ports:
15
  - 7862:7860
16
- # expose:
17
- # - "7860"
18
- # env_file:
19
- # - .env
20
 
21
  networks:
22
  nginx-bridge:
 
8
  - nginx-bridge
9
  environment:
10
  - CUDA_VISIBLE_DEVICES=all
 
 
11
  - VIRTUAL_PORT=7860
12
  ports:
13
  - 7862:7860
14
+ env_file:
15
+ - .env
 
 
16
 
17
  networks:
18
  nginx-bridge:
requirements.txt CHANGED
@@ -4,6 +4,7 @@ jieba
4
  opencc
5
  TTS
6
  omegaconf
 
7
  # interface
8
  python-dotenv
9
  pyjwt
 
4
  opencc
5
  TTS
6
  omegaconf
7
+ spaces
8
  # interface
9
  python-dotenv
10
  pyjwt