Reformat file
Browse files
app.py
CHANGED
@@ -1,9 +1,9 @@
|
|
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
|
@@ -17,7 +17,14 @@ HF_USERNAME = (
|
|
17 |
|
18 |
TRANSFORMERS_BASE_URL = "https://github.com/xenova/transformers.js/archive/refs"
|
19 |
TRANSFORMERS_REPOSITORY_REVISION = "3.0.0"
|
20 |
-
TRANSFORMERS_REF_TYPE =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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"
|
@@ -25,18 +32,18 @@ HF_BASE_URL = "https://huggingface.co"
|
|
25 |
|
26 |
if not os.path.exists(TRANSFORMERS_REPOSITORY_PATH):
|
27 |
urllib.request.urlretrieve(TRANSFORMERS_REPOSITORY_URL, ARCHIVE_PATH)
|
28 |
-
|
29 |
with tempfile.TemporaryDirectory() as tmp_dir:
|
30 |
with tarfile.open(ARCHIVE_PATH, "r:gz") as tar:
|
31 |
tar.extractall(tmp_dir)
|
32 |
-
|
33 |
extracted_folder = os.path.join(tmp_dir, os.listdir(tmp_dir)[0])
|
34 |
-
|
35 |
os.rename(extracted_folder, TRANSFORMERS_REPOSITORY_PATH)
|
36 |
-
|
37 |
os.remove(ARCHIVE_PATH)
|
38 |
print("Repository downloaded and extracted successfully.")
|
39 |
-
|
40 |
st.write("## Convert a HuggingFace model to ONNX")
|
41 |
|
42 |
input_model_id = st.text_input(
|
@@ -79,11 +86,11 @@ if input_model_id:
|
|
79 |
text=True,
|
80 |
env={},
|
81 |
)
|
82 |
-
|
83 |
# Log the script output
|
84 |
print("### Script Output ###")
|
85 |
print(output.stdout)
|
86 |
-
|
87 |
# Log any errors
|
88 |
if output.stderr:
|
89 |
print("### Script Errors ###")
|
@@ -129,4 +136,4 @@ if input_model_id:
|
|
129 |
st.write("You can now go and view the model on HuggingFace!")
|
130 |
st.link_button(
|
131 |
f"Go to {output_model_id}", output_model_url, type="primary"
|
132 |
-
)
|
|
|
|
|
1 |
import os
|
|
|
2 |
import subprocess
|
3 |
+
import sys
|
4 |
import tarfile
|
5 |
import tempfile
|
6 |
+
import urllib.request
|
7 |
|
8 |
import streamlit as st
|
9 |
from huggingface_hub import HfApi
|
|
|
17 |
|
18 |
TRANSFORMERS_BASE_URL = "https://github.com/xenova/transformers.js/archive/refs"
|
19 |
TRANSFORMERS_REPOSITORY_REVISION = "3.0.0"
|
20 |
+
TRANSFORMERS_REF_TYPE = (
|
21 |
+
"tags"
|
22 |
+
if urllib.request.urlopen(
|
23 |
+
f"{TRANSFORMERS_BASE_URL}/tags/{TRANSFORMERS_REPOSITORY_REVISION}.tar.gz"
|
24 |
+
).getcode()
|
25 |
+
== 200
|
26 |
+
else "heads"
|
27 |
+
)
|
28 |
TRANSFORMERS_REPOSITORY_URL = f"{TRANSFORMERS_BASE_URL}/{TRANSFORMERS_REF_TYPE}/{TRANSFORMERS_REPOSITORY_REVISION}.tar.gz"
|
29 |
TRANSFORMERS_REPOSITORY_PATH = "./transformers.js"
|
30 |
ARCHIVE_PATH = f"./transformers_{TRANSFORMERS_REPOSITORY_REVISION}.tar.gz"
|
|
|
32 |
|
33 |
if not os.path.exists(TRANSFORMERS_REPOSITORY_PATH):
|
34 |
urllib.request.urlretrieve(TRANSFORMERS_REPOSITORY_URL, ARCHIVE_PATH)
|
35 |
+
|
36 |
with tempfile.TemporaryDirectory() as tmp_dir:
|
37 |
with tarfile.open(ARCHIVE_PATH, "r:gz") as tar:
|
38 |
tar.extractall(tmp_dir)
|
39 |
+
|
40 |
extracted_folder = os.path.join(tmp_dir, os.listdir(tmp_dir)[0])
|
41 |
+
|
42 |
os.rename(extracted_folder, TRANSFORMERS_REPOSITORY_PATH)
|
43 |
+
|
44 |
os.remove(ARCHIVE_PATH)
|
45 |
print("Repository downloaded and extracted successfully.")
|
46 |
+
|
47 |
st.write("## Convert a HuggingFace model to ONNX")
|
48 |
|
49 |
input_model_id = st.text_input(
|
|
|
86 |
text=True,
|
87 |
env={},
|
88 |
)
|
89 |
+
|
90 |
# Log the script output
|
91 |
print("### Script Output ###")
|
92 |
print(output.stdout)
|
93 |
+
|
94 |
# Log any errors
|
95 |
if output.stderr:
|
96 |
print("### Script Errors ###")
|
|
|
136 |
st.write("You can now go and view the model on HuggingFace!")
|
137 |
st.link_button(
|
138 |
f"Go to {output_model_id}", output_model_url, type="primary"
|
139 |
+
)
|