Giter Club home page Giter Club logo

assemblyai-java-sdk's Introduction

AssemblyAI Java Library

Maven Central fern shield GitHub License AssemblyAI Twitter AssemblyAI YouTube Discord

Documentation

API reference documentation is available here.

Installation

Gradle

Add the dependency in your build.gradle:

dependencies {
    implementation 'com.assemblyai:assemblyai-java:1.x.x'
}

Maven

Add the dependency in your pom.xml:

<dependency>
    <groupId>com.assemblyai</groupId>
    <artifactId>assemblyai-java</artifactId>
    <version>1.x.x</version>
</dependency>

HTTP Client Usage

The SDK exports a vanilla HTTP client, AssemblyAI. You can use this to call into each of our API endpoints and get typed responses back.

import com.assemblyai.api.AssemblyAI;

AssemblyAI aai = AssemblyAI.builder()
  .apiKey("YOUR_API_KEY")
  .build();

TranscriptResponse transcriptResponse =
    aai.transcript().get("transcript-id");

System.out.printlin("Received response!" + transcriptResponse);

Handling Errors

When the API returns a non-success status code (4xx or 5xx response), a subclass of ApiError will be thrown:

import com.assemblyai.api.core.ApiError;

try {
  aai.transcript().get("transcript-id");
} catch (ApiError error) {
  System.out.println(error.getBody());
  System.out.println(error.getStatusCode());
}

Creating a transcript

When you create a transcript, you can either pass in a URL to an audio file or upload a file directly.

import com.assemblyai.api.types.Transcript;

// Transcribe file at remote URL
Transcript transcript = aai.transcripts().transcribe(
        "https://storage.googleapis.com/aai-web-samples/espn-bears.m4a");

// Upload a file via local path and transcribe
transcript = aai.transcripts().transcribe(
        new File("./news.mp4"));

transcribe queues a transcription job and polls it until the status is completed or error. If you don't want to wait until the transcript is ready, you can use submit:

import com.assemblyai.api.types.Transcript;

// Transcribe file at remote URL
Transcript transcript = aai.transcripts().submit(
        "https://storage.googleapis.com/aai-web-samples/espn-bears.m4a");

// Upload a file via local path and transcribe
transcript = aai.transcripts().submit(
        new File("./news.mp4"));

Using the Realtime Transcriber

The Realtime Transcriber can be used to process any live audio streams and sends data over websockets. The Realtime Transcriber will take event handlers

import com.assemblyai.api.Transcriber;

RealtimeTranscriber realtime = RealtimeTranscriber.builder()
  .apiKey("YOUR_API_KEY")
  .onPartialTranscript(partial -> System.out.println(partial))
  .onFinalTranscript(finalTranscript -> System.out.println(finalTranscript))
  .build();

realtime.sendAudio(new byte[]{...});

realtime.close();

Staged Builders

The generated builders all follow the staged builder pattern. Read more here. Staged builders only allow you to construct the object once all required properties have been specified.

For example, in the snippet below, you will not be able to access the build method on CreateTranscriptParameters until you have specified the mandatory audioUrl variable.

import com.assemblyai.api.TranscriptParams;

TranscriptParams params = TranscriptParams.builder()
  .audioUrl("https://...")
  .build();

Contributing

While we value open-source contributions to this SDK, this library is generated programmatically. Additions made directly to this library would have to be moved over to our generation code, otherwise they would be overwritten upon the next generated release. Feel free to open a PR as a proof of concept, but know that we will not be able to merge it as-is. We suggest opening an issue first to discuss with us!

On the other hand, contributions to the README are always very welcome!

assemblyai-java-sdk's People

Contributors

fern-api[bot] avatar swimburger avatar dsinghvi avatar armandobelardo avatar dannysheridan avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.