mirror of
https://github.com/xzeldon/whisper-api-server.git
synced 2025-07-13 04:14:39 +03:00
Transcribe using file buffer straight from form
This commit is contained in:
@ -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)
|
||||
}
|
||||
|
Reference in New Issue
Block a user