pdufour commited on
Commit
5b280f8
·
verified ·
1 Parent(s): 435210d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -13
app.py CHANGED
@@ -1,9 +1,12 @@
1
- import streamlit as st
2
- from huggingface_hub import HfApi
3
  import os
4
  import urllib.request
5
  import subprocess
6
  import tarfile
 
 
 
 
7
 
8
  HF_TOKEN = st.secrets.get("HF_TOKEN") or os.environ.get("HF_TOKEN")
9
  HF_USERNAME = (
@@ -12,8 +15,10 @@ HF_USERNAME = (
12
  or os.environ.get("SPACE_AUTHOR_NAME")
13
  )
14
 
15
- TRANSFORMERS_REPOSITORY_REVISION = "v3"
16
- TRANSFORMERS_REPOSITORY_URL = f"https://github.com/xenova/transformers.js/archive/refs/heads/{TRANSFORMERS_REPOSITORY_REVISION}.tar.gz"
 
 
17
  TRANSFORMERS_REPOSITORY_PATH = "./transformers.js"
18
  ARCHIVE_PATH = f"./transformers_{TRANSFORMERS_REPOSITORY_REVISION}.tar.gz"
19
  HF_BASE_URL = "https://huggingface.co"
@@ -23,15 +28,18 @@ if not os.path.exists(TRANSFORMERS_REPOSITORY_PATH):
23
  print(f"Downloading the repository from {TRANSFORMERS_REPOSITORY_URL}...")
24
  urllib.request.urlretrieve(TRANSFORMERS_REPOSITORY_URL, ARCHIVE_PATH)
25
 
26
- # Extract the .tar.gz file
27
- print(f"Extracting the archive {ARCHIVE_PATH}...")
28
- with tarfile.open(ARCHIVE_PATH, "r:gz") as tar:
29
- tar.extractall()
30
-
31
- # Rename the extracted folder to match the expected path
32
- # GitHub strips the leading 'v', so we handle that here
33
- extracted_folder = f"./transformers.js-{TRANSFORMERS_REPOSITORY_REVISION.lstrip('v')}"
34
- os.rename(extracted_folder, TRANSFORMERS_REPOSITORY_PATH)
 
 
 
35
 
36
  # Remove the downloaded .tar.gz archive
37
  os.remove(ARCHIVE_PATH)
 
1
+ import sys
 
2
  import os
3
  import urllib.request
4
  import subprocess
5
  import tarfile
6
+ import tempfile
7
+
8
+ import streamlit as st
9
+ from huggingface_hub import HfApi
10
 
11
  HF_TOKEN = st.secrets.get("HF_TOKEN") or os.environ.get("HF_TOKEN")
12
  HF_USERNAME = (
 
15
  or os.environ.get("SPACE_AUTHOR_NAME")
16
  )
17
 
18
+ TRANSFORMERS_BASE_URL = "https://github.com/xenova/transformers.js/archive/refs"
19
+ TRANSFORMERS_REPOSITORY_REVISION = "3.0.0"
20
+ TRANSFORMERS_REF_TYPE = "tags" if urllib.request.urlopen(f"{TRANSFORMERS_BASE_URL}/tags/{TRANSFORMERS_REPOSITORY_REVISION}.tar.gz").getcode() == 200 else "heads"
21
+ TRANSFORMERS_REPOSITORY_URL = f"{TRANSFORMERS_BASE_URL}/{TRANSFORMERS_REF_TYPE}/{TRANSFORMERS_REPOSITORY_REVISION}.tar.gz"
22
  TRANSFORMERS_REPOSITORY_PATH = "./transformers.js"
23
  ARCHIVE_PATH = f"./transformers_{TRANSFORMERS_REPOSITORY_REVISION}.tar.gz"
24
  HF_BASE_URL = "https://huggingface.co"
 
28
  print(f"Downloading the repository from {TRANSFORMERS_REPOSITORY_URL}...")
29
  urllib.request.urlretrieve(TRANSFORMERS_REPOSITORY_URL, ARCHIVE_PATH)
30
 
31
+ # Create a temporary directory for extraction
32
+ with tempfile.TemporaryDirectory() as tmp_dir:
33
+ # Extract the .tar.gz file to temp directory
34
+ print(f"Extracting the archive {ARCHIVE_PATH}...")
35
+ with tarfile.open(ARCHIVE_PATH, "r:gz") as tar:
36
+ tar.extractall(tmp_dir)
37
+
38
+ # Get the extracted folder name (there should be only one)
39
+ extracted_folder = os.path.join(tmp_dir, os.listdir(tmp_dir)[0])
40
+
41
+ # Move to final destination
42
+ os.rename(extracted_folder, TRANSFORMERS_REPOSITORY_PATH)
43
 
44
  # Remove the downloaded .tar.gz archive
45
  os.remove(ARCHIVE_PATH)