diff --git a/.env b/.env index 78fe012901..06dd83f350 100644 --- a/.env +++ b/.env @@ -26,6 +26,8 @@ W2L_INFERENCE_OUTPUT_BUCKET_ROOT=/w2l_output W2L_CODE_DIRECTORY=/home/bt/dev/storyteller/storyteller-web/models/Wav2Lip #W2L_MODEL_CHECKPOINT=/home/bt/models/wav2lip/s3fd-619a316812.pth W2L_MODEL_CHECKPOINT=/home/bt/models/wav2lip/wav2lip_gan.pth +W2L_MODEL_FILENAME=wav2lip_gan.pth W2L_DOWNLOAD_SCRIPT_NAME=vocodes_process_upload.py W2L_INFERENCE_SCRIPT_NAME=vocodes_process_inference.py + diff --git a/src/buckets/bucket_client.rs b/src/buckets/bucket_client.rs index 3956ab8d51..715055c032 100644 --- a/src/buckets/bucket_client.rs +++ b/src/buckets/bucket_client.rs @@ -164,6 +164,25 @@ impl BucketClient { Ok(bytes) } + pub async fn download_file_to_disk(&self, object_path: &str, filesystem_path: &str) + -> anyhow::Result<()> + { + info!("downloading from bucket: {}", object_path); + + let mut output_file = std::fs::File::create(filesystem_path)?; + let status_code = self.bucket.get_object_stream(object_path, &mut output_file).await?; + + match status_code { + 404 => bail!("File not found in bucket: {}", object_path), + _ => {}, + } + + info!("download code: {}", status_code); + + Ok(()) + } + + // NB: New version has blocking client rather than blocking calls. // pub fn download_file_blocking(&self, path: &str) -> anyhow::Result> { // info!("downloading from bucket: {}", path); diff --git a/src/buckets/bucket_path_unifier.rs b/src/buckets/bucket_path_unifier.rs index b5252ccfdc..8410901a2a 100644 --- a/src/buckets/bucket_path_unifier.rs +++ b/src/buckets/bucket_path_unifier.rs @@ -9,6 +9,7 @@ pub struct BucketPathUnifier { pub user_uploaded_audio_for_w2l_root: PathBuf, pub tts_inference_output_root: PathBuf, pub w2l_inference_output_root: PathBuf, + pub w2l_model_root: PathBuf, } impl BucketPathUnifier { @@ -27,6 +28,7 @@ impl BucketPathUnifier { user_uploaded_w2l_templates_root: PathBuf::from("/user_uploaded_w2l_templates"), tts_inference_output_root: PathBuf::from("/tts_inference_output"), w2l_inference_output_root: PathBuf::from("/w2l_inference_output"), + w2l_model_root: PathBuf::from("/w2l_pretrained_models"), } } @@ -34,6 +36,11 @@ impl BucketPathUnifier { "".to_string() } + // W2L pretrained models. There are only two. + pub fn w2l_pretrained_models_path(&self, w2l_model_name: &str) -> PathBuf { + self.w2l_model_root.join(w2l_model_name) + } + // These share the same directory as the uploaded w2l template media. // eg. /user_uploaded_w2l_templates/1/5/1/151a[...60]_detected_faces.pickle pub fn precomputed_faces_for_w2l_path(&self, template_file_hash: &str) -> PathBuf { @@ -64,10 +71,6 @@ impl BucketPathUnifier { .join(video_filename) } - //pub fn pretrained_w2l_model_path(&self, model_name: &str) -> String { - // "".to_string() - //} - pub fn hashed_directory_path(file_hash: &str) -> String { match file_hash.len() { 0 | 1=> "".to_string(), @@ -89,9 +92,17 @@ mod tests { user_uploaded_audio_for_w2l_root: PathBuf::from("/test_path_w2l_audio"), tts_inference_output_root: PathBuf::from("/test_path_tts_output"), w2l_inference_output_root: PathBuf::from("/test_path_w2l_output"), + w2l_model_root: PathBuf::from("/test_path_w2l_pretrained_models"), } } + #[test] + fn test_w2l_pretrained_models_path() { + let paths = get_instance(); + assert_eq!(paths.w2l_pretrained_models_path("model.pth").to_str().unwrap(), + "/test_path_w2l_pretrained_models/model.pth"); + } + #[test] fn test_user_audio_for_w2l_inference_path() { let paths = get_instance(); diff --git a/src/util/semi_persistent_cache_dir.rs b/src/util/semi_persistent_cache_dir.rs index 50abf148a0..488b03b8ff 100644 --- a/src/util/semi_persistent_cache_dir.rs +++ b/src/util/semi_persistent_cache_dir.rs @@ -31,36 +31,50 @@ impl SemiPersistentCacheDir { } } + // ==================== TTS MODELS ==================== + /// We cache TTS models here. + /// We'll likely need to LRU cache them. pub fn tts_model_path(&self) -> &Path { &self.tts_model_root } - /// We cache W2L models here. - pub fn w2l_model_path(&self) -> &Path { + // ==================== W2L MODELS (there are only two of them) ==================== + + /// There are only two pretrained W2L models, so we won't run out of space. + pub fn w2l_model_path(&self, model_filename: &str) -> PathBuf { + self.w2l_model_root.join(model_filename) + } + + pub fn w2l_model_directory(&self) -> &Path { &self.w2l_model_root } - /// Face templates go here. + pub fn create_w2l_model_path(&self) -> AnyhowResult<()> { + let _ = fs::create_dir_all(self.w2l_model_directory())?; + Ok(()) + } + + // ==================== W2L FACES ==================== + + /// We cache W2L faces here. + /// We'll likely need to LRU cache them. pub fn w2l_face_templates_path(&self) -> &Path { &self.w2l_face_templates_root } - /// End bump, etc. - pub fn video_asset_path(&self) -> &Path { - &self.video_asset_root - } - - pub fn create_w2l_model_path(&self) -> AnyhowResult<()> { - let _ = fs::create_dir_all(self.w2l_model_path())?; - Ok(()) - } - pub fn create_w2l_face_templates_path(&self) -> AnyhowResult<()> { let _ = fs::create_dir_all(self.w2l_face_templates_path())?; Ok(()) } + // ==================== VIDEO ASSETS (End bump, etc) ==================== + + /// There is only a single end bump, and we'll add a watermark file. + pub fn video_asset_path(&self) -> &Path { + &self.video_asset_root + } + pub fn create_video_asset_path(&self) -> AnyhowResult<()> { let _ = fs::create_dir_all(self.video_asset_path())?; Ok(()) diff --git a/src/w2l_inference_job.rs b/src/w2l_inference_job.rs index 350de0c48d..76cc89b125 100644 --- a/src/w2l_inference_job.rs +++ b/src/w2l_inference_job.rs @@ -85,6 +85,7 @@ struct Inferencer { // Command to run pub inference_script: String, + pub w2l_model_filename: String, } #[tokio::main] @@ -178,6 +179,9 @@ async fn main() -> AnyhowResult<()> { semi_persistent_cache.create_w2l_face_templates_path()?; semi_persistent_cache.create_w2l_model_path()?; + let w2l_model_filename = easyenv::get_env_string_or_default( + "W2L_MODEL_FILENAME", "wav2lip_gan.pth"); + let inferencer = Inferencer { download_temp_directory: temp_directory, mysql_pool, @@ -191,6 +195,7 @@ async fn main() -> AnyhowResult<()> { w2l_inference: w2l_inference_command, bucket_path_unifier: BucketPathUnifier::default_paths(), semi_persistent_cache, + w2l_model_filename, }; main_loop(inferencer).await; @@ -299,6 +304,27 @@ async fn process_job(inferencer: &Inferencer, job: &W2lInferenceJobRecord) -> An // TODO 7. Save record // TODO 8. Mark job done + // ==================== CONFIRM OR DOWNLOAD W2L MODEL ==================== // + + let model_filename = inferencer.w2l_model_filename.clone(); + let model_fs_path = inferencer.semi_persistent_cache.w2l_model_path(&model_filename); + + if !model_fs_path.exists() { + info!("Model file does not exist: {:?}", &model_fs_path); + + let model_object_path = inferencer.bucket_path_unifier + .w2l_pretrained_models_path(&model_filename); + + info!("Download from bucket path: {:?}", &model_object_path); + + inferencer.private_bucket_client.download_file_to_disk( + model_object_path.to_str().ok_or(anyhow!("invalid path"))?, + &model_filename).await?; + + info!("Downloaded model from bucket!"); + } + + let temp_dir = format!("temp_{}", job.id); let temp_dir = TempDir::new(&temp_dir)?;