Transcribe and Analyze Audio

Process audio files alongside text prompts using a chat model.

Quickstart

Send a text prompt to generate a response from a chat model.

import Bytez from "bytez.js";

const client = new Bytez("YOUR_BYTEZ_KEY_HERE");
const model = client.model("Qwen/Qwen2-Audio-7B-Instruct");

const textInput = [
  {
    role: "system",
    content: [{ type: "text", text: "You are a helpful assistant." }]
  },
  {
    role: "user",
    content: [
      { type: "text", text: "What sound is this?" },
      { type: "audio", url: "https://example.com/path-to-audio.mp3" }
    ]
  }
];

const { error, output } = await model.run(textInput);
if (error) {
  console.error("Error:", error);
} else {
  console.log(output);
}

Streaming

Get real-time responses when analyzing an audio file.

javascript
const stream = await model.run(textInput, params, true);

Node.js Example

javascript
const { Readable } = require('stream');

const stream = await model.run(textInput, params, true);

try {
  const readableStream = Readable.fromWeb(stream); // Convert Web Stream to Node.js Readable Stream
  for await (const chunk of readableStream) {
    console.log(chunk.toString()); // Handle each chunk of data
  }
} catch (error) {
  console.error(error); // Handle errors
}

Browser Example

javascript
const stream = await model.run(textInput, params, true);

try {
  // Check if running in a browser
  const textStream = stream.pipeThrough ? stream.pipeThrough(new TextDecoderStream()) : stream;

  for await (const chunk of textStream) {
    console.log(chunk);
  }
} catch (error) {
  console.error("Streaming error:", error);
}

Key Points

  • Node.js: Convert the Web Stream using Readable.fromWeb() for compatibility.
  • Browser: Use getReader() and TextDecoder to process the stream.
  • Error Handling: Both methods use try…catch to handle potential errors.
  • Data Handling: Data chunks are processed as they arrive via data events or .read() calls.

Explore Specialized Models

You might also be interested in pretrained models for tasks like: