Skip to content

Commit

Permalink
Add llamafile-convert command (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
chand1012 authored Jan 1, 2024
1 parent 1d9fa85 commit 1dcf274
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ install: llamafile/zipalign.1 \
$(INSTALL) o/$(MODE)/llamafile/zipalign $(PREFIX)/bin/zipalign
$(INSTALL) o/$(MODE)/llama.cpp/main/main $(PREFIX)/bin/llamafile
$(INSTALL) o/$(MODE)/llama.cpp/quantize/quantize $(PREFIX)/bin/llamafile-quantize
$(INSTALL) build/llamafile-convert $(PREFIX)/bin/llamafile-convert
$(INSTALL) o/$(MODE)/llama.cpp/perplexity/perplexity $(PREFIX)/bin/llamafile-perplexity
$(INSTALL) o/$(MODE)/llama.cpp/llava/llava-quantize $(PREFIX)/bin/llava-quantize
mkdir -p $(PREFIX)/share/man/man1
Expand Down
64 changes: 64 additions & 0 deletions build/llamafile-convert
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/bin/sh
FILE=$1
SCRIPTNAME=${0##*/}

if [ -z "$FILE" ]; then
echo "Usage: $SCRIPTNAME <gguf file or url> [cli|server|both]"
exit 1
fi

# if the file starts with http
if [ x"$FILE" != x"${FILE#http*}" ]; then
# download the file
# if the filename contains ?download=true, remove it
FILE=$(echo $FILE | sed 's/?download=true//g')
# get the filename
FILENAME=$(echo $FILE | sed 's/.*\///g')
echo "Downloading $FILENAME" >&2
if WGET=$(command -v wget 2>/dev/null); then
DOWNLOAD=$WGET
DOWNLOAD_ARGS=-O
elif CURL=$(command -v curl 2>/dev/null); then
DOWNLOAD=$CURL
DOWNLOAD_ARGS=-fLo
else
printf '%s\n' "$0: fatal error: you need to install either wget or curl" >&2
printf '%s\n' "please download https://cosmo.zip/pub/cosmos/bin/wget and put it on the system path" >&2
abort
fi
"${DOWNLOAD}" ${DOWNLOAD_ARGS} $FILENAME $FILE
# get the filename
FILE=$FILENAME
fi

# replace .gguf with .llamafile
LLAMAFILE_NAME=$(echo $FILE | sed 's/.gguf/.llamafile/g')
LLAMAFILE_PATH=$(command -v llamafile)
CLI_ARGS="-m
$FILE
...
"

convert() {
echo "Converting $FILE to $LLAMAFILE_NAME"
# print CLI args to .args
printf %s "$CLI_ARGS" > .args
cp $LLAMAFILE_PATH $LLAMAFILE_NAME
zipalign -j0 $LLAMAFILE_NAME $FILE .args
}

cleanup() {
echo "Cleaning up"
rm -f .args
# remove the downloaded file
rm -f $FILE
echo "Done"
}

abort() {
printf '%s\n' "conversion terminated." >&2
exit 1
}

convert || abort
cleanup

0 comments on commit 1dcf274

Please sign in to comment.