Update app.py
Browse files
    	
        app.py
    CHANGED
    
    | 
         @@ -56,6 +56,23 @@ except FileNotFoundError: 
     | 
|
| 56 | 
         
             
                print(f"Directory not found: {inference_dir}")
         
     | 
| 57 | 
         
             
                exit(1)
         
     | 
| 58 | 
         | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 59 | 
         
             
            # Function to create a temporary file with string content
         
     | 
| 60 | 
         
             
            def create_temp_file(content, prefix, suffix=".txt"):
         
     | 
| 61 | 
         
             
                temp_file = tempfile.NamedTemporaryFile(delete=False, mode="w", prefix=prefix, suffix=suffix)
         
     | 
| 
         @@ -72,6 +89,26 @@ def create_temp_file(content, prefix, suffix=".txt"): 
     | 
|
| 72 | 
         | 
| 73 | 
         
             
                return temp_file.name
         
     | 
| 74 | 
         | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 75 | 
         
             
            def infer(genre_txt_content, lyrics_txt_content):
         
     | 
| 76 | 
         
             
                # Create temporary files
         
     | 
| 77 | 
         
             
                genre_txt_path = create_temp_file(genre_txt_content, prefix="genre_")
         
     | 
| 
         @@ -85,7 +122,8 @@ def infer(genre_txt_content, lyrics_txt_content): 
     | 
|
| 85 | 
         
             
                os.makedirs(output_dir, exist_ok=True)
         
     | 
| 86 | 
         
             
                print(f"Output folder ensured at: {output_dir}")
         
     | 
| 87 | 
         | 
| 88 | 
         
            -
                
         
     | 
| 
         | 
|
| 89 | 
         
             
                # Command and arguments
         
     | 
