-
Notifications
You must be signed in to change notification settings - Fork 688
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
220 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
extensions/audio/src/main/java/ai/djl/audio/translator/WhisperTranslatorFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
extensions/audio/src/main/java/ai/djl/audio/translator/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* | ||
* Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance | ||
* with the License. A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0/ | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES | ||
* OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions | ||
* and limitations under the License. | ||
*/ | ||
|
||
/** Contains translators for audio processing. */ | ||
package ai.djl.audio.translator; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
extensions/spark/src/main/scala/ai/djl/spark/task/audio/BaseAudioPredictor.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance | ||
* with the License. A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0/ | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES | ||
* OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions | ||
* and limitations under the License. | ||
*/ | ||
package ai.djl.spark.task.audio | ||
|
||
import ai.djl.modality.audio.Audio | ||
import ai.djl.spark.task.BasePredictor | ||
import org.apache.spark.ml.util.Identifiable | ||
|
||
/** | ||
* BaseAudioPredictor is the base class for audio predictors. | ||
* | ||
* @param uid An immutable unique ID for the object and its derivatives. | ||
*/ | ||
abstract class BaseAudioPredictor[B](override val uid: String) extends BasePredictor[Audio, B] { | ||
|
||
def this() = this(Identifiable.randomUID("BaseAudioPredictor")) | ||
|
||
setDefault(inputClass, classOf[Audio]) | ||
} |
130 changes: 130 additions & 0 deletions
130
extensions/spark/src/main/scala/ai/djl/spark/task/audio/SpeechRecognizer.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/* | ||
* Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance | ||
* with the License. A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0/ | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES | ||
* OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions | ||
* and limitations under the License. | ||
*/ | ||
package ai.djl.spark.task.audio | ||
|
||
import ai.djl.modality.audio.AudioFactory | ||
import ai.djl.modality.audio.translator.SpeechRecognitionTranslatorFactory | ||
import org.apache.spark.ml.param.IntParam | ||
import org.apache.spark.ml.param.shared.{HasInputCol, HasOutputCol} | ||
import org.apache.spark.ml.util.Identifiable | ||
import org.apache.spark.sql.{DataFrame, Dataset, Row} | ||
import org.apache.spark.sql.types.{BinaryType, StringType, StructField, StructType} | ||
|
||
import java.io.ByteArrayInputStream | ||
|
||
/** | ||
* SpeechRecognizer performs speech recognition on audio. | ||
* | ||
* @param uid An immutable unique ID for the object and its derivatives. | ||
*/ | ||
class SpeechRecognizer(override val uid: String) extends BaseAudioPredictor[String] | ||
with HasInputCol with HasOutputCol { | ||
|
||
def this() = this(Identifiable.randomUID("SpeechRecognizer")) | ||
|
||
final val channels = new IntParam(this, "channels", "The number of channels") | ||
final val sampleRate = new IntParam(this, "sampleRate", "The audio sample rate") | ||
final val sampleFormat = new IntParam(this, "sampleFormat", "The audio sample format") | ||
|
||
protected var inputColIndex: Int = _ | ||
|
||
/** | ||
* Sets the inputCol parameter. | ||
* | ||
* @param value the value of the parameter | ||
*/ | ||
def setInputCol(value: String): this.type = set(inputCol, value) | ||
|
||
/** | ||
* Sets the outputCol parameter. | ||
* | ||
* @param value the value of the parameter | ||
*/ | ||
def setOutputCol(value: String): this.type = set(outputCol, value) | ||
|
||
/** | ||
* Sets the channels parameter. | ||
* | ||
* @param value the value of the parameter | ||
*/ | ||
def setChannels(value: Int): this.type = set(channels, value) | ||
|
||
/** | ||
* Sets the sampleRate parameter. | ||
* | ||
* @param value the value of the parameter | ||
*/ | ||
def setSampleRate(value: Int): this.type = set(sampleRate, value) | ||
|
||
/** | ||
* Sets the sampleFormat parameter. | ||
* | ||
* @param value the value of the parameter | ||
*/ | ||
def setSampleFormat(value: Int): this.type = set(sampleFormat, value) | ||
|
||
setDefault(outputClass, classOf[String]) | ||
setDefault(translatorFactory, new SpeechRecognitionTranslatorFactory()) | ||
|
||
/** | ||
* Performs speech recognition on the provided dataset. | ||
* | ||
* @param dataset input dataset | ||
* @return output dataset | ||
*/ | ||
def recognize(dataset: Dataset[_]): DataFrame = { | ||
transform(dataset) | ||
} | ||
|
||
/** @inheritdoc */ | ||
override def transform(dataset: Dataset[_]): DataFrame = { | ||
inputColIndex = dataset.schema.fieldIndex($(inputCol)) | ||
super.transform(dataset) | ||
} | ||
|
||
/** | ||
* Transforms the rows. | ||
* | ||
* @param iter the rows to transform | ||
* @return the transformed rows | ||
*/ | ||
override protected def transformRows(iter: Iterator[Row]): Iterator[Row] = { | ||
val predictor = model.newPredictor() | ||
iter.map(row => { | ||
val data = row.getAs[Array[Byte]](inputColIndex) | ||
val audioFactory = AudioFactory.newInstance | ||
if (isDefined(channels)) { | ||
audioFactory.setChannels($(channels)) | ||
} | ||
if (isDefined(sampleRate)) { | ||
audioFactory.setSampleRate($(sampleRate)) | ||
} | ||
if (isDefined(sampleFormat)) { | ||
audioFactory.setSampleFormat($(sampleFormat)) | ||
} | ||
val audio = audioFactory.fromInputStream(new ByteArrayInputStream(data)) | ||
Row.fromSeq(row.toSeq :+ predictor.predict(audio)) | ||
}) | ||
} | ||
|
||
/** @inheritdoc */ | ||
def validateInputType(schema: StructType): Unit = { | ||
validateType(schema($(inputCol)), BinaryType) | ||
} | ||
|
||
/** @inheritdoc */ | ||
override def transformSchema(schema: StructType): StructType = { | ||
val outputSchema = StructType(schema.fields :+ StructField($(outputCol), StringType)) | ||
outputSchema | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
extensions/spark/src/main/scala/ai/djl/spark/task/audio/WhisperSpeechRecognizer.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance | ||
* with the License. A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0/ | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES | ||
* OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions | ||
* and limitations under the License. | ||
*/ | ||
package ai.djl.spark.task.audio | ||
|
||
import ai.djl.audio.translator.WhisperTranslatorFactory | ||
import org.apache.spark.ml.util.Identifiable | ||
|
||
/** | ||
* WhisperSpeechRecognizer is very similar to the SpeechRecognizer that performs speech recognition on audio, | ||
* except that this API is specially tailored for OpenAI Whisper related models. | ||
* | ||
* @param uid An immutable unique ID for the object and its derivatives. | ||
*/ | ||
class WhisperSpeechRecognizer(override val uid: String) extends SpeechRecognizer { | ||
|
||
def this() = this(Identifiable.randomUID("WhisperSpeechRecognizer")) | ||
|
||
setDefault(translatorFactory, new WhisperTranslatorFactory()) | ||
} |