code: def download_weights(
      model_id: str,
      revision: Optional[str] = None,
      extension: str = ".safetensors",
      auto_convert: bool = True,
      logger_level: str = "INFO",
      json_output: bool = False,
      trust_remote_code: bool = False,
  ):
      # Remove default handler
      logger.remove()
      logger.add(
          sys.stdout,
          format="{message}",
          filter="text_generation_server",
          level=logger_level,
          serialize=json_output,
          backtrace=True,
          diagnose=False,
      )
  
      # Import here after the logger is added to log potential import exceptions
      from text_generation_server import utils
  
      # Test if files were already download
      try:
          utils.weight_files(model_id, revision, extension)
          logger.info(
              "Files are already present on the host. "
              "Skipping download."
          )
          return
      # Local files not found
      except (
          utils.LocalEntryNotFoundError,
          FileNotFoundError,
          utils.EntryNotFoundError,
      ):
          pass
  
      is_local_model = (
          Path(model_id).exists() and Path(model_id).is_dir()
      ) or os.getenv("WEIGHTS_CACHE_OVERRIDE", None) is not None
  
      if not is_local_model:
          try:
              adapter_config_filename = hf_hub_download(
                  model_id,
                  revision=revision,
                  filename="adapter_config.json",
              )
              utils.download_and_unload_peft(
                  model_id,
                  revision,
                  trust_remote_code=trust_remote_code,
              )
              is_local_model = True
              utils.weight_files(model_id, revision, extension)
              return
          except (
              utils.LocalEntryNotFoundError,
              utils.EntryNotFoundError,
          ):
              pass
  
          try:
              import json
  
              medusa_head = hf_hub_download(
                  model_id,
                  revision=revision,
                  filename="medusa_lm_head.pt",
              )
              if auto_convert:
                  medusa_sf = Path(
                      medusa_head[: -len(".pt")] + ".safetensors"
                  )
                  if not medusa_sf.exists():
                      utils.convert_files(
                          [Path(medusa_head)], [medusa_sf], []
                      )
              medusa_config = hf_hub_download(
                  model_id, revision=revision, filename="config.json"
              )
              with open(medusa_config, "r") as f:
                  config = json.load(f)
  
              model_id = config["base_model_name_or_path"]
              revision = "main"
              try:
                  utils.weight_files(model_id, revision, extension)
                  logger.info(
                      f"Files for parent {model_id} are already present on the host. "
                      "Skipping download."
                  )
                  return
              # Local files not found
              except (
                  utils.LocalEntryNotFoundError,
                  FileNotFoundError,
                  utils.EntryNotFoundError,
              ):
                  pass
          except (
              utils.LocalEntryNotFoundError,
              utils.EntryNotFoundError,
          ):
              pass
  
          # Try to download weights from the hub
          try:
              filenames = utils.weight_hub_files(
                  model_id, revision, extension
              )
              utils.download_weights(filenames, model_id, revision)
              # Successfully downloaded weights
              return
  
          # No weights found on the hub with this extension
          except utils.EntryNotFoundError as e:
              # Check if we want to automatically convert to safetensors or if we can use .bin weights instead
              if not extension == ".safetensors" or not auto_convert:
                  raise e
  
      elif (Path(model_id) / "medusa_lm_head.pt").exists():
          # Try to load as a local Medusa model
          try:
              import json
  
              medusa_head = Path(model_id) / "medusa_lm_head.pt"
              if auto_convert:
                  medusa_sf = (
                      Path(model_id) / "medusa_lm_head.safetensors"
                  )
                  if not medusa_sf.exists():
                      utils.convert_files(
                          [Path(medusa_head)], [medusa_sf], []
                      )
              medusa_config = Path(model_id) / "config.json"
              with open(medusa_config, "r") as f:
                  config = json.load(f)
  
              model_id = config["base_model_name_or_path"]
              revision = "main"
              try:
                  utils.weight_files(model_id, revision, extension)
                  logger.info(
                      f"Files for parent {model_id} are already present on the host. "
                      "Skipping download."
                  )
                  return
              # Local files not found
              except (
                  utils.LocalEntryNotFoundError,
                  utils.EntryNotFoundError,
              ):
                  pass
          except (
              utils.LocalEntryNotFoundError,
              utils.EntryNotFoundError,
          ):
              pass
  
      elif (Path(model_id) / "adapter_config.json").exists():
          # Try to load as a local PEFT model
          try:
              utils.download_and_unload_peft(
                  model_id,
                  revision,
                  trust_remote_code=trust_remote_code,
              )
              utils.weight_files(model_id, revision, extension)
              return
          except (
              utils.LocalEntryNotFoundError,
              utils.EntryNotFoundError,
          ):
              pass
  
      # Try to see if there are local pytorch weights
      try:
          # Get weights for a local model, a hub cached model and inside the WEIGHTS_CACHE_OVERRIDE
          local_pt_files = utils.weight_files(
              model_id, revision, ".bin"
          )
  
      # No local pytorch weights
      except utils.LocalEntryNotFoundError:
          if extension == ".safetensors":
              logger.warning(
                  f"No safetensors weights found for model {model_id} at revision {revision}. "
                  f"Downloading PyTorch weights."
              )
  
          # Try to see if there are pytorch weights on the hub
          pt_filenames = utils.weight_hub_files(
              model_id, revision, ".bin"
          )
          # Download pytorch weights
          local_pt_files = utils.download_weights(
              pt_filenames, model_id, revision
          )
  
      if auto_convert:
          logger.warning(
              f"No safetensors weights found for model {model_id} at revision {revision}. "
              f"Converting PyTorch weights to safetensors."
          )
  
          # Safetensors final filenames
          local_st_files = [
              p.parent / f"{p.stem.lstrip('pytorch_')}.safetensors"
              for p in local_pt_files
          ]
          try:
              import transformers
              import json
  
              if is_local_model:
                  config_filename = os.path.join(
                      model_id, "config.json"
                  )
              else:
                  config_filename = hf_hub_download(
                      model_id,
                      revision=revision,
                      filename="config.json",
                  )
              with open(config_filename, "r") as f:
                  config = json.load(f)
              architecture = config["architectures"][0]
  
              class_ = getattr(transformers, architecture)
  
              # Name for this varible depends on transformers version.
              discard_names = getattr(class_, "_tied_weights_keys", [])
  
          except Exception as e:
              discard_names = []
          # Convert pytorch weights to safetensors
          utils.convert_files(
              local_pt_files, local_st_files, discard_names
          )
  
file_path: text-generation-inference/server/text_generation_server/cli.py
imports: import os
  import sys
  import typer
  from pathlib import Path
  from loguru import logger
  from typing import Optional
  from enum import Enum
  from huggingface_hub import hf_hub_download
label_desc: Use image node_id 2 for calling a global function with example usage: download_weights(model_id, revision, extension, auto_convert, logger_level, json_output, trust_remote_code) without return types