Skip to content

Commit

Permalink
feat: Add /v1/audio/transcriptions and /v1/audio/translations wit…
Browse files Browse the repository at this point in the history
…h whisper.cpp (#276)
  • Loading branch information
hiro-v authored Jan 28, 2024
1 parent 7e245c5 commit 1d3798d
Show file tree
Hide file tree
Showing 18 changed files with 8,135 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ range=$((max - min + 1))
PORT=$((RANDOM % range + min))

# Start the binary file
"$BINARY_PATH" 1 127.0.0.1 $PORT > /tmp/nitro.log 2>&1 &
"$BINARY_PATH" 1 127.0.0.1 $PORT >/tmp/nitro.log 2>&1 &

# Get the process id of the binary file
pid=$!

if ! ps -p $pid > /dev/null; then
if ! ps -p $pid >/dev/null; then
echo "nitro failed to start. Logs:"
cat /tmp/nitro.log
exit 1
Expand All @@ -35,26 +35,27 @@ fi
# Wait for a few seconds to let the server start
sleep 5

# Check if /tmp/testmodel exists, if not, download it
if [[ ! -f "/tmp/testmodel" ]]; then
wget $DOWNLOAD_URL -O /tmp/testmodel
# Check if /tmp/testllm exists, if not, download it
if [[ ! -f "/tmp/testllm" ]]; then
wget $DOWNLOAD_URL -O /tmp/testllm
fi

# Run the curl commands
response1=$(curl -o /tmp/response1.log -s -w "%{http_code}" --location "http://127.0.0.1:$PORT/inferences/llamacpp/loadModel" \
--header 'Content-Type: application/json' \
--data '{
"llama_model_path": "/tmp/testmodel",
--header 'Content-Type: application/json' \
--data '{
"llama_model_path": "/tmp/testllm",
"ctx_len": 50,
"ngl": 32,
"embedding": false
}' 2>&1)

response2=$(curl -o /tmp/response2.log -s -w "%{http_code}" --location "http://127.0.0.1:$PORT/inferences/llamacpp/chat_completion" \
--header 'Content-Type: application/json' \
--header 'Accept: text/event-stream' \
--header 'Access-Control-Allow-Origin: *' \
--data '{
response2=$(
curl -o /tmp/response2.log -s -w "%{http_code}" --location "http://127.0.0.1:$PORT/v1/chat/completions" \
--header 'Content-Type: application/json' \
--header 'Accept: text/event-stream' \
--header 'Access-Control-Allow-Origin: *' \
--data '{
"messages": [
{"content": "Hello there", "role": "assistant"},
{"content": "Write a long and sad story for me", "role": "user"}
Expand Down Expand Up @@ -98,7 +99,6 @@ echo "----------------------"
echo "Log run test:"
cat /tmp/response2.log


echo "Nitro test run successfully!"

# Kill the server process
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@echo off

set "TEMP=C:\Users\%UserName%\AppData\Local\Temp"
set "MODEL_PATH=%TEMP%\testmodel"
set "MODEL_PATH=%TEMP%\testllm"

rem Check for required arguments
if "%~2"=="" (
Expand Down
93 changes: 93 additions & 0 deletions .github/scripts/e2e-test-whisper-linux-and-mac.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#!/bin/bash

## Example run command
# ./linux-and-mac.sh './jan/plugins/@janhq/inference-plugin/dist/nitro/nitro_mac_arm64' https://huggingface.co/TheBloke/TinyLlama-1.1B-Chat-v0.3-GGUF/resolve/main/tinyllama-1.1b-chat-v0.3.Q2_K.gguf

# Check for required arguments
if [[ $# -ne 2 ]]; then
echo "Usage: $0 <path_to_binary> <url_to_download>"
exit 1
fi

rm /tmp/response1.log /tmp/response2.log /tmp/nitro.log

BINARY_PATH=$1
DOWNLOAD_URL=$2

# Random port to ensure it's not used
min=10000
max=11000
range=$((max - min + 1))
PORT=$((RANDOM % range + min))

# Start the binary file
"$BINARY_PATH" 1 127.0.0.1 $PORT >/tmp/nitro.log 2>&1 &

# Get the process id of the binary file
pid=$!

if ! ps -p $pid >/dev/null; then
echo "nitro failed to start. Logs:"
cat /tmp/nitro.log
exit 1
fi

# Wait for a few seconds to let the server start
sleep 5

# Check if /tmp/testwhisper exists, if not, download it
if [[ ! -f "/tmp/testwhisper" ]]; then
wget $DOWNLOAD_URL -O /tmp/testwhisper
fi

# Run the curl commands
response1=$(curl -o /tmp/response1.log -s -w "%{http_code}" --location "http://127.0.0.1:$PORT/v1/audio/load_model" \
--header 'Content-Type: application/json' \
--data '{
"model_path": "/tmp/testwhisper",
"model_id": "whisper.cpp"
}' 2>&1)

response2=$(
curl -o /tmp/response2.log -s -w "%{http_code}" --location "http://127.0.0.1:$PORT/v1/audio/transcriptions" \
--header 'Access-Control-Allow-Origin: *' \
--form 'file=@"../whisper.cpp/samples/jfk.wav"' \
--form 'model_id="whisper.cpp"' \
--form 'temperature="0.0"' \
--form 'prompt="The transcript is about OpenAI which makes technology like DALL·E, GPT-3, and ChatGPT with the hope of one day building an AGI system that benefits all of humanity. The president is trying to raly people to support the cause."' \
2>&1
)

error_occurred=0
if [[ "$response1" -ne 200 ]]; then
echo "The first curl command failed with status code: $response1"
cat /tmp/response1.log
error_occurred=1
fi

if [[ "$response2" -ne 200 ]]; then
echo "The second curl command failed with status code: $response2"
cat /tmp/response2.log
error_occurred=1
fi

if [[ "$error_occurred" -eq 1 ]]; then
echo "Nitro test run failed!!!!!!!!!!!!!!!!!!!!!!"
echo "Nitro Error Logs:"
cat /tmp/nitro.log
kill $pid
exit 1
fi

echo "----------------------"
echo "Log load model:"
cat /tmp/response1.log

echo "----------------------"
echo "Log run test:"
cat /tmp/response2.log

echo "Nitro test run successfully!"

# Kill the server process
kill $pid
109 changes: 109 additions & 0 deletions .github/scripts/e2e-test-whisper-windows.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
@echo off

set "TEMP=C:\Users\%UserName%\AppData\Local\Temp"
set "MODEL_PATH=%TEMP%\testwhisper"

rem Check for required arguments
if "%~2"=="" (
echo Usage: %~0 ^<path_to_binary^> ^<url_to_download^>
exit /b 1
)

set "BINARY_PATH=%~1"
set "DOWNLOAD_URL=%~2"

for %%i in ("%BINARY_PATH%") do set "BINARY_NAME=%%~nxi"

echo BINARY_NAME=%BINARY_NAME%

del %TEMP%\response1.log 2>nul
del %TEMP%\response2.log 2>nul
del %TEMP%\nitro.log 2>nul

set /a min=9999
set /a max=11000
set /a range=max-min+1
set /a PORT=%min% + %RANDOM% %% %range%

rem Start the binary file
start /B "" "%BINARY_PATH%" 1 "127.0.0.1" %PORT% > %TEMP%\nitro.log 2>&1

ping -n 6 127.0.0.1 %PORT% > nul

rem Capture the PID of the started process with "nitro" in its name
for /f "tokens=2" %%a in ('tasklist /fi "imagename eq %BINARY_NAME%" /fo list ^| findstr /B "PID:"') do (
set "pid=%%a"
)

echo pid=%pid%

if not defined pid (
echo nitro failed to start. Logs:
type %TEMP%\nitro.log
exit /b 1
)

rem Wait for a few seconds to let the server start

rem Check if %TEMP%\testmodel exists, if not, download it
if not exist "%MODEL_PATH%" (
bitsadmin.exe /transfer "DownloadTestModel" %DOWNLOAD_URL% "%MODEL_PATH%"
)

rem Define JSON strings for curl data
call set "MODEL_PATH_STRING=%%MODEL_PATH:\=\\%%"
set "curl_data1={\"model_path\":\"%MODEL_PATH_STRING%\",\"model_id\":\"whisper.cpp\"}"

rem Print the values of curl_data1 for debugging
echo curl_data1=%curl_data1%

rem Run the curl commands and capture the status code
curl.exe -o %TEMP%\response1.log -s -w "%%{http_code}" --location "http://127.0.0.1:%PORT%/v1/audio/load_model" --header "Content-Type: application/json" --data "%curl_data1%" > %TEMP%\response1_code.log 2>&1

curl.exe -o %TEMP%\response2.log -s -w "%%{http_code}" --location "http://127.0.0.1:%PORT%/v1/audio/transcriptions" ^
--header "Access-Control-Allow-Origin: *" ^
--form 'model_id="whisper.cpp"' ^
--form 'file=@"..\whisper.cpp\samples\jfk.wav"' ^
--form 'temperature="0.0"' ^
--form 'prompt="The transcript is about OpenAI which makes technology like DALL·E, GPT-3, and ChatGPT with the hope of one day building an AGI system that benefits all of humanity. The president is trying to raly people to support the cause."' ^
> %TEMP%\response2_code.log 2>&1

set "error_occurred=0"

rem Read the status codes from the log files
for /f %%a in (%TEMP%\response1_code.log) do set "response1=%%a"
for /f %%a in (%TEMP%\response2_code.log) do set "response2=%%a"

if "%response1%" neq "200" (
echo The first curl command failed with status code: %response1%
type %TEMP%\response1.log
set "error_occurred=1"
)

if "%response2%" neq "200" (
echo The second curl command failed with status code: %response2%
type %TEMP%\response2.log
set "error_occurred=1"
)

if "%error_occurred%"=="1" (
echo Nitro test run failed!!!!!!!!!!!!!!!!!!!!!!
echo Nitro Error Logs:
type %TEMP%\nitro.log
taskkill /f /pid %pid%
exit /b 1
)


echo ----------------------
echo Log load model:
type %TEMP%\response1.log

echo ----------------------
echo "Log run test:"
type %TEMP%\response2.log

echo Nitro test run successfully!

rem Kill the server process
taskkill /f /pid %pid%
Loading

0 comments on commit 1d3798d

Please sign in to comment.