Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions audio_separator/separator/separator.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import logging
import warnings
import importlib
import io
from typing import Optional

import hashlib
Expand Down Expand Up @@ -316,18 +317,35 @@ def get_model_hash(self, model_path):
This method returns the MD5 hash of a given model file.
"""
self.logger.debug(f"Calculating hash of model file {model_path}")
# Use the specific byte count from the original logic
BYTES_TO_HASH = 10000 * 1024 # 10,240,000 bytes

try:
# Open the model file in binary read mode
file_size = os.path.getsize(model_path)

with open(model_path, "rb") as f:
# Move the file pointer 10MB before the end of the file
f.seek(-10000 * 1024, 2)
# Read the file from the current pointer to the end and calculate its MD5 hash
return hashlib.md5(f.read()).hexdigest()
except IOError as e:
# If an IOError occurs (e.g., if the file is less than 10MB large), log the error
self.logger.error(f"IOError seeking -10MB or reading model file for hash calculation: {e}")
# Attempt to open the file again, read its entire content, and calculate the MD5 hash
return hashlib.md5(open(model_path, "rb").read()).hexdigest()
if file_size < BYTES_TO_HASH:
# Hash the entire file if smaller than the target byte count
self.logger.debug(f"File size {file_size} < {BYTES_TO_HASH}, hashing entire file.")
hash_value = hashlib.md5(f.read()).hexdigest()
else:
# Seek to the specific position before the end (from the beginning) and hash
seek_pos = file_size - BYTES_TO_HASH
self.logger.debug(f"File size {file_size} >= {BYTES_TO_HASH}, seeking to {seek_pos} and hashing remaining bytes.")
f.seek(seek_pos, io.SEEK_SET)
hash_value = hashlib.md5(f.read()).hexdigest()

# Log the calculated hash
self.logger.info(f"Hash of model file {model_path} is {hash_value}")
return hash_value

except FileNotFoundError:
self.logger.error(f"Model file not found at {model_path}")
raise # Re-raise the specific error
except Exception as e:
# Catch other potential errors (e.g., permissions, other IOErrors)
self.logger.error(f"Error calculating hash for {model_path}: {e}")
raise # Re-raise other errors

def download_file_if_not_exists(self, url, output_path):
"""
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"

[tool.poetry]
name = "audio-separator"
version = "0.30.1"
version = "0.30.2"
description = "Easy to use audio stem separation, using various models from UVR trained primarily by @Anjok07"
authors = ["Andrew Beveridge <andrew@beveridge.uk>"]
license = "MIT"
Expand Down