Skip to content

Commit

Permalink
PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Chocobozzz committed Jun 27, 2024
1 parent 3f1aa5d commit a252262
Show file tree
Hide file tree
Showing 18 changed files with 99 additions and 35 deletions.
33 changes: 30 additions & 3 deletions client/src/app/+admin/overview/videos/video-list.component.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DatePipe, NgClass, NgFor, NgIf } from '@angular/common'
import { Component, OnInit, ViewChild } from '@angular/core'
import { ActivatedRoute, Router, RouterLink } from '@angular/router'
import { AuthService, ConfirmService, Notifier, RestPagination, RestTable } from '@app/core'
import { AuthService, ConfirmService, Notifier, RestPagination, RestTable, ServerService } from '@app/core'
import { formatICU, getAbsoluteAPIUrl } from '@app/helpers'
import { Video } from '@app/shared/shared-main/video/video.model'
import { VideoService } from '@app/shared/shared-main/video/video.service'
Expand All @@ -27,6 +27,7 @@ import {
VideoActionsDropdownComponent
} from '../../../shared/shared-video-miniature/video-actions-dropdown.component'
import { VideoAdminService } from './video-admin.service'
import { VideoCaptionService } from '@app/shared/shared-main/video-caption/video-caption.service'

@Component({
selector: 'my-video-list',
Expand Down Expand Up @@ -81,7 +82,8 @@ export class VideoListComponent extends RestTable <Video> implements OnInit {
removeFiles: true,
transcoding: true,
studio: true,
stats: true
stats: true,
generateTranscription: true
}

loading = true
Expand All @@ -94,7 +96,9 @@ export class VideoListComponent extends RestTable <Video> implements OnInit {
private notifier: Notifier,
private videoService: VideoService,
private videoAdminService: VideoAdminService,
private videoBlockService: VideoBlockService
private videoBlockService: VideoBlockService,
private videoCaptionService: VideoCaptionService,
private server: ServerService
) {
super()
}
Expand All @@ -103,6 +107,10 @@ export class VideoListComponent extends RestTable <Video> implements OnInit {
return this.auth.getUser()
}

get serverConfig () {
return this.server.getHTMLConfig()
}

ngOnInit () {
this.initialize()

Expand Down Expand Up @@ -154,6 +162,14 @@ export class VideoListComponent extends RestTable <Video> implements OnInit {
isDisplayed: videos => videos.every(v => v.canRemoveFiles(this.authUser)),
iconName: 'delete'
}
],
[
{
label: $localize`Generate caption`,
handler: videos => this.generateCaption(videos),
isDisplayed: videos => videos.every(v => v.canGenerateTranscription(this.authUser, this.serverConfig.videoTranscription.enabled)),
iconName: 'video-lang'
}
]
]
}
Expand Down Expand Up @@ -371,4 +387,15 @@ export class VideoListComponent extends RestTable <Video> implements OnInit {
error: err => this.notifier.error(err.message)
})
}

