Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,9 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from huggingface_hub import HfApi
|
| 3 |
import os
|
|
|
|
| 4 |
import subprocess
|
|
|
|
| 5 |
|
| 6 |
HF_TOKEN = st.secrets.get("HF_TOKEN") or os.environ.get("HF_TOKEN")
|
| 7 |
HF_USERNAME = (
|
|
@@ -9,18 +11,32 @@ HF_USERNAME = (
|
|
| 9 |
or os.environ.get("HF_USERNAME")
|
| 10 |
or os.environ.get("SPACE_AUTHOR_NAME")
|
| 11 |
)
|
| 12 |
-
|
| 13 |
-
TRANSFORMERS_REPOSITORY_REVISION = "
|
|
|
|
| 14 |
TRANSFORMERS_REPOSITORY_PATH = "./transformers.js"
|
|
|
|
| 15 |
HF_BASE_URL = "https://huggingface.co"
|
| 16 |
|
| 17 |
if not os.path.exists(TRANSFORMERS_REPOSITORY_PATH):
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
st.write("## Convert a HuggingFace model to ONNX")
|
| 25 |
|
| 26 |
input_model_id = st.text_input(
|
|
@@ -62,6 +78,15 @@ if input_model_id:
|
|
| 62 |
capture_output=True,
|
| 63 |
text=True,
|
| 64 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
model_folder_path = (
|
| 67 |
f"{TRANSFORMERS_REPOSITORY_PATH}/models/{input_model_id}"
|
|
@@ -103,4 +128,4 @@ if input_model_id:
|
|
| 103 |
st.write("You can now go and view the model on HuggingFace!")
|
| 104 |
st.link_button(
|
| 105 |
f"Go to {output_model_id}", output_model_url, type="primary"
|
| 106 |
-
)
|
|
|
|
| 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 = (
|
|
|
|
| 11 |
or os.environ.get("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"
|
| 20 |
|
| 21 |
if not os.path.exists(TRANSFORMERS_REPOSITORY_PATH):
|
| 22 |
+
# Download the .tar.gz file
|
| 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)
|
| 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(
|
|
|
|
| 78 |
capture_output=True,
|
| 79 |
text=True,
|
| 80 |
)
|
| 81 |
+
|
| 82 |
+
# Log the script output
|
| 83 |
+
print("### Script Output ###")
|
| 84 |
+
print(output.stdout)
|
| 85 |
+
|
| 86 |
+
# Log any errors
|
| 87 |
+
if output.stderr:
|
| 88 |
+
print("### Script Errors ###")
|
| 89 |
+
print(output.stderr)
|
| 90 |
|
| 91 |
model_folder_path = (
|
| 92 |
f"{TRANSFORMERS_REPOSITORY_PATH}/models/{input_model_id}"
|
|
|
|
| 128 |
st.write("You can now go and view the model on HuggingFace!")
|
| 129 |
st.link_button(
|
| 130 |
f"Go to {output_model_id}", output_model_url, type="primary"
|
| 131 |
+
)
|