@@ -317,30 +317,35 @@ def get_model_hash(self, model_path):
317317 This method returns the MD5 hash of a given model file.
318318 """
319319 self .logger .debug (f"Calculating hash of model file { model_path } " )
320- MB_10 = 10 * 1024 * 1024
320+ # Use the specific byte count from the original logic
321+ BYTES_TO_HASH = 10000 * 1024 # 10,240,000 bytes
322+
321323 try :
324+ file_size = os .path .getsize (model_path )
325+
322326 with open (model_path , "rb" ) as f :
323- # Get file size
324- f .seek (0 , io .SEEK_END )
325- file_size = f .tell ()
326-
327- if file_size < MB_10 :
328- # If file is smaller than 10MB, hash the whole file
329- f .seek (0 , io .SEEK_SET )
330- return hashlib .md5 (f .read ()).hexdigest ()
327+ if file_size < BYTES_TO_HASH :
328+ # Hash the entire file if smaller than the target byte count
329+ self .logger .debug (f"File size { file_size } < { BYTES_TO_HASH } , hashing entire file." )
330+ hash_value = hashlib .md5 (f .read ()).hexdigest ()
331331 else :
332- # Otherwise, hash the last 10MB
333- f .seek (file_size - MB_10 , io .SEEK_SET )
334- return hashlib .md5 (f .read ()).hexdigest ()
335- except IOError as e :
336- # Log the error but attempt to hash the entire file as a fallback
337- # This might still lead to lookup errors if the hash differs from UVR's expectation
338- self .logger .error (f"IOError calculating hash for { model_path } : { e } . Falling back to hashing entire file." )
339- try :
340- return hashlib .md5 (open (model_path , "rb" ).read ()).hexdigest ()
341- except Exception as inner_e :
342- self .logger .error (f"Failed to hash entire file after initial IOError: { inner_e } " )
343- raise inner_e # Re-raise the exception if the fallback also fails
332+ # Seek to the specific position before the end (from the beginning) and hash
333+ seek_pos = file_size - BYTES_TO_HASH
334+ self .logger .debug (f"File size { file_size } >= { BYTES_TO_HASH } , seeking to { seek_pos } and hashing remaining bytes." )
335+ f .seek (seek_pos , io .SEEK_SET )
336+ hash_value = hashlib .md5 (f .read ()).hexdigest ()
337+
338+ # Log the calculated hash
339+ self .logger .info (f"Hash of model file { model_path } is { hash_value } " )
340+ return hash_value
341+
342+ except FileNotFoundError :
343+ self .logger .error (f"Model file not found at { model_path } " )
344+ raise # Re-raise the specific error
345+ except Exception as e :
346+ # Catch other potential errors (e.g., permissions, other IOErrors)
347+ self .logger .error (f"Error calculating hash for { model_path } : { e } " )
348+ raise # Re-raise other errors
344349
345350 def download_file_if_not_exists (self , url , output_path ):
346351 """
0 commit comments