Skip to content

Commit

Permalink
Add: Azure speech cognitive service - get transcription report (v3.1)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongdongy committed Mar 28, 2023
1 parent e911c24 commit e5dfb30
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 10 deletions.
4 changes: 2 additions & 2 deletions examples/azure/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ name = "azure-text-to-speech"
path = "src/text-to-speech.rs"

[[bin]]
name = "azure-batch-create-transcription"
path = "src/batch-create-transcription.rs"
name = "azure-batch-transcription"
path = "src/batch-transcription.rs"
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,22 @@ async fn main() -> Result<(), Box<dyn Error>> {
.await?;

std::thread::sleep(Duration::from_secs(5));

// Check transcription job status.
let trans = trans.status().await?;
if let Some(Status::Succeeded) = trans.status {
// Get transcription result files.
let results = trans.files().await?;
let files = results.values;
let files = results.values.clone();

if files.len() > 0 {
println!("{:#?}", files.get(0).unwrap().file().await?);
// Get transcription report.
let report = results.report().await?;
println!("{:#?}", report);

// Get transcription result file via individual API endpoint.
let file = files.get(0).unwrap().file().await?;
println!("{:#?}", file);
}
}

Expand Down
44 changes: 42 additions & 2 deletions rust-ai/src/azure/apis/speech.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ use crate::azure::{
common::{MicrosoftOutputFormat, ResponseExpectation, ResponseType},
speech::{
entity::EntityReference,
file::{File, PaginatedFiles},
file::{File, FileKind, PaginatedFiles},
filter::FilterOperator,
health::ServiceHealth,
transcription::{Status, Transcription},
transcription::{Status, Transcription, TranscriptionReport},
ErrorResponse,
},
tts::Voice,
Expand Down Expand Up @@ -414,6 +414,46 @@ impl PaginatedFiles {
Ok(None)
}
}

pub async fn report(self) -> Result<Option<TranscriptionReport>, Box<dyn std::error::Error>> {
if self.values.len() == 0 {
return Ok(None);
}

let mut report = self
.values
.iter()
.filter(|file| file.kind == FileKind::TranscriptionReport);

if let Some(report) = report.next().clone() {
let text = request_get_endpoint(
&SpeechServiceEndpoint::None,
None,
Some(report.links.content_url.clone()),
)
.await?;

return match serde_json::from_str::<TranscriptionReport>(&text) {
Ok(report) => Ok(Some(report)),
Err(e) => {
warn!(target: "azure", "Unable to parse transcription result file: `{:#?}`", e);
match serde_json::from_str::<ErrorResponse>(&text) {
Ok(error) => {
println!("{:#?}", error);
error!(target: "azure", "Error from Azure: `{:?}`", e);
Err(Box::new(e))
}
Err(e) => {
error!(target: "azure", "Unable to parse error response: `{:?}`", e);
Err(Box::new(e))
}
}
}
};
} else {
Ok(None)
}
}
}

impl File {
Expand Down
14 changes: 11 additions & 3 deletions rust-ai/src/azure/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub enum SpeechServiceEndpoint {
Get_Transcription_v3_1,
Get_Transcription_Files_v3_1,
Get_Transcription_File_v3_1,
None,
}

impl SpeechServiceEndpoint {
Expand Down Expand Up @@ -56,20 +57,24 @@ impl SpeechServiceEndpoint {
"https://{}.api.cognitive.microsoft.com/speechtotext/v3.1/transcriptions/",
region
),

Self::Get_Transcription_Files_v3_1 => format!(
"https://{}.api.cognitive.microsoft.com/speechtotext/v3.1/transcriptions/",
region
),

Self::Get_Transcription_File_v3_1 => format!(
"https://{}.api.cognitive.microsoft.com/speechtotext/v3.1/transcriptions/",
region
),
Self::None => String::new(),
}
}
}

/// If you would like to use a plain request URI, pass
/// [`SpeechServiceEndpoint::None`] as the endpoint, and then provide the
/// complete URI through `url_suffix` parameter.
pub async fn request_get_endpoint(
endpoint: &SpeechServiceEndpoint,
params: Option<HashMap<String, String>>,
Expand All @@ -78,7 +83,10 @@ pub async fn request_get_endpoint(
let config = Config::load().unwrap();
let region = config.azure.speech.region;

let mut url = endpoint.build(&region);
let mut url = match endpoint {
SpeechServiceEndpoint::None => String::new(),
_ => endpoint.build(&region),
};

if let Some(url_suffix) = url_suffix {
url.push_str(&url_suffix);
Expand Down
2 changes: 1 addition & 1 deletion rust-ai/src/azure/types/speech/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl File {
}

/// Type of data.
#[derive(Clone, Debug, Deserialize, Serialize)]
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
pub enum FileKind {
/// Type of data is dataset report.
DatasetReport,
Expand Down
17 changes: 17 additions & 0 deletions rust-ai/src/azure/types/speech/transcription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,3 +355,20 @@ impl Default for TranscriptionProperties {
}
}
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct TranscriptionReport {
#[serde(rename = "successfulTranscriptionsCount")]
pub successful_transcriptions_count: usize,

#[serde(rename = "failedTranscriptionsCount")]

pub failed_transcriptions_count: usize,
pub details: Vec<TranscriptionReportDetail>,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct TranscriptionReportDetail {
pub source: String,
pub status: String,
}

0 comments on commit e5dfb30

Please sign in to comment.