Spaces:
				
			
			
	
			
			
		Paused
		
	
	
	
			
			
	
	
	
	
		
		
		Paused
		
	Create app.py
Browse files
    	
        app.py
    ADDED
    
    | @@ -0,0 +1,106 @@ | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            import gradio as gr
         | 
| 2 | 
            +
            import subprocess
         | 
| 3 | 
            +
            import os 
         | 
| 4 | 
            +
            import shutil
         | 
| 5 | 
            +
            import tempfile
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            from huggingface_hub import snapshot_download 
         | 
| 8 | 
            +
             | 
| 9 | 
            +
            # Create xcodec_mini_infer folder
         | 
| 10 | 
            +
            folder_path = './inference/xcodec_mini_infer'
         | 
| 11 | 
            +
             | 
| 12 | 
            +
            # Create the folder if it doesn't exist
         | 
| 13 | 
            +
            if not os.path.exists(folder_path):
         | 
| 14 | 
            +
                os.mkdir(folder_path)
         | 
| 15 | 
            +
                print(f"Folder created at: {folder_path}")
         | 
| 16 | 
            +
            else:
         | 
| 17 | 
            +
                print(f"Folder already exists at: {folder_path}")
         | 
| 18 | 
            +
             | 
| 19 | 
            +
            snapshot_download(
         | 
| 20 | 
            +
                repo_id = "m-a-p/xcodec_mini_infer",
         | 
| 21 | 
            +
                local_dir = "./inference/xcodec_mini_infer"
         | 
| 22 | 
            +
            )
         | 
| 23 | 
            +
             | 
| 24 | 
            +
            # Change to the "inference" directory
         | 
| 25 | 
            +
            inference_dir = "./inference"
         | 
| 26 | 
            +
            try:
         | 
| 27 | 
            +
                os.chdir(inference_dir)
         | 
| 28 | 
            +
                print(f"Changed working directory to: {os.getcwd()}")
         | 
| 29 | 
            +
            except FileNotFoundError:
         | 
| 30 | 
            +
                print(f"Directory not found: {inference_dir}")
         | 
| 31 | 
            +
                exit(1)
         | 
| 32 | 
            +
             | 
| 33 | 
            +
            # Function to create a temporary file with string content
         | 
| 34 | 
            +
            def create_temp_file(content, prefix, suffix=".txt"):
         | 
| 35 | 
            +
                temp_file = tempfile.NamedTemporaryFile(delete=False, mode="w", prefix=prefix, suffix=suffix)
         | 
| 36 | 
            +
                temp_file.write(content)
         | 
| 37 | 
            +
                temp_file.close()
         | 
| 38 | 
            +
                return temp_file.name
         | 
| 39 | 
            +
             | 
| 40 | 
            +
            def infer(genre_txt_content, lyrics_txt_content):
         | 
| 41 | 
            +
                # Create temporary files
         | 
| 42 | 
            +
                genre_txt_path = create_temp_file(genre_txt_content, prefix="genre_")
         | 
| 43 | 
            +
                lyrics_txt_path = create_temp_file(lyrics_txt_content, prefix="lyrics_")
         | 
| 44 | 
            +
             | 
| 45 | 
            +
                # Ensure the output folder exists
         | 
| 46 | 
            +
                output_dir = "./output"
         | 
| 47 | 
            +
                os.makedirs(output_dir, exist_ok=True)
         | 
| 48 | 
            +
                print(f"Output folder ensured at: {output_dir}")
         | 
| 49 | 
            +
             | 
| 50 | 
            +
                
         | 
| 51 | 
            +
                # Command and arguments
         | 
| 52 | 
            +
                command = [
         | 
| 53 | 
            +
                    "python", "infer.py",
         | 
| 54 | 
            +
                    "--stage1_model", "m-a-p/YuE-s1-7B-anneal-en-cot",
         | 
| 55 | 
            +
                    "--stage2_model", "m-a-p/YuE-s2-1B-general",
         | 
| 56 | 
            +
                    "--genre_txt", f"{genre_txt_path}",
         | 
| 57 | 
            +
                    "--lyrics_txt", f"{lyrics_txt_path}",
         | 
| 58 | 
            +
                    "--run_n_segments", "2",
         | 
| 59 | 
            +
                    "--stage2_batch_size", "4",
         | 
| 60 | 
            +
                    "--output_dir", f"{output_dir}",
         | 
| 61 | 
            +
                    "--cuda_idx", "0",
         | 
| 62 | 
            +
                    "--max_new_tokens", "3000"
         | 
| 63 | 
            +
                ]
         | 
| 64 | 
            +
                
         | 
| 65 | 
            +
                # Execute the command
         | 
| 66 | 
            +
                try:
         | 
| 67 | 
            +
                    subprocess.run(command, check=True)
         | 
| 68 | 
            +
                    print("Command executed successfully!")
         | 
| 69 | 
            +
                    
         | 
| 70 | 
            +
                    # Check and print the contents of the output folder
         | 
| 71 | 
            +
                    output_files = os.listdir(output_dir)
         | 
| 72 | 
            +
                    if output_files:
         | 
| 73 | 
            +
                        print("Output folder contents:")
         | 
| 74 | 
            +
                        for file in output_files:
         | 
| 75 | 
            +
                            print(f"- {file}")
         | 
| 76 | 
            +
                    else:
         | 
| 77 | 
            +
                        print("Output folder is empty.")
         | 
| 78 | 
            +
                    return None
         | 
| 79 | 
            +
                except subprocess.CalledProcessError as e:
         | 
| 80 | 
            +
                    print(f"Error occurred: {e}")
         | 
| 81 | 
            +
                    return None
         | 
| 82 | 
            +
                finally:
         | 
| 83 | 
            +
                    # Clean up temporary files
         | 
| 84 | 
            +
                    os.remove(genre_txt_path)
         | 
| 85 | 
            +
                    os.remove(lyrics_txt_path)
         | 
| 86 | 
            +
                    print("Temporary files deleted.")
         | 
| 87 | 
            +
             | 
| 88 | 
            +
            # Gradio 
         | 
| 89 | 
            +
             | 
| 90 | 
            +
            with gr.Blocks() as demo:
         | 
| 91 | 
            +
                with gr.Column():
         | 
| 92 | 
            +
                    gr.Markdown("# YuE")
         | 
| 93 | 
            +
                    with gr.Row():
         | 
| 94 | 
            +
                        with gr.Column():
         | 
| 95 | 
            +
                            genre_txt = gr.Textbox(label="Genre")
         | 
| 96 | 
            +
                            lyrics_txt = gr.Textbox(label="Lyrics")
         | 
| 97 | 
            +
                            submit_btn = gr.Button("Submit")
         | 
| 98 | 
            +
                        with gr.Column():
         | 
| 99 | 
            +
                            music_out = gr.Audio(label="Audio Result")
         | 
| 100 | 
            +
                
         | 
| 101 | 
            +
                submit_btn.click(
         | 
| 102 | 
            +
                    fn = infer, 
         | 
| 103 | 
            +
                    inputs = [genre_txt, lyrics_txt],
         | 
| 104 | 
            +
                    outputs = [music_out]
         | 
| 105 | 
            +
                )
         | 
| 106 | 
            +
            demo.queue().launch(show_api=False, show_error=True)
         | 

