Spaces:
Paused
Paused
Delete app.py
Browse files
app.py
DELETED
@@ -1,202 +0,0 @@
|
|
1 |
-
#Appsec
|
2 |
-
import logging
|
3 |
-
import os
|
4 |
-
import subprocess
|
5 |
-
import sys
|
6 |
-
from dataclasses import dataclass
|
7 |
-
from pathlib import Path
|
8 |
-
from typing import Optional, Tuple
|
9 |
-
from urllib.request import urlopen, urlretrieve
|
10 |
-
|
11 |
-
import streamlit as st
|
12 |
-
from huggingface_hub import HfApi, whoami
|
13 |
-
|
14 |
-
logging.basicConfig(level=logging.INFO)
|
15 |
-
logger = logging.getLogger(__name__)
|
16 |
-
|
17 |
-
|
18 |
-
@dataclass
|
19 |
-
class Config:
|
20 |
-
"""Application configuration."""
|
21 |
-
|
22 |
-
hf_token: str
|
23 |
-
hf_username: str
|
24 |
-
transformers_version: str = "3.0.0"
|
25 |
-
hf_base_url: str = "https://huggingface.co"
|
26 |
-
transformers_base_url: str = (
|
27 |
-
"https://github.com/xenova/transformers.js/archive/refs"
|
28 |
-
)
|
29 |
-
repo_path: Path = Path("./transformers.js")
|
30 |
-
|
31 |
-
@classmethod
|
32 |
-
def from_env(cls) -> "Config":
|
33 |
-
"""Create config from environment variables and secrets."""
|
34 |
-
system_token = st.secrets.get("HF_TOKEN")
|
35 |
-
user_token = st.session_state.get("user_hf_token")
|
36 |
-
if user_token:
|
37 |
-
hf_username = whoami(token=user_token)["name"]
|
38 |
-
else:
|
39 |
-
hf_username = (
|
40 |
-
os.getenv("SPACE_AUTHOR_NAME") or whoami(token=system_token)["name"]
|
41 |
-
)
|
42 |
-
hf_token = user_token or system_token
|
43 |
-
|
44 |
-
if not hf_token:
|
45 |
-
raise ValueError("HF_TOKEN must be set")
|
46 |
-
|
47 |
-
return cls(hf_token=hf_token, hf_username=hf_username)
|
48 |
-
|
49 |
-
|
50 |
-
class ModelConverter:
|
51 |
-
"""Handles model conversion and upload operations."""
|
52 |
-
|
53 |
-
def __init__(self, config: Config):
|
54 |
-
self.config = config
|
55 |
-
self.api = HfApi(token=config.hf_token)
|
56 |
-
|
57 |
-
def _get_ref_type(self) -> str:
|
58 |
-
"""Determine the reference type for the transformers repository."""
|
59 |
-
url = f"{self.config.transformers_base_url}/tags/{self.config.transformers_version}.tar.gz"
|
60 |
-
try:
|
61 |
-
return "tags" if urlopen(url).getcode() == 200 else "heads"
|
62 |
-
except Exception as e:
|
63 |
-
logger.warning(f"Failed to check tags, defaulting to heads: {e}")
|
64 |
-
return "heads"
|
65 |
-
|
66 |
-
def setup_repository(self) -> None:
|
67 |
-
"""Download and setup transformers repository if needed."""
|
68 |
-
if self.config.repo_path.exists():
|
69 |
-
return
|
70 |
-
|
71 |
-
ref_type = self._get_ref_type()
|
72 |
-
archive_url = f"{self.config.transformers_base_url}/{ref_type}/{self.config.transformers_version}.tar.gz"
|
73 |
-
archive_path = Path(f"./transformers_{self.config.transformers_version}.tar.gz")
|
74 |
-
|
75 |
-
try:
|
76 |
-
urlretrieve(archive_url, archive_path)
|
77 |
-
self._extract_archive(archive_path)
|
78 |
-
logger.info("Repository downloaded and extracted successfully")
|
79 |
-
except Exception as e:
|
80 |
-
raise RuntimeError(f"Failed to setup repository: {e}")
|
81 |
-
finally:
|
82 |
-
archive_path.unlink(missing_ok=True)
|
83 |
-
|
84 |
-
def _extract_archive(self, archive_path: Path) -> None:
|
85 |
-
"""Extract the downloaded archive."""
|
86 |
-
import tarfile
|
87 |
-
import tempfile
|
88 |
-
|
89 |
-
with tempfile.TemporaryDirectory() as tmp_dir:
|
90 |
-
with tarfile.open(archive_path, "r:gz") as tar:
|
91 |
-
tar.extractall(tmp_dir)
|
92 |
-
|
93 |
-
extracted_folder = next(Path(tmp_dir).iterdir())
|
94 |
-
extracted_folder.rename(self.config.repo_path)
|
95 |
-
|
96 |
-
def convert_model(self, input_model_id: str) -> Tuple[bool, Optional[str]]:
|
97 |
-
"""Convert the model to ONNX format."""
|
98 |
-
try:
|
99 |
-
result = subprocess.run(
|
100 |
-
[
|
101 |
-
sys.executable,
|
102 |
-
"-m",
|
103 |
-
"scripts.convert",
|
104 |
-
"--quantize",
|
105 |
-
"--model_id",
|
106 |
-
input_model_id,
|
107 |
-
],
|
108 |
-
cwd=self.config.repo_path,
|
109 |
-
capture_output=True,
|
110 |
-
text=True,
|
111 |
-
env={},
|
112 |
-
)
|
113 |
-
|
114 |
-
if result.returncode != 0:
|
115 |
-
return False, result.stderr
|
116 |
-
|
117 |
-
return True, result.stderr
|
118 |
-
|
119 |
-
except Exception as e:
|
120 |
-
return False, str(e)
|
121 |
-
|
122 |
-
def upload_model(self, input_model_id: str, output_model_id: str) -> Optional[str]:
|
123 |
-
"""Upload the converted model to Hugging Face."""
|
124 |
-
try:
|
125 |
-
self.api.create_repo(output_model_id, exist_ok=True, private=False)
|
126 |
-
model_folder_path = self.config.repo_path / "models" / input_model_id
|
127 |
-
|
128 |
-
self.api.upload_folder(
|
129 |
-
folder_path=str(model_folder_path), repo_id=output_model_id
|
130 |
-
)
|
131 |
-
return None
|
132 |
-
except Exception as e:
|
133 |
-
return str(e)
|
134 |
-
finally:
|
135 |
-
import shutil
|
136 |
-
|
137 |
-
shutil.rmtree(model_folder_path, ignore_errors=True)
|
138 |
-
|
139 |
-
|
140 |
-
def main():
|
141 |
-
"""Main application entry point."""
|
142 |
-
st.write("## Convert a Hugging Face model to ONNX")
|
143 |
-
|
144 |
-
try:
|
145 |
-
config = Config.from_env()
|
146 |
-
converter = ModelConverter(config)
|
147 |
-
converter.setup_repository()
|
148 |
-
|
149 |
-
input_model_id = st.text_input(
|
150 |
-
"Enter the Hugging Face model ID to convert. Example: `EleutherAI/pythia-14m`"
|
151 |
-
)
|
152 |
-
|
153 |
-
if not input_model_id:
|
154 |
-
return
|
155 |
-
|
156 |
-
st.text_input(
|
157 |
-
f"Optional: Your Hugging Face write token. Fill it if you want to upload the model under your account.",
|
158 |
-
type="password",
|
159 |
-
key="user_hf_token",
|
160 |
-
)
|
161 |
-
|
162 |
-
model_name = input_model_id.split("/")[-1]
|
163 |
-
output_model_id = f"{config.hf_username}/{model_name}-ONNX"
|
164 |
-
output_model_url = f"{config.hf_base_url}/{output_model_id}"
|
165 |
-
|
166 |
-
if converter.api.repo_exists(output_model_id):
|
167 |
-
st.write("This model has already been converted! 🎉")
|
168 |
-
st.link_button(f"Go to {output_model_id}", output_model_url, type="primary")
|
169 |
-
return
|
170 |
-
|
171 |
-
st.write(f"URL where the model will be converted and uploaded to:")
|
172 |
-
st.code(output_model_url, language="plaintext")
|
173 |
-
|
174 |
-
if not st.button(label="Proceed", type="primary"):
|
175 |
-
return
|
176 |
-
|
177 |
-
with st.spinner("Converting model..."):
|
178 |
-
success, stderr = converter.convert_model(input_model_id)
|
179 |
-
if not success:
|
180 |
-
st.error(f"Conversion failed: {stderr}")
|
181 |
-
return
|
182 |
-
|
183 |
-
st.success("Conversion successful!")
|
184 |
-
st.code(stderr)
|
185 |
-
|
186 |
-
with st.spinner("Uploading model..."):
|
187 |
-
error = converter.upload_model(input_model_id, output_model_id)
|
188 |
-
if error:
|
189 |
-
st.error(f"Upload failed: {error}")
|
190 |
-
return
|
191 |
-
|
192 |
-
st.success("Upload successful!")
|
193 |
-
st.write("You can now go and view the model on Hugging Face!")
|
194 |
-
st.link_button(f"Go to {output_model_id}", output_model_url, type="primary")
|
195 |
-
|
196 |
-
except Exception as e:
|
197 |
-
logger.exception("Application error")
|
198 |
-
st.error(f"An error occurred: {str(e)}")
|
199 |
-
|
200 |
-
|
201 |
-
if __name__ == "__main__":
|
202 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|