Transcribe using file buffer straight from form

This commit is contained in:
Lukasz
2024-03-11 20:55:47 +01:00
committed by xzeldon
parent a21b6b9f5a
commit 1f8912eb6d
4 changed files with 70 additions and 5 deletions

View File

@ -1,6 +1,7 @@
package api
import (
"io"
"net/http"
"strings"
@ -11,7 +12,7 @@ type TranscribeResponse struct {
Text string `json:"text"`
}
func Transcribe(c echo.Context, whisperState *WhisperState) error {
func TranscribeFromFile(c echo.Context, whisperState *WhisperState) error {
audioPath, err := saveFormFile("file", c)
if err != nil {
c.Logger().Errorf("Error reading file: %s", err)
@ -26,6 +27,11 @@ func Transcribe(c echo.Context, whisperState *WhisperState) error {
err = whisperState.context.RunFull(whisperState.params, buffer)
if err != nil {
c.Logger().Errorf("Error processing audio: %s", err)
return err
}
result, err := getResult(whisperState.context)
if err != nil {
c.Logger().Error(err)
@ -43,3 +49,59 @@ func Transcribe(c echo.Context, whisperState *WhisperState) error {
return c.JSON(http.StatusOK, response)
}
func Transcribe(c echo.Context, whisperState *WhisperState) error {
// Get the file header
fileHeader, err := c.FormFile("file")
if err != nil {
c.Logger().Errorf("Error retrieving the file: %s", err)
return err
}
// Open the file
file, err := fileHeader.Open()
if err != nil {
c.Logger().Errorf("Error opening the file: %s", err)
return err
}
defer file.Close()
// Read the file into a buffer
buffer, err := io.ReadAll(file)
if err != nil {
c.Logger().Errorf("Error reading the file into buffer: %s", err)
return err
}
whisperState.mutex.Lock()
defer whisperState.mutex.Unlock()
bufferSpecial, err := whisperState.media.LoadAudioFileData(&buffer, true)
if err != nil {
c.Logger().Errorf("Error loading audio file data: %s", err)
return err
}
err = whisperState.context.RunStreamed(whisperState.params, bufferSpecial)
if err != nil {
c.Logger().Errorf("Error processing audio: %s", err)
return err
}
result, err := getResult(whisperState.context)
if err != nil {
c.Logger().Error(err)
return err
}
if len(result) == 0 {
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "Internal server error"})
}
response := TranscribeResponse{
Text: strings.TrimLeft(result, " "),
}
return c.JSON(http.StatusOK, response)
}