Skip to content

Commit

Permalink
Implement disable partial transcripts
Browse files Browse the repository at this point in the history
  • Loading branch information
Swimburger committed Apr 10, 2024
1 parent 37db09e commit ef1e1d3
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/main/java/com/assemblyai/api/RealtimeTranscriber.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.function.BiConsumer;
import java.util.function.Consumer;

import com.sun.org.apache.xpath.internal.operations.Bool;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
Expand All @@ -33,6 +34,7 @@ public final class RealtimeTranscriber implements AutoCloseable {
private static final OkHttpClient OK_HTTP_CLIENT = new OkHttpClient.Builder().build();
private final String apiKey;
private final int sampleRate;
private final Optional<Boolean> disablePartialTranscripts;
private final Optional<List<String>> wordBoost;
private final Optional<Integer> endUtteranceSilenceThreshold;
private final Consumer<SessionBegins> onSessionBegins;
Expand All @@ -47,6 +49,7 @@ public final class RealtimeTranscriber implements AutoCloseable {
private RealtimeTranscriber(
String apiKey,
int sampleRate,
Optional<Boolean> disablePartialTranscripts,
Optional<List<String>> wordBoost,
Optional<Integer> endUtteranceSilenceThreshold,
Consumer<SessionBegins> onSessionBegins,
Expand All @@ -57,6 +60,7 @@ private RealtimeTranscriber(
BiConsumer<Integer, String> onClose) {
this.apiKey = apiKey;
this.sampleRate = sampleRate;
this.disablePartialTranscripts = disablePartialTranscripts;
this.wordBoost = wordBoost;
this.endUtteranceSilenceThreshold = endUtteranceSilenceThreshold;
this.onSessionBegins = onSessionBegins;
Expand All @@ -73,6 +77,9 @@ private RealtimeTranscriber(
*/
public void connect() {
String url = BASE_URL + "/v2/realtime/ws?sample_rate=" + sampleRate;
if(disablePartialTranscripts.isPresent() && disablePartialTranscripts.get()){
url += "&disable_partial_transcripts=true";
}
if (wordBoost.isPresent() && !wordBoost.get().isEmpty()) {
try {
url += "&word_boost=" + ObjectMappers.JSON_MAPPER.writeValueAsString(wordBoost.get());
Expand Down Expand Up @@ -145,6 +152,7 @@ public static final class Builder {
private static final int DEFAULT_SAMPLE_RATE = 16_000;
private String apiKey;
private Integer sampleRate;
private Optional<Boolean> disablePartialTranscripts;
private List<String> wordBoost;
private Optional<Integer> endUtteranceSilenceThreshold = Optional.empty();
private Consumer<SessionBegins> onSessionBegins;
Expand Down Expand Up @@ -176,6 +184,16 @@ public RealtimeTranscriber.Builder sampleRate(int sampleRate) {
return this;
}

/**
* Disable partial transcripts.
*
* @return this
*/
public RealtimeTranscriber.Builder disablePartialTranscripts() {
this.disablePartialTranscripts = Optional.of(true);
return this;
}

/**
* Sets word boost
*
Expand Down Expand Up @@ -284,6 +302,7 @@ public RealtimeTranscriber build() {
return new RealtimeTranscriber(
apiKey,
sampleRate == null ? DEFAULT_SAMPLE_RATE : sampleRate,
disablePartialTranscripts,
Optional.ofNullable(wordBoost),
endUtteranceSilenceThreshold,
onSessionBegins,
Expand Down

0 comments on commit ef1e1d3

Please sign in to comment.