Files
artcraft-services/src/script_execution/tacotron_inference_command.rs
T
2021-06-01 14:23:16 -04:00

126 lines
3.7 KiB
Rust

use crate::AnyhowResult;
use log::{info,warn};
use std::process::{Command, Stdio};
use subprocess::{Popen, PopenConfig, Redirection};
use std::fs::OpenOptions;
use std::path::Path;
/// This command is used to run inference.
/// It uses preprocessed face files so that it's much faster.
#[derive(Clone)]
pub struct TacotronInferenceCommand {
tacotron_directory: String,
inference_script_name: String,
}
impl TacotronInferenceCommand {
pub fn new(
tacotron_directory: &str,
inference_script_name: &str,
) -> Self {
Self {
tacotron_directory: tacotron_directory.to_string(),
inference_script_name: inference_script_name.to_string(),
}
}
pub fn execute<P: AsRef<Path>>(
&self,
synthesizer_checkpoint_path: P,
vocoder_checkpoint_path: P,
input_text_filename: P,
output_audio_filename: P,
output_spectrogram_filename: P,
output_metadata_filename: P,
spawn_process: bool
) -> AnyhowResult<()> {
let mut command = String::new();
command.push_str("echo 'test'");
command.push_str(" && ");
command.push_str(&format!("cd {}", self.tacotron_directory));
command.push_str(" && ");
command.push_str("source python/bin/activate");
command.push_str(" && ");
command.push_str("python ");
command.push_str(&self.inference_script_name);
command.push_str(" --synthesizer_checkpoint_path ");
command.push_str(&synthesizer_checkpoint_path.as_ref().display().to_string());
command.push_str(" --vocoder_checkpoint_path ");
command.push_str(&vocoder_checkpoint_path.as_ref().display().to_string());
command.push_str(" --input_text_filename ");
command.push_str(&input_text_filename.as_ref().display().to_string());
command.push_str(" --output_audio_filename ");
command.push_str(&output_audio_filename.as_ref().display().to_string());
command.push_str(" --output_spectrogram_filename ");
command.push_str(&output_spectrogram_filename.as_ref().display().to_string());
command.push_str(" --output_metadata_filename ");
command.push_str(&output_metadata_filename.as_ref().display().to_string());
info!("Command: {:?}", command);
let command_parts = [
"bash",
"-c",
&command
];
if spawn_process {
// NB: This forks and returns immediately.
//let _child_pid = command_builder.spawn()?;
let stdout_file = OpenOptions::new()
.read(true)
.write(true)
.create(true)
.truncate(true)
.open("/tmp/tacotron_upload_stdout.txt")?;
let stderr_file = OpenOptions::new()
.read(true)
.write(true)
.create(true)
.truncate(true)
.open("/tmp/tacotron_upload_stderr.txt")?;
let mut p = Popen::create(&command_parts, PopenConfig {
//stdout: Redirection::Pipe,
//stderr: Redirection::Pipe,
stdout: Redirection::File(stdout_file),
stderr: Redirection::File(stderr_file),
..Default::default()
})?;
info!("Pid : {:?}", p.pid());
p.detach();
} else {
// NB: This is a blocking call.
/*let output = command_builder.output()?;
info!("Output status: {}", output.status);
info!("Stdout: {:?}", String::from_utf8(output.stdout));
error!("Stderr: {:?}", String::from_utf8(output.stderr));
if !output.status.success() {
bail!("Bad error code: {:?}", output.status);
}*/
let mut p = Popen::create(&command_parts, PopenConfig {
//stdout: Redirection::Pipe,
//stderr: Redirection::Pipe,
..Default::default()
})?;
info!("Pid : {:?}", p.pid());
let exit_status = p.wait()?;
info!("Exit status: {:?}", exit_status);
}
Ok(())
}
}