Skip to content

Commit

Permalink
Simplify word search
Browse files Browse the repository at this point in the history
  • Loading branch information
Swimburger committed Apr 23, 2024
1 parent b44142a commit 64bb37b
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 12 deletions.
16 changes: 10 additions & 6 deletions sample-app/src/main/java/sample/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import com.assemblyai.api.resources.files.types.UploadedFile;
import com.assemblyai.api.resources.lemur.requests.LemurTaskParams;
import com.assemblyai.api.resources.lemur.types.LemurTaskResponse;
import com.assemblyai.api.resources.realtime.requests.CreateRealtimeTemporaryTokenParams;
import com.assemblyai.api.resources.realtime.types.AudioEncoding;
import com.assemblyai.api.resources.realtime.types.RealtimeTemporaryTokenResponse;
import com.assemblyai.api.resources.realtime.types.SessionInformation;
import com.assemblyai.api.resources.transcripts.requests.*;
import com.assemblyai.api.resources.transcripts.types.*;
Expand Down Expand Up @@ -58,12 +60,9 @@ public static void main(String... args) throws IOException, InterruptedException
String vtt = client.transcripts().getSubtitles(transcript.getId(), SubtitleFormat.VTT);
System.out.println("Get transcript vtt. " + vtt);

WordSearchResponse search = client.transcripts().wordSearch(transcript.getId(), WordSearchParams.builder()
.words("NBC")
.build());
WordSearchResponse search = client.transcripts().wordSearch(transcript.getId(), List.of("NBC", "President"));
System.out.println("Search transcript. " + search);


