|
| 1 | +// ------------------------------------------------------------------------- |
| 2 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +// Licensed under the MIT License. |
| 4 | +// ------------------------------------------------------------------------- |
| 5 | + |
| 6 | +import { FoundryLocalManager } from '../src/index.js'; |
| 7 | +import path from 'path'; |
| 8 | + |
| 9 | +async function main() { |
| 10 | + let modelToLoad: any = null; |
| 11 | + |
| 12 | + try { |
| 13 | + // Initialize the Foundry Local SDK |
| 14 | + console.log('Initializing Foundry Local SDK...'); |
| 15 | + |
| 16 | + const manager = FoundryLocalManager.create({ |
| 17 | + appName: 'FoundryLocalAudioExample', |
| 18 | + logLevel: 'info' |
| 19 | + }); |
| 20 | + console.log('✓ SDK initialized successfully'); |
| 21 | + |
| 22 | + // Explore available models |
| 23 | + console.log('\nFetching available models...'); |
| 24 | + const catalog = manager.catalog; |
| 25 | + const models = await catalog.getModels(); |
| 26 | + |
| 27 | + console.log(`Found ${models.length} models:`); |
| 28 | + for (const model of models) { |
| 29 | + const variants = model.variants.map((v: any) => v.id).join(', '); |
| 30 | + console.log(` - ${model.alias} (variants: ${variants})`); |
| 31 | + } |
| 32 | + |
| 33 | + const modelAlias = 'whisper-tiny'; |
| 34 | + |
| 35 | + // Get the Whisper model |
| 36 | + console.log(`\nLoading model ${modelAlias}...`); |
| 37 | + modelToLoad = await catalog.getModel(modelAlias); |
| 38 | + if (!modelToLoad) { |
| 39 | + throw new Error(`Model ${modelAlias} not found`); |
| 40 | + } |
| 41 | + |
| 42 | + // Download if not cached |
| 43 | + if (!modelToLoad.isCached) { |
| 44 | + console.log('Downloading model...'); |
| 45 | + await modelToLoad.download((progress: number) => { |
| 46 | + process.stdout.write(`\rDownload: ${progress.toFixed(1)}%`); |
| 47 | + }); |
| 48 | + console.log(); |
| 49 | + } |
| 50 | + |
| 51 | + await modelToLoad.load(); |
| 52 | + console.log('✓ Model loaded'); |
| 53 | + |
| 54 | + // Create audio client |
| 55 | + console.log('\nCreating audio client...'); |
| 56 | + const audioClient = modelToLoad.createAudioClient(); |
| 57 | + |
| 58 | + // Configure settings |
| 59 | + audioClient.settings.language = 'en'; |
| 60 | + audioClient.settings.temperature = 0.0; // deterministic results |
| 61 | + |
| 62 | + console.log('✓ Audio client created'); |
| 63 | + |
| 64 | + // Audio file path — update this to point to your audio file |
| 65 | + const audioFilePath = path.join(process.cwd(), '..', 'testdata', 'Recording.mp3'); |
| 66 | + |
| 67 | + // Example: Standard transcription |
| 68 | + console.log('\nTesting standard transcription...'); |
| 69 | + const result = await audioClient.transcribe(audioFilePath); |
| 70 | + console.log('\nTranscription result:'); |
| 71 | + console.log(result.text); |
| 72 | + |
| 73 | + // Example: Streaming transcription |
| 74 | + console.log('\nTesting streaming transcription...'); |
| 75 | + await audioClient.transcribeStreaming(audioFilePath, (chunk: any) => { |
| 76 | + process.stdout.write(chunk.text); |
| 77 | + }); |
| 78 | + console.log('\n'); |
| 79 | + |
| 80 | + // Unload the model |
| 81 | + console.log('Unloading model...'); |
| 82 | + await modelToLoad.unload(); |
| 83 | + console.log(`✓ Model unloaded`); |
| 84 | + |
| 85 | + console.log('\n✓ Audio transcription example completed successfully'); |
| 86 | + |
| 87 | + } catch (error) { |
| 88 | + console.log('Error running example:', error); |
| 89 | + if (error instanceof Error && error.stack) { |
| 90 | + console.log(error.stack); |
| 91 | + } |
| 92 | + // Best-effort cleanup |
| 93 | + if (modelToLoad) { |
| 94 | + try { await modelToLoad.unload(); } catch { /* ignore */ } |
| 95 | + } |
| 96 | + process.exit(1); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +// Run the example |
| 101 | +main().catch(console.error); |
| 102 | + |
| 103 | +export { main }; |
0 commit comments