Create upload-onnx.py
Browse files- upload-onnx.py +67 -0
upload-onnx.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import shutil
|
3 |
+
from huggingface_hub import HfApi, HfFolder, repo_type_and_id_from_hf_id, upload_file
|
4 |
+
|
5 |
+
def main():
|
6 |
+
# Obtiene el nombre del modelo de la variable de entorno
|
7 |
+
model_name = os.getenv('MODELNAME')
|
8 |
+
if model_name is None:
|
9 |
+
print("MODELNAME no está definida en las variables de entorno.")
|
10 |
+
return
|
11 |
+
|
12 |
+
# Ruta a los archivos
|
13 |
+
onnx_file = f"{model_name}.onnx"
|
14 |
+
config_file = "config.json"
|
15 |
+
onnx_json_file = f"{model_name}.onnx.json"
|
16 |
+
|
17 |
+
# Verifica si el archivo .onnx existe
|
18 |
+
if os.path.isfile(onnx_file):
|
19 |
+
print(f"{model_name}.onnx encontrado.")
|
20 |
+
else:
|
21 |
+
print(f"{model_name}.onnx no se encuentra.")
|
22 |
+
return
|
23 |
+
|
24 |
+
# Renombra config.json a ${MODELNAME}.onnx.json si existe
|
25 |
+
if os.path.isfile(config_file):
|
26 |
+
shutil.move(config_file, onnx_json_file)
|
27 |
+
print(f"Renombrado config.json a {onnx_json_file}")
|
28 |
+
else:
|
29 |
+
print("config.json no se encuentra.")
|
30 |
+
return
|
31 |
+
|
32 |
+
# Configuración para la API de Hugging Face
|
33 |
+
token = HfFolder.get_token() # Obtiene el token de acceso desde el archivo .huggingface/token
|
34 |
+
api = HfApi()
|
35 |
+
|
36 |
+
# Crea el repositorio en Hugging Face si no existe
|
37 |
+
repo_id = f"{HfFolder.get_username(token)}/{model_name}"
|
38 |
+
try:
|
39 |
+
api.create_repo(repo_id, token=token)
|
40 |
+
print(f"Repositorio {repo_id} creado en Hugging Face.")
|
41 |
+
except Exception as e:
|
42 |
+
print(f"El repositorio {repo_id} ya existe o se produjo un error: {e}")
|
43 |
+
|
44 |
+
# Subir archivos a Hugging Face
|
45 |
+
try:
|
46 |
+
# Subir el archivo .onnx
|
47 |
+
upload_file(
|
48 |
+
path_or_fileobj=onnx_file,
|
49 |
+
path_in_repo=f"{model_name}.onnx",
|
50 |
+
repo_id=repo_id,
|
51 |
+
token=token,
|
52 |
+
)
|
53 |
+
print(f"{model_name}.onnx subido a Hugging Face.")
|
54 |
+
|
55 |
+
# Subir el archivo .onnx.json
|
56 |
+
upload_file(
|
57 |
+
path_or_fileobj=onnx_json_file,
|
58 |
+
path_in_repo=f"{model_name}.onnx.json",
|
59 |
+
repo_id=repo_id,
|
60 |
+
token=token,
|
61 |
+
)
|
62 |
+
print(f"{model_name}.onnx.json subido a Hugging Face.")
|
63 |
+
except Exception as e:
|
64 |
+
print(f"Error al subir archivos a Hugging Face: {e}")
|
65 |
+
|
66 |
+
if __name__ == '__main__':
|
67 |
+
main()
|