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

Code before:
        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"

            processed_lines.append(line)

        # Combine processed lines back into a single string
        modelfile_content = ''.join(processed_lines)

        url = f"{BASE_URL}/api/create"
        payload = {"name": model_name, "modelfile": modelfile_content}

        # Making a POST request with the stream parameter set to True to handle streaming responses
        with requests.post(url, json=payload, stream=True) as response:
            response.raise_for_status()
            # Iterating over the response line by line and displaying the status
            for line in response.iter_lines():
                if line:
                    chunk = json.loads(line)
                    if callback:
                        callback(chunk)
                    else:

Review comment (suggestion):
```suggestion
                with open(path, 'rb') as bin_file:
                    digest = hashlib.file_digest(bin_file, 'sha256')
                    blob = f'sha256:{digest.hexdigest()}'
```

Code after:
        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)

        # Combine processed lines back into a single string
        modelfile_content = '\n'.join(processed_lines)

        url = f"{BASE_URL}/api/create"
        payload = {"name": model_name, "modelfile": modelfile_content}

        # Making a POST request with the stream parameter set to True to handle streaming responses
        with requests.post(url, json=payload, stream=True) as response:
            response.raise_for_status()
            # Iterating over the response line by line and displaying the status
            for line in response.iter_lines():
                if line:
                    chunk = json.loads(line)
                    if callback:
                        callback(chunk)
                    else:
                        print(f"Status: {chunk.get('status')}")