From 191532c392413c796039ba015d6e08cfa4cbb861 Mon Sep 17 00:00:00 2001 From: Matt Nemitz Date: Fri, 8 Sep 2023 11:37:19 +0100 Subject: [PATCH 1/3] Allow optional file name when creating job --- examples/example_batch.js | 10 ++++++++-- src/batch/client.ts | 15 ++++++++++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/examples/example_batch.js b/examples/example_batch.js index 1d2ef62..54cdf02 100755 --- a/examples/example_batch.js +++ b/examples/example_batch.js @@ -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", @@ -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(' ')); }) diff --git a/src/batch/client.ts b/src/batch/client.ts index e56a248..dc292d4 100644 --- a/src/batch/client.ts +++ b/src/batch/client.ts @@ -106,6 +106,7 @@ export class BatchTranscription { */ async transcribe({ input, + fileName, transcription_config, translation_config, output_config, @@ -119,6 +120,7 @@ export class BatchTranscription { const submitResponse = await this.createJob({ input: fileOrFetchConfig, + fileName, transcription_config, translation_config, output_config, @@ -160,6 +162,7 @@ export class BatchTranscription { async createJob({ input, + fileName, transcription_config, translation_config, output_config, @@ -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)); @@ -225,10 +228,20 @@ export type TranscriptionFormat = 'json-v2' | 'text' | 'srt'; export type TranscribeConfig = Omit & { 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 & { 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; }; From 7d7560a8ffadbf580f07116064ca61cba04f9ea9 Mon Sep 17 00:00:00 2001 From: Matt Nemitz Date: Fri, 8 Sep 2023 14:04:26 +0100 Subject: [PATCH 2/3] Add test --- tests/index.test.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/index.test.ts b/tests/index.test.ts index f48369f..3d85c17 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -6,9 +6,10 @@ 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), ), ]); @@ -17,9 +18,11 @@ 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(transcription.job.data_name).toEqual(exampleFileName); }, 30000); it('lists jobs', async () => { From 276cce127b26c7f126ef534be07057320f9b47ec Mon Sep 17 00:00:00 2001 From: Matt Nemitz Date: Mon, 11 Sep 2023 17:14:09 +0100 Subject: [PATCH 3/3] Fix type --- tests/index.test.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/tests/index.test.ts b/tests/index.test.ts index 3d85c17..4e4577d 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -1,4 +1,8 @@ -import { RealtimeSession, AddTranscript } from '../dist'; +import { + RealtimeSession, + AddTranscript, + RetrieveTranscriptResponse, +} from '../dist'; import { Speechmatics } from '../dist'; import dotenv from 'dotenv'; import fs from 'fs'; @@ -22,7 +26,10 @@ describe('Testing batch capabilities', () => { transcription_config: { language: 'en' }, }); expect(transcription).toBeDefined(); - expect(transcription.job.data_name).toEqual(exampleFileName); + expect(typeof transcription).toBe('object'); + expect((transcription as RetrieveTranscriptResponse).job.data_name).toEqual( + exampleFileName, + ); }, 30000); it('lists jobs', async () => {