Skip to content

Commit

Permalink
Enhancement: arm64 (#42)
Browse files Browse the repository at this point in the history
Co-authored-by: Tighearnán Carroll <tighearnan.carroll@gamil.com>
  • Loading branch information
Tighearnán Carroll and Tighearnán Carroll committed May 18, 2023
1 parent dab0178 commit f31124b
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 29 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ jobs:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Extract metadata for Docker
id: meta
uses: docker/metadata-action@v4
Expand All @@ -42,5 +46,6 @@ jobs:
with:
context: .
push: true
platforms: linux/amd64,linux/arm64/v8
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
26 changes: 20 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
FROM golang:1.20 as build
FROM --platform=${BUILDPLATFORM} golang:1.20 as build

ARG TARGETARCH BUILDPLATFORM

WORKDIR /build

Expand All @@ -9,15 +11,27 @@ RUN go mod verify

COPY . .

RUN script/install-libtensorflow
RUN dpkg --add-architecture arm64 && \
apt update && \
apt install -y \
gcc-10-aarch64-linux-gnu \
libsqlite3-dev:arm64 && \
mkdir /tmp/extra-lib


RUN go build -a -o 'snips.sh'
RUN if [ "${TARGETARCH}" = "amd64" ]; then \
script/install-libtensorflow; \
cp /usr/local/lib/libtensorflow.so.2 /tmp/extra-lib/; \
cp /usr/local/lib/libtensorflow_framework.so.2 /tmp/extra-lib/; \
go build -a -o 'snips.sh'; \
else \
CC=aarch64-linux-gnu-gcc-10 GOARCH=${TARGETARCH} CGO_ENABLED=1 go build -ldflags "-linkmode external -extldflags -static" -a -o 'snips.sh'; \
fi

FROM ubuntu:20.04

FROM --platform=${BUILDPLATFORM} ubuntu:20.04
COPY --from=build /tmp/extra-lib/* /usr/local/lib/
COPY --from=build /build/snips.sh /usr/bin/snips.sh
COPY --from=build /usr/local/lib/libtensorflow.so.2 /usr/local/lib/
COPY --from=build /usr/local/lib/libtensorflow_framework.so.2 /usr/local/lib/

RUN ldconfig

Expand Down
6 changes: 6 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"net/url"
"os"
"runtime"
"text/tabwriter"
"time"

Expand All @@ -21,6 +22,9 @@ KEY TYPE DEFAULT DESCRIPTION
type Config struct {
Debug bool `default:"False" desc:"enable debug logging and pprof"`

// NOTE: always false on arm64 arch.
// currently not shipping libtensorflow for arm
// https://github.com/robherley/snips.sh/issues/39
EnableGuesser bool `default:"True" desc:"enable guesslang model to detect file types"`

HMACKey string `default:"hmac-and-cheese" desc:"symmetric key used to sign URLs"`
Expand Down Expand Up @@ -86,5 +90,7 @@ func Load() (*Config, error) {
return nil, err
}

cfg.EnableGuesser = cfg.EnableGuesser && runtime.GOARCH == "amd64"

return cfg, nil
}
24 changes: 1 addition & 23 deletions internal/renderer/detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,15 @@ package renderer
import (
"net/http"
"strings"
"time"

"github.com/armon/go-metrics"
"github.com/robherley/guesslang-go/pkg/guesser"
"github.com/robherley/snips.sh/internal/snips"
"github.com/rs/zerolog/log"
)

const (
// MinimumContentGuessLength is the minimum length of the content to use guesslang, smaller content will use the fallback lexer.
MinimumContentGuessLength = 64
)

var (
guesslang *guesser.Guesser
)

func init() {
var err error
guesslang, err = guesser.New()
if err != nil {
log.Fatal().Err(err).Msg("failed to initialize guesslang")
}
}

// DetectFileType returns the type of the file based on the content and the hint.
// If useGuesser is true, it will try to guess the type of the file using guesslang.
// If the content's mimetype is not detected as text/plain, it returns "binary"
Expand All @@ -43,13 +27,7 @@ func DetectFileType(content []byte, hint string, useGuesser bool) string {
case hint != "":
lexer = GetLexer(hint)
case useGuesser && len(content) >= MinimumContentGuessLength:
guessStart := time.Now()
answer, err := guesslang.Guess(string(content))
metrics.MeasureSince([]string{"guess", "duration"}, guessStart)
if err != nil {
log.Warn().Err(err).Msg("failed to guess the file type")
} else if answer.Reliable {
guess := strings.ToLower(answer.Predictions[0].Language)
if guess := Guess(string(content)); guess != "" {
lexer = GetLexer(guess)
}
default:
Expand Down
34 changes: 34 additions & 0 deletions internal/renderer/guess_amd64.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//go:build amd64

package renderer

import (
"strings"
"time"

"github.com/armon/go-metrics"
"github.com/robherley/guesslang-go/pkg/guesser"
"github.com/rs/zerolog/log"
)

var guesslang *guesser.Guesser

func init() {
var err error
guesslang, err = guesser.New()
if err != nil {
log.Fatal().Err(err).Msg("failed to initialize guesslang")
}
}

func Guess(content string) string {
guessStart := time.Now()
answer, err := guesslang.Guess(content)
metrics.MeasureSince([]string{"guess", "duration"}, guessStart)
if err != nil {
log.Warn().Err(err).Msg("failed to guess the file type")
return ""
}

return strings.ToLower(answer.Predictions[0].Language)
}
9 changes: 9 additions & 0 deletions internal/renderer/guess_arm64.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//go:build arm64

package renderer

func Guess(content string) string {
// currently not supporting guessing in arm64 bc of libtensorflow requirements
// https://github.com/robherley/snips.sh/issues/39
panic("not implemented")
}

0 comments on commit f31124b

Please sign in to comment.