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

Code before:
    except requests.exceptions.RequestException as e:
        print(f"An error occurred: {e}")
        return None, None
    

# Create a blob file on the server if it doesn't exist.
def create_blob(digest, file_path):
    url = f"{BASE_URL}/api/blobs/{digest}"

    # 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()

Review comment (suggestion):
Instead of reading the entire file into memory, you can use `f` as an iterator directly

```python
for line in f:
  line = line.strip()
  if not line:
    continue

  ...
```

Code after:
    except requests.exceptions.RequestException as e:
        print(f"An error occurred: {e}")
        return None, None
    

# Create a blob file on the server if it doesn't exist.
def create_blob(digest, file_path):
    url = f"{BASE_URL}/api/blobs/{digest}"

    # 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())