private generateCaption (videos: Video[]) {
this.videoCaptionService.generateCaption(videos.map(v => v.id))
.subscribe({
next: () => {
this.notifier.success($localize`Transcription jobs created.`)
},

error: err => this.notifier.error(err.message)
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@

&.with-icon {
@include dropdown-with-icon-item;

.icon-video-lang {
top: 0;
}
}

a,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { Observable, of } from 'rxjs'
import { catchError, map, switchMap } from 'rxjs/operators'
import { HttpClient } from '@angular/common/http'
import { Injectable } from '@angular/core'
import { RestExtractor, ServerService } from '@app/core'
import { objectToFormData } from '@app/helpers'
import { peertubeTranslate, sortBy } from '@peertube/peertube-core-utils'
import { ResultList, VideoCaption } from '@peertube/peertube-models'
import { Observable, from, of } from 'rxjs'
import { catchError, concatMap, map, switchMap, toArray } from 'rxjs/operators'
import { environment } from '../../../../environments/environment'
import { VideoCaptionEdit } from './video-caption-edit.model'
import { VideoPasswordService } from '../video/video-password.service'
import { VideoService } from '../video/video.service'
import { VideoCaptionEdit } from './video-caption-edit.model'

@Injectable()
export class VideoCaptionService {
Expand Down Expand Up @@ -74,4 +74,13 @@ export class VideoCaptionService {
getCaptionContent ({ captionPath }: Pick<VideoCaption, 'captionPath'>) {
return this.authHttp.get(environment.originServerUrl + captionPath, { responseType: 'text' })
}

generateCaption (videoIds: (number | string)[]) {
return from(videoIds)
.pipe(
concatMap(videoId => this.authHttp.post(`${VideoService.BASE_VIDEO_URL}/${videoId}/captions/generate`, {})),
toArray(),
catchError(err => this.restExtractor.handleError(err))
)
}
}
4 changes: 4 additions & 0 deletions client/src/app/shared/shared-main/video/video.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ export class Video implements VideoServerModel {
this.isUpdatableBy(user)
}

canGenerateTranscription (user: AuthUser, transcriptionEnabled: boolean) {
return transcriptionEnabled && this.isLocal && user.hasRight(UserRight.UPDATE_ANY_VIDEO)
}

// ---------------------------------------------------------------------------

isOwner (user: AuthUser) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
DropdownButtonSize,
DropdownDirection
} from '../shared-main/buttons/action-dropdown.component'
import { VideoCaptionService } from '../shared-main/video-caption/video-caption.service'
import { RedundancyService } from '../shared-main/video/redundancy.service'
import { VideoDetails } from '../shared-main/video/video-details.model'
import { Video } from '../shared-main/video/video.model'
Expand All @@ -37,6 +38,7 @@ export type VideoActionsDisplayType = {
transcoding?: boolean
studio?: boolean
stats?: boolean
generateTranscription?: boolean
}

@Component({
Expand Down Expand Up @@ -115,6 +117,7 @@ export class VideoActionsDropdownComponent implements OnChanges {
private videoBlocklistService: VideoBlockService,
private screenService: ScreenService,
private videoService: VideoService,
private videoCaptionService: VideoCaptionService,
private redundancyService: RedundancyService,
private serverService: ServerService
) { }
Expand Down Expand Up @@ -206,6 +209,10 @@ export class VideoActionsDropdownComponent implements OnChanges {
return this.video.isLiveInfoAvailableBy(this.user)
}

canGenerateTranscription () {
return this.video.canGenerateTranscription(this.user, this.serverService.getHTMLConfig().videoTranscription.enabled)
}

isVideoDownloadableByAnonymous () {
return (
this.video &&
Expand Down Expand Up @@ -338,14 +345,25 @@ export class VideoActionsDropdownComponent implements OnChanges {
this.videoService.runTranscoding({ videos: [ video ], type, askForForceTranscodingIfNeeded: true })
.subscribe({
next: () => {
this.notifier.success($localize`Transcoding jobs created for "${video.name}".`)
this.notifier.success($localize`Transcoding job created for "${video.name}".`)
this.transcodingCreated.emit()
},

error: err => this.notifier.error(err.message)
})
}

generateCaption (video: Video) {
this.videoCaptionService.generateCaption([ video.id ])
.subscribe({
next: () => {
this.notifier.success($localize`Transcription job created for "${video.name}".`)
},

error: err => this.notifier.error(err.message)
})
}

onVideoBlocked () {
this.videoBlocked.emit()
}
Expand Down Expand Up @@ -466,6 +484,14 @@ export class VideoActionsDropdownComponent implements OnChanges {
iconName: 'delete'
}
],
[
{
label: $localize`Generate caption`,
handler: ({ video }) => this.generateCaption(video),
isDisplayed: () => this.displayOptions.generateTranscription && this.canGenerateTranscription(),
iconName: 'video-lang'
}
],
[ // actions regarding the account/its server
{
label: $localize`Mute account`,
Expand Down
3 changes: 2 additions & 1 deletion packages/tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"devDependencies": {},
"scripts": {
"build": "tsc",
"watch": "tsc -w"
"watch": "tsc -w",
"install-dependencies:transcription": "pip install -r ./requirements.txt ../transcription-devtools/requirements.txt"
},
"dependencies": {}
}
2 changes: 2 additions & 0 deletions packages/tests/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
whisper-ctranslate2
openai-whisper
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('Open AI Whisper transcriber', function () {
engine: {
name: 'openai-whisper',
type: 'binary',
binary: 'whisper',
command: 'whisper',
supportedModelFormats: [ 'PyTorch' ],
languageDetection: true,
version: ''
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('Whisper CTranslate2 transcriber', function () {
engine: {
name: 'whisper-ctranslate2',
type: 'binary',
binary: 'whisper-ctranslate2',
command: 'whisper-ctranslate2',
supportedModelFormats: [ 'CTranslate2' ],
languageDetection: true,
version: '0.4.4'
Expand Down Expand Up @@ -152,7 +152,7 @@ describe('Whisper CTranslate2 transcriber', function () {
engine: {
name: 'openai-whisper',
type: 'binary',
binary: 'whisper',
command: 'whisper',
supportedModelFormats: [ 'PyTorch' ],
version: '0.4.4'
},
Expand Down
1 change: 1 addition & 0 deletions packages/transcription-devtools/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
jiwer
2 changes: 1 addition & 1 deletion packages/transcription-devtools/src/benchmark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ void (async () => {

benchmarkResults = benchmarkReducer(benchmarkResults, {
uuid,
engine: transcriber.getEngine(),
engine: transcriber.engine,
WER: await evaluator.wer(),
CER: await evaluator.cer(),
model: model.name
Expand Down
2 changes: 1 addition & 1 deletion packages/transcription/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import { OpenaiTranscriber } from '@peertube/peertube-transcription'
// Create a transcriber powered by OpenAI Whisper CLI
const transcriber = new OpenaiTranscriber({
name: 'openai-whisper',
binary: 'whisper',
command: 'whisper',
languageDetection: true,
binDirectory
});
Expand Down
20 changes: 5 additions & 15 deletions packages/transcription/src/abstract-transcriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export interface TranscribeArgs {
}

export abstract class AbstractTranscriber {
protected engine: TranscriptionEngine
engine: TranscriptionEngine

protected binDirectory: string
protected enginePath: string

Expand Down Expand Up @@ -67,25 +68,14 @@ export abstract class AbstractTranscriber {
return model.format === 'PyTorch'
}

getEngine () {
return this.engine
}

protected getEngineBinary () {
if (this.enginePath) return this.enginePath
if (this.binDirectory) return join(this.binDirectory, this.engine.binary)
if (this.binDirectory) return join(this.binDirectory, this.engine.command)

return this.engine.binary
return this.engine.command
}

abstract transcribe ({
mediaFilePath,
model,
language,
format = 'vtt',
transcriptDirectory,
runId = buildSUUID()
}: TranscribeArgs): Promise<TranscriptFile>
abstract transcribe (options: TranscribeArgs): Promise<TranscriptFile>

abstract install (path: string): Promise<void>
}
2 changes: 1 addition & 1 deletion packages/transcription/src/transcription-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export interface TranscriptionEngine {
description?: string
language?: string
type: 'binary'
binary: string
command: string
version: string
license?: string
forgeURL?: string
Expand Down
4 changes: 2 additions & 2 deletions packages/transcription/src/whisper/engines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const engines: TranscriptionEngine[] = [
description: 'High-performance inference of OpenAI\'s Whisper automatic speech recognition model',
language: 'python',
type: 'binary',
binary: 'whisper',
command: 'whisper',
forgeURL: 'https://github.com/openai/whisper',
license: 'MIT',
supportedModelFormats: [ 'PyTorch' ],
Expand All @@ -18,7 +18,7 @@ export const engines: TranscriptionEngine[] = [
description: 'Whisper command line client compatible with original OpenAI client based on CTranslate2.',
language: 'python',
type: 'binary',
binary: 'whisper-ctranslate2',
command: 'whisper-ctranslate2',
forgeURL: 'https://github.com/Softcatala/whisper-ctranslate2',
license: 'MIT',
supportedModelFormats: [ 'CTranslate2' ],
Expand Down
4 changes: 2 additions & 2 deletions scripts/ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ elif [ "$1" = "api-5" ]; then

MOCHA_PARALLEL=true runJSTest "$1" $((2*$speedFactor)) $transcodingFiles $runnersFiles
elif [ "$1" = "external-plugins" ]; then
pip install whisper-ctranslate2 openai-whisper jiwer
npm run install-dependencies:transcription --workspace=@peertube/tests

npm run build:server
npm run build:tests
Expand All @@ -149,7 +149,7 @@ elif [ "$1" = "lint" ]; then

( cd client && npm run lint )
elif [ "$1" = "transcription" ]; then
pip install whisper-ctranslate2 openai-whisper jiwer
npm run install-dependencies:transcription --workspace=@peertube/tests

npm run build:server
npm run build:tests
Expand Down
2 changes: 1 addition & 1 deletion server/core/lib/video-captions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export async function generateSubtitle (options: {
})

if (!CONFIG.VIDEO_TRANSCRIPTION.ENGINE_PATH) {
logger.info(`Installing transcriber ${transcriber.getEngine().name} to generate subtitles`, lTags())
logger.info(`Installing transcriber ${transcriber.engine.name} to generate subtitles`, lTags())
await transcriber.install(DIRECTORIES.LOCAL_PIP_DIRECTORY)
}
}
Expand Down
2 changes: 1 addition & 1 deletion support/doc/development/tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ jiwer --help
Otherwise, install the packages. On Debian-based systems (like Debian, Ubuntu or Mint):
```bash
sudo apt-get install parallel libimage-exiftool-perl
sudo pip install whisper-ctranslate2 openai-whisper
sudo pip install -r packages/tests/requirements.txt -r packages/transcription-devtools/requirements.txt
```

### Test
Expand Down

0 comments on commit a252262

Please sign in to comment.