#-*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4

PortSystem          1.0
PortGroup           github 1.0
PortGroup           makefile 1.0

name                whisper
categories          audio
github.setup        ggerganov whisper.cpp 1.6.2 v
github.tarball_from archive
license             MIT
maintainers         {ijams.me:nate @exprez135} \
                    openmaintainer
description         Port of OpenAI's Whisper model in C/C++
long_description    High-performance inference of OpenAI's Whisper automatic speech recognition \
                    (ASR) model
notes               "
Install this port's variants to include models, or download
your own and use the built-in `-m` feature.

Remember that whisper.cpp only functions on 16-bit WAV
files. You can use `ffmpeg -i input.mp3 -ar 16000
output.wav` to convert the file first.
"

checksums           whisper.cpp-${version}.tar.gz \
                    rmd160  26b0b29e46c990b13c9d10d95c11aa429fc8d56d \
                    sha256  da7988072022acc3cfa61b370b3c51baad017f1900c3dc4e68cb276499f66894 \
                    size    5088298

build.target        main
makefile.has_destdir    no
patchfiles          whisper-add-models.diff \
                    ggml-hard-coded.diff \
                    add-metal-path.diff

post-patch {
    reinplace "s|__PREFIX__|${prefix}|" \
        whisper.cpp \
        ggml-metal.metal \
        ggml-metal.m
}

post-build {
    move ${worksrcpath}/main ${worksrcpath}/whisper
}

destroot {
    xinstall -d ${destroot}${prefix}/bin
    xinstall -m 755 ${worksrcpath}/whisper ${destroot}${prefix}/bin/whisper
}

post-destroot {
    xinstall -d ${destroot}${prefix}/share/whisper/ggml
    xinstall ${worksrcpath}/ggml-metal.metal ${destroot}${prefix}/share/whisper/ggml
    xinstall ${worksrcpath}/ggml-common.h ${destroot}${prefix}/share/whisper/ggml
}


# Define model variants

variant tiny description {Install tiny model} {
    distfiles-append        ggml-tiny.bin:tiny
    master_sites-append     https://huggingface.co/ggerganov/whisper.cpp/resolve/main/
    checksums-append        ggml-tiny.bin \
                            sha256  be07e048e1e599ad46341c8d2a135645097a538221678b7acdd1b1919c6e1b21 \
                            size    77691713
    post-destroot {
        xinstall -d ${destroot}${prefix}/share/whisper/models
        xinstall ${distpath}/ggml-tiny.bin ${destroot}${prefix}/share/whisper/models/tiny.bin
    }
}

variant base description {Install base model} {
    distfiles-append        ggml-base.bin:base
    master_sites-append     https://huggingface.co/ggerganov/whisper.cpp/resolve/main/
    checksums-append        ggml-base.bin \
                            sha256  60ed5bc3dd14eea856493d334349b405782ddcaf0028d4b5df4088345fba2efe \
                            size    147951465
    post-destroot {
        xinstall -d ${destroot}${prefix}/share/whisper/models
        xinstall ${distpath}/ggml-base.bin ${destroot}${prefix}/share/whisper/models/base.bin
    }
}

variant small description {Install small model} {
    distfiles-append        ggml-small.bin:small
    master_sites-append     https://huggingface.co/ggerganov/whisper.cpp/resolve/main/
    checksums-append        ggml-small.bin \
                            sha256  1be3a9b2063867b937e64e2ec7483364a79917e157fa98c5d94b5c1fffea987b \
                            size    487601967
    post-destroot {
        xinstall -d ${destroot}${prefix}/share/whisper/models
        xinstall ${distpath}/ggml-small.bin ${destroot}${prefix}/share/whisper/models/small.bin
    }
}

variant medium description {Install medium model} {
    distfiles-append        ggml-medium.bin:medium
    master_sites-append     https://huggingface.co/ggerganov/whisper.cpp/resolve/main/
    checksums-append        ggml-medium.bin \
                            sha256  6c14d5adee5f86394037b4e4e8b59f1673b6cee10e3cf0b11bbdbee79c156208 \
                            size    1533763059
    post-destroot {
        xinstall -d ${destroot}${prefix}/share/whisper/models
        xinstall ${distpath}/ggml-medium.bin ${destroot}${prefix}/share/whisper/models/medium.bin
    }
}

variant large description {Install large model} {
    distfiles-append        ggml-large-v3.bin:large
    master_sites-append     https://huggingface.co/ggerganov/whisper.cpp/resolve/main/
    checksums-append        ggml-large.bin
                            sha256  64d182b440b98d5203c4f9bd541544d84c605196c4f7b845dfa11fb23594d1e2 \
                            size    3095033483
    post-destroot {
        xinstall -d ${destroot}${prefix}/share/whisper/models
        xinstall ${distpath}/ggml-large-v3.bin ${destroot}${prefix}/share/whisper/models/large.bin
    }
}