Surn commited on
Commit
ce5d032
·
1 Parent(s): 142acfb

User History 0.3.5

Browse files
src/gradio_user_history/__init__.py CHANGED
@@ -21,4 +21,4 @@ Useful links:
21
  from ._user_history import render, save_image, save_file, setup # noqa: F401
22
 
23
 
24
- __version__ = "0.3.4"
 
21
  from ._user_history import render, save_image, save_file, setup # noqa: F401
22
 
23
 
24
+ __version__ = "0.3.5"
src/gradio_user_history/_user_history.py CHANGED
@@ -19,7 +19,6 @@ import wave
19
  from mutagen.mp3 import MP3, EasyMP3
20
  import torchaudio
21
  import subprocess
22
- from modules.file_utils import get_file_parts, rename_file_to_lowercase_extension
23
  from tqdm import tqdm
24
 
25
  user_profile = gr.State(None)
@@ -494,8 +493,8 @@ def _add_metadata(file_location: Path, metadata: Dict[str, Any], support_path: s
494
  if file_type not in valid_file_types:
495
  raise ValueError("Invalid file type. Valid file types are .wav, .mp3, .mp4, .png")
496
 
497
- directory, filename, name, ext, new_ext = get_file_parts(file_location)
498
- new_file_location = rename_file_to_lowercase_extension(os.path.join(directory, name +"_h"+ new_ext))
499
 
500
  if file_type == ".wav":
501
  # Open and process .wav file
@@ -576,6 +575,55 @@ def _archives_path() -> Path:
576
  path.mkdir(parents=True, exist_ok=True)
577
  return path
578
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
579
 
580
  #################
581
  # Admin section #
 
19
  from mutagen.mp3 import MP3, EasyMP3
20
  import torchaudio
21
  import subprocess
 
22
  from tqdm import tqdm
23
 
24
  user_profile = gr.State(None)
 
493
  if file_type not in valid_file_types:
494
  raise ValueError("Invalid file type. Valid file types are .wav, .mp3, .mp4, .png")
495
 
496
+ directory, filename, name, ext, new_ext = _get_file_parts(file_location)
497
+ new_file_location = _rename_file_to_lowercase_extension(os.path.join(directory, name +"_h"+ new_ext))
498
 
499
  if file_type == ".wav":
500
  # Open and process .wav file
 
575
  path.mkdir(parents=True, exist_ok=True)
576
  return path
577
 
578
+ def _get_file_parts(file_path: str):
579
+ # Split the path into directory and filename
580
+ directory, filename = os.path.split(file_path)
581
+
582
+ # Split the filename into name and extension
583
+ name, ext = os.path.splitext(filename)
584
+
585
+ # Convert the extension to lowercase
586
+ new_ext = ext.lower()
587
+ return directory, filename, name, ext, new_ext
588
+
589
+ def _rename_file_to_lowercase_extension(file_path: str) -> str:
590
+ """
591
+ Renames a file's extension to lowercase in place.
592
+
593
+ Parameters:
594
+ file_path (str): The original file path.
595
+
596
+ Returns:
597
+ str: The new file path with the lowercase extension.
598
+
599
+ Raises:
600
+ OSError: If there is an error renaming the file (e.g., file not found, permissions issue).
601
+ """
602
+ directory, filename, name, ext, new_ext = _get_file_parts(file_path)
603
+ # If the extension changes, rename the file
604
+ if ext != new_ext:
605
+ new_filename = name + new_ext
606
+ new_file_path = os.path.join(directory, new_filename)
607
+ try:
608
+ os.rename(file_path, new_file_path)
609
+ print(f"Rename {file_path} to {new_file_path}\n")
610
+ except Exception as e:
611
+ print(f"os.rename failed: {e}. Falling back to binary copy operation.")
612
+ try:
613
+ # Read the file in binary mode and write it to new_file_path
614
+ with open(file_path, 'rb') as f:
615
+ data = f.read()
616
+ with open(new_file_path, 'wb') as f:
617
+ f.write(data)
618
+ print(f"Copied {file_path} to {new_file_path}\n")
619
+ # Optionally, remove the original file after copying
620
+ #os.remove(file_path)
621
+ except Exception as inner_e:
622
+ print(f"Failed to copy file from {file_path} to {new_file_path}: {inner_e}")
623
+ raise inner_e
624
+ return new_file_path
625
+ else:
626
+ return file_path
627
 
628
  #################
629
  # Admin section #