#-*- 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           cmake 1.1

name                whisper
categories          audio
github.setup        ggerganov whisper.cpp 1.7.5 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  211e55a336132a878471382d8002425f069db683 \
                    sha256  2fda42b57b7b8427d724551bd041616d85401fb9382e42b0349132a28920a34f \
                    size    6216643

patchfiles          whisper-add-models.diff \
                    whisper-add-depends.diff \
                    ggml-hard-coded.diff \
                    add-metal-path.diff

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

destroot {
    xinstall -d ${destroot}${prefix}/bin
    xinstall -m 755 ${workpath}/build/bin/whisper-cli ${destroot}${prefix}/bin/whisper
}

post-destroot {
    xinstall -d ${destroot}${prefix}/share/whisper/ggml
    xinstall ${workpath}/build/bin/ggml-metal.metal ${destroot}${prefix}/share/whisper/ggml
    xinstall ${workpath}/build/bin/ggml-common.h ${destroot}${prefix}/share/whisper/ggml
    xinstall -d ${destroot}${prefix}/lib
    xinstall ${workpath}/build/src/libwhisper.${version}.dylib ${destroot}${prefix}/lib/libwhisper.1.dylib
    xinstall ${workpath}/build/ggml/src/libggml.dylib ${destroot}${prefix}/lib/libggml.dylib
    xinstall ${workpath}/build/ggml/src/libggml-cpu.dylib ${destroot}${prefix}/lib/libggml-cpu.dylib
    xinstall ${workpath}/build/ggml/src/libggml-base.dylib ${destroot}${prefix}/lib/libggml-base.dylib
    xinstall ${workpath}/build/ggml/src/ggml-blas/libggml-blas.dylib ${destroot}${prefix}/lib/libggml-blas.dylib
    xinstall ${workpath}/build/ggml/src/ggml-metal/libggml-metal.dylib ${destroot}${prefix}/lib/libggml-metal.dylib
}


# 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-v3.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
    }
}