LemurTaskResponse response = client.lemur().task(LemurTaskParams.builder()
.prompt("Summarize this transcript.")
.transcriptIds(List.of(transcript.getId()))
Expand All @@ -89,15 +88,20 @@ public static void main(String... args) throws IOException, InterruptedException
TranscriptList transcripts = client.transcripts().list();
System.out.println("List transcript. " + transcripts);

RealtimeTemporaryTokenResponse tokenResponse = client.realtime().createTemporaryToken(CreateRealtimeTemporaryTokenParams.builder()
.expiresIn(60)
.build()
);

try (RealtimeTranscriber realtimeTranscriber = RealtimeTranscriber.builder()
.apiKey(System.getenv("ASSEMBLYAI_API_KEY"))
.token(tokenResponse.getToken())
.encoding(AudioEncoding.PCM_S16LE)
.onSessionBegins(System.out::println)
.onPartialTranscript(System.out::println)
.onFinalTranscript(System.out::println)
.onError((err) -> System.out.println(err.getMessage()))
.onSessionInformation((info) -> System.out.println(info.getAudioDurationSeconds()))
.onClose((code, reason) -> System.out.printf("%s: %s", code, reason))
.onSessionInformation(System.out::println)
.build()) {
realtimeTranscriber.connect();
streamFile("sample-app/src/main/resources/gore-short.wav", realtimeTranscriber);
Expand Down
77 changes: 71 additions & 6 deletions src/main/java/com/assemblyai/api/PollingTranscriptsClient.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package com.assemblyai.api;

import com.assemblyai.api.core.ClientOptions;
import com.assemblyai.api.core.RequestOptions;
import com.assemblyai.api.resources.files.types.UploadedFile;
import com.assemblyai.api.resources.transcripts.TranscriptsClient;
import com.assemblyai.api.resources.transcripts.requests.TranscriptParams;
import com.assemblyai.api.resources.transcripts.requests.WordSearchParams;
import com.assemblyai.api.resources.transcripts.types.Transcript;
import com.assemblyai.api.resources.transcripts.types.TranscriptOptionalParams;
import com.assemblyai.api.resources.transcripts.types.TranscriptStatus;
import com.assemblyai.api.resources.transcripts.types.WordSearchResponse;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;

public class PollingTranscriptsClient extends TranscriptsClient {

Expand All @@ -23,23 +28,43 @@ public PollingTranscriptsClient(ClientOptions clientOptions, AssemblyAI client)
this.client = client;
}

/*********************************************************************
* Submit methods will immediately return the transcript
*********************************************************************
/**
* Submits a transcription job for an audio file. This will not wait until the transcript status is "completed" or "error".
* @param file Audio file to transcribe
* @return Queued transcript
* @throws IOException The file will be read and an IOException may be thrown.
*/
public Transcript submit(File file) throws IOException {
return submit(file, EMPTY_PARAMS);
}

/**
* Submits a transcription job for an audio file. This will not wait until the transcript status is "completed" or "error".
* @param file Audio file to transcribe
* @param transcriptParams The parameters to transcribe an audio file.
* @return Queued transcript
* @throws IOException The file will be read and an IOException may be thrown.
*/
public Transcript submit(File file, TranscriptOptionalParams transcriptParams) throws IOException {
UploadedFile uploadedFile = client.files().upload(Files.readAllBytes(file.toPath()));
return submit(uploadedFile.getUploadUrl(), transcriptParams);
}

/**
* Submits a transcription job for an audio file. This will not wait until the transcript status is "completed" or "error".
* @param url URL to the audio file to transcribe
* @return Queued transcript
*/
public Transcript submit(String url) {
return submit(url, EMPTY_PARAMS);
}

/**
* Submits a transcription job for an audio file. This will not wait until the transcript status is "completed" or "error".
* @param url URL to the audio file to transcribe
* @param transcriptParams The parameters to transcribe an audio file.
* @return Queued transcript
*/
public Transcript submit(String url, TranscriptOptionalParams transcriptParams) {
TranscriptParams createTranscriptParams = TranscriptParams.builder()
.audioUrl(url)
Expand Down Expand Up @@ -81,23 +106,43 @@ public Transcript submit(String url, TranscriptOptionalParams transcriptParams)
return super.submit(createTranscriptParams);
}

/*********************************************************************
* Transcribe audio. This will create a transcript and wait until the transcript status is "completed" or "error".
*********************************************************************
/**
* Transcribe an audio file. This will create a transcript and wait until the transcript status is "completed" or "error".
* @param file Audio file to transcribe
* @return A transcript with status "completed" or "error"
* @throws IOException The file will be read and an IOException may be thrown.
*/
public Transcript transcribe(File file) throws IOException {
return transcribe(file, EMPTY_PARAMS);
}

/**
* Transcribe an audio file. This will create a transcript and wait until the transcript status is "completed" or "error".
* @param file Audio file to transcribe
* @param transcriptParams The parameters to transcribe an audio file.
* @return A transcript with status "completed" or "error"
* @throws IOException The file will be read and an IOException may be thrown.
*/
public Transcript transcribe(File file, TranscriptOptionalParams transcriptParams) throws IOException {
UploadedFile uploadedFile = client.files().upload(Files.readAllBytes(file.toPath()));
return transcribe(uploadedFile.getUploadUrl(), transcriptParams);
}

/**
* Transcribe an audio file. This will create a transcript and wait until the transcript status is "completed" or "error".
* @param url URL to the audio file to transcribe
* @return A transcript with status "completed" or "error"
*/
public Transcript transcribe(String url) {
return transcribe(url, EMPTY_PARAMS);
}

/**
* Transcribe an audio file. This will create a transcript and wait until the transcript status is "completed" or "error".
* @param url URL to the audio file to transcribe
* @param transcriptParams The parameters to transcribe an audio file.
* @return A transcript with status "completed" or "error"
*/
public Transcript transcribe(String url, TranscriptOptionalParams transcriptParams) {
Transcript transcriptResponse = submit(url, transcriptParams);
return awaitCompletion(transcriptResponse.getId());
Expand All @@ -117,4 +162,24 @@ private Transcript awaitCompletion(String transcriptId) {
throw new RuntimeException("Interrupted while polling transcript id=" + transcriptId);
}
}

/**
* Search through the transcript for a specific set of keywords. You can search for individual words, numbers, or phrases containing up to five words or numbers.
*/
public WordSearchResponse wordSearch(String transcriptId, List<String> words) {
return wordSearch(transcriptId, words, null);
}

/**
* Search through the transcript for a specific set of keywords. You can search for individual words, numbers, or phrases containing up to five words or numbers.
*/
public WordSearchResponse wordSearch(String transcriptId, List<String> words, RequestOptions requestOptions) {
return wordSearch(
transcriptId,
WordSearchParams.builder()
.words(String.join(",", words))
.build(),
requestOptions
);
}
}

0 comments on commit 64bb37b

Please sign in to comment.