Repository: ollama/ollama
File: api/client.py (Python)

Code before:
    # Check if the blob exists
    response = requests.head(url)
    if response.status_code != 404:
        return  # Blob already exists, no need to upload

    # Upload the blob
    with open(file_path, 'rb') as file_data:
        requests.post(url, data=file_data)


# Create a model from a Modelfile. Use the callback function to override the default handler.
def create(model_name, filename, callback=None):
    try:
        file_path = Path(filename).expanduser().resolve()
        # Read and process the modelfile
        with open(file_path, 'r') as f:
            lines = f.readlines()

        processed_lines = []
        for line in lines:
            # Skip empty or whitespace-only lines
            if not line.strip():
                continue
            
            parts = line.strip().split()
            command, *args = parts

            if command in ["FROM", "ADAPTER"]:
                path = Path(args[0]).expanduser()

                # Check if path is relative and resolve it
                if not path.is_absolute():
                    path = (file_path.parent / path).resolve()
                
                # Skip if file does not exist for "model", this is handled by the server
                if not path.exists() and command == "FROM":
                    processed_lines.append(line)
                    continue

                # Calculate SHA-256 hash
                with open(path, 'rb') as bin_file:
                    hash = hashlib.sha256()
                    hash.update(bin_file.read())
                    digest = f"sha256:{hash.hexdigest()}"
                
                # Add the file to the remote server
                create_blob(digest, path)

                # Replace path with digest in the line
                line = f"{command} @{digest}\n"


Review comment (suggestion):
```python
first, rest = line.split(maxsplit=1)
```

Code after:
    # Check if the blob exists
    response = requests.head(url)
    if response.status_code != 404:
        return  # Blob already exists, no need to upload
    response.raise_for_status()

    # Upload the blob
    with open(file_path, 'rb') as file_data:
        requests.post(url, data=file_data)


# Create a model from a Modelfile. Use the callback function to override the default handler.
def create(model_name, filename, callback=None):
    try:
        file_path = Path(filename).expanduser().resolve()
        processed_lines = []

        # Read and process the modelfile
        with open(file_path, 'r') as f:
            for line in f:            
                # Skip empty or whitespace-only lines
                if not line.strip():
                    continue
            
                command, args = line.split(maxsplit=1)

                if command.upper() in ["FROM", "ADAPTER"]:
                    path = Path(args.strip()).expanduser()

                    # Check if path is relative and resolve it
                    if not path.is_absolute():
                        path = (file_path.parent / path)

                    # Skip if file does not exist for "model", this is handled by the server
                    if not path.exists():
                        processed_lines.append(line)
                        continue

                    # Calculate SHA-256 hash
                    with open(path, 'rb') as bin_file:
                        hash = hashlib.sha256()
                        hash.update(bin_file.read())
                        blob = f"sha256:{hash.hexdigest()}"
                
                    # Add the file to the remote server
                    create_blob(blob, path)

                    # Replace path with digest in the line
                    line = f"{command} @{blob}\n"

                processed_lines.append(line)