feat(audio): count whisper-1 quota by audio duration

This commit is contained in:
WqyJh
2024-01-17 18:16:15 +08:00
parent eed9f5fdf0
commit 89799d84c5
7 changed files with 119 additions and 59 deletions

19
common/audio/audio.go Normal file
View File

@@ -0,0 +1,19 @@
package audio
import (
"bytes"
"context"
"os/exec"
"strconv"
)
// GetAudioDuration returns the duration of an audio file in seconds.
func GetAudioDuration(ctx context.Context, filename string) (float64, error) {
// ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 {{input}}
c := exec.CommandContext(ctx, "ffprobe", "-v", "error", "-show_entries", "format=duration", "-of", "default=noprint_wrappers=1:nokey=1", filename)
output, err := c.Output()
if err != nil {
return 0, err
}
return strconv.ParseFloat(string(bytes.TrimSpace(output)), 64)
}

20
common/file.go Normal file
View File

@@ -0,0 +1,20 @@
package common
import (
"io"
"os"
)
// SaveTmpFile saves data to a temporary file. The filename would be apppended with a random string.
func SaveTmpFile(filename string, data io.Reader) (string, error) {
f, err := os.CreateTemp(os.TempDir(), filename)
if err != nil {
return "", err
}
defer f.Close()
_, err = io.Copy(f, data)
if err != nil {
return "", err
}
return f.Name(), nil
}

22
common/http.go Normal file
View File

@@ -0,0 +1,22 @@
package common
import (
"bytes"
"io"
"net/http"
)
func CloneRequest(old *http.Request) *http.Request {
req := old.Clone(old.Context())
oldBody, err := io.ReadAll(old.Body)
if err != nil {
return nil
}
err = old.Body.Close()
if err != nil {
return nil
}
old.Body = io.NopCloser(bytes.NewBuffer(oldBody))
req.Body = io.NopCloser(bytes.NewBuffer(oldBody))
return req
}

View File

@@ -61,7 +61,7 @@ var ModelRatio = map[string]float64{
"text-davinci-003": 10,
"text-davinci-edit-001": 10,
"code-davinci-edit-001": 10,
"whisper-1": 15, // $0.006 / minute -> $0.006 / 150 words -> $0.006 / 200 tokens -> $0.03 / 1k tokens
"whisper-1": 1, // $0.006 / minute -> $0.002 / 20 seconds -> $0.002 / 1K tokens
"tts-1": 7.5, // $0.015 / 1K characters
"tts-1-1106": 7.5,
"tts-1-hd": 15, // $0.030 / 1K characters
@@ -157,5 +157,8 @@ func GetCompletionRatio(name string) float64 {
if strings.HasPrefix(name, "claude-2") {
return 2.965517
}
if strings.HasPrefix(name, "whisper-1") {
return 0 // only count input audio duration
}
return 1
}