| 90 | 
         
             
                command = [
         
     | 
| 91 | 
         
             
                    "python", "infer.py",
         
     | 
| 
         @@ -105,7 +143,6 @@ def infer(genre_txt_content, lyrics_txt_content): 
     | 
|
| 105 | 
         
             
                env = os.environ.copy()  # Copy current environment
         
     | 
| 106 | 
         
             
                env.update({
         
     | 
| 107 | 
         
             
                    "CUDA_VISIBLE_DEVICES": "0",
         
     | 
| 108 | 
         
            -
                    "PYTORCH_CUDA_ALLOC_CONF": "max_split_size_mb:512",
         
     | 
| 109 | 
         
             
                    "CUDA_HOME": "/usr/local/cuda",
         
     | 
| 110 | 
         
             
                    "PATH": f"/usr/local/cuda/bin:{env.get('PATH', '')}",
         
     | 
| 111 | 
         
             
                    "LD_LIBRARY_PATH": f"/usr/local/cuda/lib64:{env.get('LD_LIBRARY_PATH', '')}"
         
     | 
| 
         @@ -122,9 +159,17 @@ def infer(genre_txt_content, lyrics_txt_content): 
     | 
|
| 122 | 
         
             
                        print("Output folder contents:")
         
     | 
| 123 | 
         
             
                        for file in output_files:
         
     | 
| 124 | 
         
             
                            print(f"- {file}")
         
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 125 | 
         
             
                    else:
         
     | 
| 126 | 
         
             
                        print("Output folder is empty.")
         
     | 
| 127 | 
         
            -
             
     | 
| 128 | 
         
             
                except subprocess.CalledProcessError as e:
         
     | 
| 129 | 
         
             
                    print(f"Error occurred: {e}")
         
     | 
| 130 | 
         
             
                    return None
         
     | 
| 
         | 
|
| 56 | 
         
             
                print(f"Directory not found: {inference_dir}")
         
     | 
| 57 | 
         
             
                exit(1)
         
     | 
| 58 | 
         | 
| 59 | 
         
            +
            def empty_output_folder(output_dir):
         
     | 
| 60 | 
         
            +
                # List all files in the output directory
         
     | 
| 61 | 
         
            +
                files = os.listdir(output_dir)
         
     | 
| 62 | 
         
            +
                
         
     | 
| 63 | 
         
            +
                # Iterate over the files and remove them
         
     | 
| 64 | 
         
            +
                for file in files:
         
     | 
| 65 | 
         
            +
                    file_path = os.path.join(output_dir, file)
         
     | 
| 66 | 
         
            +
                    try:
         
     | 
| 67 | 
         
            +
                        if os.path.isdir(file_path):
         
     | 
| 68 | 
         
            +
                            # If it's a directory, remove it recursively
         
     | 
| 69 | 
         
            +
                            shutil.rmtree(file_path)
         
     | 
| 70 | 
         
            +
                        else:
         
     | 
| 71 | 
         
            +
                            # If it's a file, delete it
         
     | 
| 72 | 
         
            +
                            os.remove(file_path)
         
     | 
| 73 | 
         
            +
                    except Exception as e:
         
     | 
| 74 | 
         
            +
                        print(f"Error deleting file {file_path}: {e}")
         
     | 
| 75 | 
         
            +
             
     | 
| 76 | 
         
             
            # Function to create a temporary file with string content
         
     | 
| 77 | 
         
             
            def create_temp_file(content, prefix, suffix=".txt"):
         
     | 
| 78 | 
         
             
                temp_file = tempfile.NamedTemporaryFile(delete=False, mode="w", prefix=prefix, suffix=suffix)
         
     | 
| 
         | 
|
| 89 | 
         | 
| 90 | 
         
             
                return temp_file.name
         
     | 
| 91 | 
         | 
| 92 | 
         
            +
            def get_last_mp3_file(output_dir):
         
     | 
| 93 | 
         
            +
                # List all files in the output directory
         
     | 
| 94 | 
         
            +
                files = os.listdir(output_dir)
         
     | 
| 95 | 
         
            +
                
         
     | 
| 96 | 
         
            +
                # Filter only .mp3 files
         
     | 
| 97 | 
         
            +
                mp3_files = [file for file in files if file.endswith('.mp3')]
         
     | 
| 98 | 
         
            +
                
         
     | 
| 99 | 
         
            +
                if not mp3_files:
         
     | 
| 100 | 
         
            +
                    print("No .mp3 files found in the output folder.")
         
     | 
| 101 | 
         
            +
                    return None
         
     | 
| 102 | 
         
            +
                
         
     | 
| 103 | 
         
            +
                # Get the full path for the mp3 files
         
     | 
| 104 | 
         
            +
                mp3_files_with_path = [os.path.join(output_dir, file) for file in mp3_files]
         
     | 
| 105 | 
         
            +
                
         
     | 
| 106 | 
         
            +
                # Sort the files based on the modification time (most recent first)
         
     | 
| 107 | 
         
            +
                mp3_files_with_path.sort(key=lambda x: os.path.getmtime(x), reverse=True)
         
     | 
| 108 | 
         
            +
                
         
     | 
| 109 | 
         
            +
                # Return the most recent .mp3 file
         
     | 
| 110 | 
         
            +
                return mp3_files_with_path[0]
         
     | 
| 111 | 
         
            +
             
     | 
| 112 | 
         
             
            def infer(genre_txt_content, lyrics_txt_content):
         
     | 
| 113 | 
         
             
                # Create temporary files
         
     | 
| 114 | 
         
             
                genre_txt_path = create_temp_file(genre_txt_content, prefix="genre_")
         
     | 
| 
         | 
|
| 122 | 
         
             
                os.makedirs(output_dir, exist_ok=True)
         
     | 
| 123 | 
         
             
                print(f"Output folder ensured at: {output_dir}")
         
     | 
| 124 | 
         | 
| 125 | 
         
            +
                empty_output_folder(output_dir)
         
     | 
| 126 | 
         
            +
             
         
     | 
| 127 | 
         
             
                # Command and arguments
         
     | 
| 128 | 
         
             
                command = [
         
     | 
| 129 | 
         
             
                    "python", "infer.py",
         
     | 
| 
         | 
|
| 143 | 
         
             
                env = os.environ.copy()  # Copy current environment
         
     | 
| 144 | 
         
             
                env.update({
         
     | 
| 145 | 
         
             
                    "CUDA_VISIBLE_DEVICES": "0",
         
     | 
| 
         | 
|
| 146 | 
         
             
                    "CUDA_HOME": "/usr/local/cuda",
         
     | 
| 147 | 
         
             
                    "PATH": f"/usr/local/cuda/bin:{env.get('PATH', '')}",
         
     | 
| 148 | 
         
             
                    "LD_LIBRARY_PATH": f"/usr/local/cuda/lib64:{env.get('LD_LIBRARY_PATH', '')}"
         
     | 
| 
         | 
|
| 159 | 
         
             
                        print("Output folder contents:")
         
     | 
| 160 | 
         
             
                        for file in output_files:
         
     | 
| 161 | 
         
             
                            print(f"- {file}")
         
     | 
| 162 | 
         
            +
             
     | 
| 163 | 
         
            +
                        last_mp3 = get_last_mp3_file(output_dir)
         
     | 
| 164 | 
         
            +
             
     | 
| 165 | 
         
            +
                        if last_mp3:
         
     | 
| 166 | 
         
            +
                            print("Last .mp3 file:", last_mp3)
         
     | 
| 167 | 
         
            +
                            return last_mp3
         
     | 
| 168 | 
         
            +
                        else:
         
     | 
| 169 | 
         
            +
                            return None
         
     | 
| 170 | 
         
             
                    else:
         
     | 
| 171 | 
         
             
                        print("Output folder is empty.")
         
     | 
| 172 | 
         
            +
                        return None
         
     | 
| 173 | 
         
             
                except subprocess.CalledProcessError as e:
         
     | 
| 174 | 
         
             
                    print(f"Error occurred: {e}")
         
     | 
| 175 | 
         
             
                    return None
         
     |