ニジカ投稿局 https://tv.nizika.tv
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

103 lines
2.6 KiB

  1. import { SimpleLogger } from '@peertube/peertube-models'
  2. import { buildSUUID, SUUID } from '@peertube/peertube-node-utils'
  3. import { $ } from 'execa'
  4. import { PerformanceObserver } from 'node:perf_hooks'
  5. import { join } from 'path'
  6. import { TranscriptFile, TranscriptFormat } from './transcript-file.js'
  7. import { TranscriptionEngine } from './transcription-engine.js'
  8. import { TranscriptionModel } from './transcription-model.js'
  9. import { TranscriptionRun } from './transcription-run.js'
  10. export interface TranscribeArgs {
  11. mediaFilePath: string
  12. model: TranscriptionModel
  13. format: TranscriptFormat
  14. transcriptDirectory: string
  15. language?: string
  16. runId?: SUUID
  17. }
  18. export abstract class AbstractTranscriber {
  19. engine: TranscriptionEngine
  20. protected binDirectory: string
  21. protected enginePath: string
  22. protected logger: SimpleLogger
  23. protected performanceObserver?: PerformanceObserver
  24. protected run?: TranscriptionRun
  25. constructor (options: {
  26. engine: TranscriptionEngine
  27. binDirectory?: string
  28. enginePath?: string
  29. logger: SimpleLogger
  30. performanceObserver?: PerformanceObserver
  31. }) {
  32. const { engine, logger, enginePath, binDirectory, performanceObserver } = options
  33. this.engine = engine
  34. this.enginePath = enginePath
  35. this.logger = logger
  36. this.binDirectory = binDirectory
  37. this.performanceObserver = performanceObserver
  38. }
  39. createRun (uuid: SUUID = buildSUUID()) {
  40. this.run = new TranscriptionRun(this.logger, uuid)
  41. }
  42. startRun () {
  43. this.run.start()
  44. }
  45. stopRun () {
  46. this.run.stop()
  47. delete this.run
  48. }
  49. assertLanguageDetectionAvailable (language?: string) {
  50. if (!this.engine.languageDetection && !language) {
  51. throw new Error(`Language detection isn't available in ${this.engine.name}. A language must me provided explicitly.`)
  52. }
  53. }
  54. supports (model: TranscriptionModel) {
  55. return model.format === 'PyTorch'
  56. }
  57. protected getEngineBinary () {
  58. if (this.enginePath) return this.enginePath
  59. if (this.binDirectory) return join(this.binDirectory, this.engine.command)
  60. return this.engine.command
  61. }
  62. protected getExec (env?: { [ id: string ]: string }) {
  63. const logLevels = {
  64. command: 'debug',
  65. output: 'debug',
  66. ipc: 'debug',
  67. error: 'error',
  68. duration: 'debug'
  69. }
  70. return $({
  71. verbose: (_verboseLine, { message, ...verboseObject }) => {
  72. const level = logLevels[verboseObject.type]
  73. this.logger[level](message, verboseObject)
  74. },
  75. env
  76. })
  77. }
  78. abstract transcribe (options: TranscribeArgs): Promise<TranscriptFile>
  79. abstract install (path: string): Promise<void>
  80. }