mirror of
https://github.com/xzeldon/whisper-api-server.git
synced 2024-12-27 09:55:45 +00:00
xzeldon
a025285256
Additionally, made the following changes: ∙ - Allow listening only on local interfaces ∙ - Update project structure
46 lines
956 B
Go
46 lines
956 B
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
)
|
|
|
|
type TranscribeResponse struct {
|
|
Text string `json:"text"`
|
|
}
|
|
|
|
func Transcribe(c echo.Context, whisperState *WhisperState) error {
|
|
audioPath, err := saveFormFile("file", c)
|
|
if err != nil {
|
|
c.Logger().Errorf("Error reading file: %s", err)
|
|
return err
|
|
}
|
|
|
|
whisperState.mutex.Lock()
|
|
buffer, err := whisperState.media.LoadAudioFile(audioPath, true)
|
|
if err != nil {
|
|
c.Logger().Errorf("Error loading audio file data: %s", err)
|
|
}
|
|
|
|
err = whisperState.context.RunFull(whisperState.params, buffer)
|
|
|
|
result, err := getResult(whisperState.context)
|
|
if err != nil {
|
|
c.Logger().Error(err)
|
|
}
|
|
|
|
defer whisperState.mutex.Unlock()
|
|
|
|
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)
|
|
}
|