Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow optional file name when creating job #7

Merged
merged 3 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions examples/example_batch.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const { Speechmatics } = require('../dist');
const fs = require('fs');
const path = require('path');

const fileName = 'example.wav';

if (parseInt(process.version.match(/(?:v)([0-9]{2})/)[1]) < 18) {
throw new Error(
"Requires node 18 or higher. If this isn't possible, see our documentation about polyfilling",
Expand All @@ -13,11 +15,15 @@ if (parseInt(process.version.match(/(?:v)([0-9]{2})/)[1]) < 18) {

const sm = new Speechmatics(process.env.API_KEY);
const inputFile = new Blob([
fs.readFileSync(path.join(__dirname, 'example_files', 'example.wav')),
fs.readFileSync(path.join(__dirname, 'example_files', fileName)),
]);

sm.batch
.transcribe({ input: inputFile, transcription_config: { language: 'en' } })
.transcribe({
input: inputFile,
fileName,
transcription_config: { language: 'en' },
})
.then(({ results }) => {
console.log(results.map((r) => r.alternatives[0].content).join(' '));
})
Expand Down
15 changes: 14 additions & 1 deletion src/batch/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export class BatchTranscription {
*/
async transcribe({
input,
fileName,
transcription_config,
translation_config,
output_config,
Expand All @@ -119,6 +120,7 @@ export class BatchTranscription {

const submitResponse = await this.createJob({
input: fileOrFetchConfig,
fileName,
transcription_config,
translation_config,
output_config,
Expand Down Expand Up @@ -160,6 +162,7 @@ export class BatchTranscription {

async createJob({
input,
fileName,
transcription_config,
translation_config,
output_config,
Expand All @@ -180,7 +183,7 @@ export class BatchTranscription {
if ('url' in input) {
config.fetch_data = input;
} else {
formData.append('data_file', input);
formData.append('data_file', input, fileName);
}
formData.append('config', JSON.stringify(config));

Expand Down Expand Up @@ -225,10 +228,20 @@ export type TranscriptionFormat = 'json-v2' | 'text' | 'srt';

export type TranscribeConfig = Omit<JobConfig, 'type'> & {
input: Blob | { fetch: DataFetchConfig };
/**
* Optional file name when passing a raw Blob.
* Note that when passing a `File` object, this is not necessary, as the File's name will be used.
*/
fileName?: string;
language?: ISO639_1_Language;
format?: TranscriptionFormat;
};

export type CreateJobConfig = Omit<JobConfig, 'type'> & {
input: Blob | DataFetchConfig;
/**
* Optional file name when passing a raw Blob.
* Note that when passing a `File` object, this is not necessary, as the File's name will be used.
*/
fileName?: string;
};
14 changes: 12 additions & 2 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import { RealtimeSession, AddTranscript } from '../dist';
import {
RealtimeSession,
AddTranscript,
RetrieveTranscriptResponse,
} from '../dist';
import { Speechmatics } from '../dist';
import dotenv from 'dotenv';
import fs from 'fs';
import path from 'path';

dotenv.config();

const exampleFileName = 'example.wav';
const EXAMPLE_FILE = new Blob([
fs.readFileSync(
path.join(__dirname, '..', 'examples', 'example_files', 'example.wav'),
path.join(__dirname, '..', 'examples', 'example_files', exampleFileName),
),
]);

Expand All @@ -17,9 +22,14 @@ describe('Testing batch capabilities', () => {
const speechmatics = new Speechmatics(process.env.API_KEY as string);
const transcription = await speechmatics.batch.transcribe({
input: EXAMPLE_FILE,
fileName: exampleFileName,
transcription_config: { language: 'en' },
});
expect(transcription).toBeDefined();
expect(typeof transcription).toBe('object');
expect((transcription as RetrieveTranscriptResponse).job.data_name).toEqual(
exampleFileName,
);
}, 30000);

it('lists jobs', async () => {
Expand Down
Loading