Spaces:
				
			
			
	
			
			
		Runtime error
		
	
	
	
			
			
	
	
	
	
		
		
		Runtime error
		
	Update app.py
Browse files
    	
        app.py
    CHANGED
    
    | @@ -5,11 +5,38 @@ import torch | |
| 5 | 
             
            import pickle
         | 
| 6 | 
             
            import numpy
         | 
| 7 | 
             
            import librosa
         | 
|  | |
| 8 | 
             
            from avatar import Avatar
         | 
| 9 |  | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 10 | 
             
            # Cấu hình ban đầu
         | 
| 11 | 
             
            options = ['Aude', 'Kyla', 'Liv', 'MC6']
         | 
| 12 | 
             
            images = ['ref_videos/Aude.png', 'ref_videos/Kyla.png', 'ref_videos/Liv.png', 'ref_videos/MC6.png']
         | 
|  | |
| 13 | 
             
            # Thêm đường dẫn đến thư mục Wav2Lip
         | 
| 14 | 
             
            wav2lip_path = os.path.join(os.path.dirname(__file__), "Wav2Lip")
         | 
| 15 | 
             
            if wav2lip_path not in sys.path:
         | 
| @@ -27,18 +54,20 @@ init_progress_bar = st.progress(0) | |
| 27 |  | 
| 28 | 
             
            # Khởi tạo session state
         | 
| 29 | 
             
            if 'is_initialized' not in st.session_state:
         | 
|  | |
|  | |
| 30 | 
             
                # Khởi tạo Avatar
         | 
| 31 | 
             
                st.session_state.avatar = Avatar()
         | 
| 32 | 
             
                st.session_state.avatar.export_video = False
         | 
| 33 |  | 
| 34 | 
             
                # Load model
         | 
| 35 | 
            -
                current_status_placeholder.write(" | 
| 36 | 
             
                st.session_state.avatar.load_model("checkpoint/wav2lip_gan.pth")
         | 
| 37 | 
            -
                current_status_placeholder.write(" | 
| 38 |  | 
| 39 | 
             
                # Cấu hình thiết bị
         | 
| 40 | 
             
                st.session_state.avatar.device = 'cuda' if torch.cuda.is_available() else 'cpu'
         | 
| 41 | 
            -
                print(st.session_state.avatar.device)
         | 
| 42 |  | 
| 43 | 
             
                # Cấu hình đường dẫn
         | 
| 44 | 
             
                st.session_state.avatar.output_audio_path = "audio/"
         | 
| @@ -56,15 +85,6 @@ if 'is_initialized' not in st.session_state: | |
| 56 | 
             
                st.session_state.avatar.get_video_full_frames(st.session_state.avatar.ref_video_path_and_filename)
         | 
| 57 | 
             
                st.session_state.avatar.face_detect_batch_size = 16
         | 
| 58 |  | 
| 59 | 
            -
                # Tạo và lưu face detection results
         | 
| 60 | 
            -
                face_det_results = st.session_state.avatar.create_face_detection_results(
         | 
| 61 | 
            -
                    st.session_state.avatar.video_full_frames, 
         | 
| 62 | 
            -
                    save_result=False
         | 
| 63 | 
            -
                )
         | 
| 64 | 
            -
                output_path = f"ref_videos/{st.session_state.selected_option}_face_det_result.pkl"
         | 
| 65 | 
            -
                with open(output_path, 'wb') as f:
         | 
| 66 | 
            -
                    pickle.dump(face_det_results, f)
         | 
| 67 | 
            -
                
         | 
| 68 | 
             
                # Load face detection results cho tất cả options
         | 
| 69 | 
             
                st.session_state.face_det_results_dict = {}
         | 
| 70 | 
             
                for option in options:
         | 
| @@ -77,7 +97,7 @@ if 'is_initialized' not in st.session_state: | |
| 77 | 
             
                # Xử lý text to speech
         | 
| 78 | 
             
                input_text = "Hi How are you?"
         | 
| 79 | 
             
                st.session_state.avatar.text_to_lip_video(input_text, init_progress_bar)
         | 
| 80 | 
            -
                current_status_placeholder.write(" | 
| 81 |  | 
| 82 | 
             
                st.session_state['is_initialized'] = True
         | 
| 83 |  | 
|  | |
| 5 | 
             
            import pickle
         | 
| 6 | 
             
            import numpy
         | 
| 7 | 
             
            import librosa
         | 
| 8 | 
            +
            import subprocess
         | 
| 9 | 
             
            from avatar import Avatar
         | 
| 10 |  | 
| 11 | 
            +
            def run_pickleface():
         | 
| 12 | 
            +
                try:
         | 
| 13 | 
            +
                    result = subprocess.run(
         | 
| 14 | 
            +
                        ['python', 'pickleface.py'],
         | 
| 15 | 
            +
                        check=True,
         | 
| 16 | 
            +
                        capture_output=True,
         | 
| 17 | 
            +
                        text=True
         | 
| 18 | 
            +
                    )
         | 
| 19 | 
            +
                    print(result.stdout)
         | 
| 20 | 
            +
                    return True
         | 
| 21 | 
            +
                except subprocess.CalledProcessError as e:
         | 
| 22 | 
            +
                    print(f"Error running pickleface.py: {e.stderr}")
         | 
| 23 | 
            +
                    return False
         | 
| 24 | 
            +
             | 
| 25 | 
            +
            def initialize_face_detection_results():
         | 
| 26 | 
            +
                # Kiểm tra xem tất cả file pkl đã tồn tại chưa
         | 
| 27 | 
            +
                missing_files = [opt for opt in options if not os.path.exists(f'ref_videos/{opt}_face_det_result.pkl')]
         | 
| 28 | 
            +
                
         | 
| 29 | 
            +
                if missing_files:
         | 
| 30 | 
            +
                    current_status_placeholder.write("Creating face detection results...")
         | 
| 31 | 
            +
                    if not run_pickleface():
         | 
| 32 | 
            +
                        st.error("Failed to create face detection results")
         | 
| 33 | 
            +
                        st.stop()
         | 
| 34 | 
            +
                    current_status_placeholder.write("Face detection results created successfully!")
         | 
| 35 | 
            +
             | 
| 36 | 
             
            # Cấu hình ban đầu
         | 
| 37 | 
             
            options = ['Aude', 'Kyla', 'Liv', 'MC6']
         | 
| 38 | 
             
            images = ['ref_videos/Aude.png', 'ref_videos/Kyla.png', 'ref_videos/Liv.png', 'ref_videos/MC6.png']
         | 
| 39 | 
            +
             | 
| 40 | 
             
            # Thêm đường dẫn đến thư mục Wav2Lip
         | 
| 41 | 
             
            wav2lip_path = os.path.join(os.path.dirname(__file__), "Wav2Lip")
         | 
| 42 | 
             
            if wav2lip_path not in sys.path:
         | 
|  | |
| 54 |  | 
| 55 | 
             
            # Khởi tạo session state
         | 
| 56 | 
             
            if 'is_initialized' not in st.session_state:
         | 
| 57 | 
            +
                initialize_face_detection_results()
         | 
| 58 | 
            +
                
         | 
| 59 | 
             
                # Khởi tạo Avatar
         | 
| 60 | 
             
                st.session_state.avatar = Avatar()
         | 
| 61 | 
             
                st.session_state.avatar.export_video = False
         | 
| 62 |  | 
| 63 | 
             
                # Load model
         | 
| 64 | 
            +
                current_status_placeholder.write("Loading model...")
         | 
| 65 | 
             
                st.session_state.avatar.load_model("checkpoint/wav2lip_gan.pth")
         | 
| 66 | 
            +
                current_status_placeholder.write("Model loaded successfully")
         | 
| 67 |  | 
| 68 | 
             
                # Cấu hình thiết bị
         | 
| 69 | 
             
                st.session_state.avatar.device = 'cuda' if torch.cuda.is_available() else 'cpu'
         | 
| 70 | 
            +
                print(f"Using device: {st.session_state.avatar.device}")
         | 
| 71 |  | 
| 72 | 
             
                # Cấu hình đường dẫn
         | 
| 73 | 
             
                st.session_state.avatar.output_audio_path = "audio/"
         | 
|  | |
| 85 | 
             
                st.session_state.avatar.get_video_full_frames(st.session_state.avatar.ref_video_path_and_filename)
         | 
| 86 | 
             
                st.session_state.avatar.face_detect_batch_size = 16
         | 
| 87 |  | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 88 | 
             
                # Load face detection results cho tất cả options
         | 
| 89 | 
             
                st.session_state.face_det_results_dict = {}
         | 
| 90 | 
             
                for option in options:
         | 
|  | |
| 97 | 
             
                # Xử lý text to speech
         | 
| 98 | 
             
                input_text = "Hi How are you?"
         | 
| 99 | 
             
                st.session_state.avatar.text_to_lip_video(input_text, init_progress_bar)
         | 
| 100 | 
            +
                current_status_placeholder.write("Face detection results loaded")
         | 
| 101 |  | 
| 102 | 
             
                st.session_state['is_initialized'] = True
         | 
| 103 |  | 
