|
import os |
|
import base64 |
|
import shutil |
|
from huggingface_hub import HfApi, upload_file |
|
|
|
def main(): |
|
|
|
model_name = os.getenv('MODELNAME') |
|
if model_name is None: |
|
print("MODELNAME no está definida en las variables de entorno.") |
|
return |
|
|
|
|
|
onnx_file = f"{model_name}.onnx" |
|
config_file = "config.json" |
|
onnx_json_file = f"{model_name}.onnx.json" |
|
|
|
|
|
if not os.path.isfile(onnx_file): |
|
print(f"{model_name}.onnx no se encuentra.") |
|
return |
|
|
|
print(f"{model_name}.onnx encontrado.") |
|
|
|
|
|
if os.path.isfile(config_file): |
|
shutil.move(config_file, onnx_json_file) |
|
print(f"Renombrado config.json a {onnx_json_file}") |
|
else: |
|
print("config.json no se encuentra.") |
|
return |
|
|
|
|
|
token_base64 = os.getenv('TOKEN') |
|
if token_base64 is None: |
|
print("TOKEN no está definida en las variables de entorno.") |
|
return |
|
|
|
|
|
token = base64.b64decode(token_base64).decode('utf-8') |
|
|
|
|
|
api = HfApi() |
|
|
|
|
|
user_info = api.whoami(token=token) |
|
username = user_info['name'] |
|
|
|
|
|
repo_id = f"{username}/{model_name}" |
|
try: |
|
|
|
api.create_repo(repo_id=repo_id, token=token, private=True) |
|
print(f"Repositorio {repo_id} creado en Hugging Face como privado.") |
|
except Exception as e: |
|
print(f"El repositorio {repo_id} ya existe o se produjo un error: {e}") |
|
|
|
|
|
try: |
|
|
|
upload_file( |
|
path_or_fileobj=onnx_file, |
|
path_in_repo=f"{model_name}.onnx", |
|
repo_id=repo_id, |
|
token=token, |
|
) |
|
print(f"{model_name}.onnx subido a Hugging Face.") |
|
|
|
|
|
upload_file( |
|
path_or_fileobj=onnx_json_file, |
|
path_in_repo=f"{model_name}.onnx.json", |
|
repo_id=repo_id, |
|
token=token, |
|
) |
|
print(f"{model_name}.onnx.json subido a Hugging Face.") |
|
except Exception as e: |
|
print(f"Error al subir archivos a Hugging Face: {e}") |
|
|
|
if __name__ == '__main__': |
|
main() |
|
|