mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-11-12 03:13:41 +08:00
feat: batch update with laisky's one-api
This commit is contained in:
62
common/helper/audio.go
Normal file
62
common/helper/audio.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package helper
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// 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) {
|
||||
if data == nil {
|
||||
return "", errors.New("data is nil")
|
||||
}
|
||||
|
||||
f, err := os.CreateTemp("", "*-"+filename)
|
||||
if err != nil {
|
||||
return "", errors.Wrapf(err, "failed to create temporary file %s", filename)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
_, err = io.Copy(f, data)
|
||||
if err != nil {
|
||||
return "", errors.Wrapf(err, "failed to copy data to temporary file %s", filename)
|
||||
}
|
||||
|
||||
return f.Name(), nil
|
||||
}
|
||||
|
||||
// GetAudioTokens returns the number of tokens in an audio file.
|
||||
func GetAudioTokens(ctx context.Context, audio io.Reader, tokensPerSecond float64) (float64, error) {
|
||||
filename, err := SaveTmpFile("audio", audio)
|
||||
if err != nil {
|
||||
return 0, errors.Wrap(err, "failed to save audio to temporary file")
|
||||
}
|
||||
defer os.Remove(filename)
|
||||
|
||||
duration, err := GetAudioDuration(ctx, filename)
|
||||
if err != nil {
|
||||
return 0, errors.Wrap(err, "failed to get audio tokens")
|
||||
}
|
||||
|
||||
return duration * tokensPerSecond, nil
|
||||
}
|
||||
|
||||
// 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, "/usr/bin/ffprobe", "-v", "error", "-show_entries", "format=duration", "-of", "default=noprint_wrappers=1:nokey=1", filename)
|
||||
output, err := c.Output()
|
||||
if err != nil {
|
||||
return 0, errors.Wrap(err, "failed to get audio duration")
|
||||
}
|
||||
|
||||
// Actually gpt-4-audio calculates tokens with 0.1s precision,
|
||||
// while whisper calculates tokens with 1s precision
|
||||
return strconv.ParseFloat(string(bytes.TrimSpace(output)), 64)
|
||||
}
|
||||
68
common/helper/audio_test.go
Normal file
68
common/helper/audio_test.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package helper
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestGetAudioDuration(t *testing.T) {
|
||||
// skip if there is no ffmpeg installed
|
||||
_, err := exec.LookPath("ffmpeg")
|
||||
if err != nil {
|
||||
t.Skip("ffmpeg not installed, skipping test")
|
||||
}
|
||||
|
||||
t.Run("should return correct duration for a valid audio file", func(t *testing.T) {
|
||||
tmpFile, err := os.CreateTemp("", "test_audio*.mp3")
|
||||
require.NoError(t, err)
|
||||
defer os.Remove(tmpFile.Name())
|
||||
|
||||
// download test audio file
|
||||
resp, err := http.Get("https://s3.laisky.com/uploads/2025/01/audio-sample.m4a")
|
||||
require.NoError(t, err)
|
||||
defer resp.Body.Close()
|
||||
|
||||
_, err = io.Copy(tmpFile, resp.Body)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, tmpFile.Close())
|
||||
|
||||
duration, err := GetAudioDuration(context.Background(), tmpFile.Name())
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, duration, 3.904)
|
||||
})
|
||||
|
||||
t.Run("should return an error for a non-existent file", func(t *testing.T) {
|
||||
_, err := GetAudioDuration(context.Background(), "non_existent_file.mp3")
|
||||
require.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestGetAudioTokens(t *testing.T) {
|
||||
// skip if there is no ffmpeg installed
|
||||
_, err := exec.LookPath("ffmpeg")
|
||||
if err != nil {
|
||||
t.Skip("ffmpeg not installed, skipping test")
|
||||
}
|
||||
|
||||
t.Run("should return correct tokens for a valid audio file", func(t *testing.T) {
|
||||
// download test audio file
|
||||
resp, err := http.Get("https://s3.laisky.com/uploads/2025/01/audio-sample.m4a")
|
||||
require.NoError(t, err)
|
||||
defer resp.Body.Close()
|
||||
|
||||
tokens, err := GetAudioTokens(context.Background(), resp.Body, 50)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, tokens, 195.2)
|
||||
})
|
||||
|
||||
t.Run("should return an error for a non-existent file", func(t *testing.T) {
|
||||
_, err := GetAudioTokens(context.Background(), nil, 1)
|
||||
require.Error(t, err)
|
||||
})
|
||||
}
|
||||
@@ -6,13 +6,13 @@ import (
|
||||
"html/template"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"os/exec"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/songquanpeng/one-api/common/random"
|
||||
)
|
||||
|
||||
@@ -32,6 +32,14 @@ func OpenBrowser(url string) {
|
||||
}
|
||||
}
|
||||
|
||||
// RespondError sends a JSON response with a success status and an error message.
|
||||
func RespondError(c *gin.Context, err error) {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": false,
|
||||
"message": err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
func GetIp() (ip string) {
|
||||
ips, err := net.InterfaceAddrs()
|
||||
if err != nil {
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// GetTimestamp get current timestamp in seconds
|
||||
func GetTimestamp() int64 {
|
||||
return time.Now().Unix()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user