Files
soft_usite/pkg/services/whisper_asr_service.go
T
Lizandro GuarnizoandClaude Sonnet 5 d493d6dee6 feat: agrega integraciones OCR y Whisper ASR (servicios propios)
Nuevos submódulos en Integraciones para conectar el servicio propio de
OCR (extracción de texto de imágenes) y el de transcripción de audio
self-hosted (whisper-asr-webservice, Basic Auth), con panel de
configuración y prueba en vivo, siguiendo el patrón ya usado por
WebSMS/Hostinger.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-13 10:09:40 -05:00

68 lines
1.8 KiB
Go

package services
import (
"bytes"
"encoding/json"
"fmt"
"io"
"mime/multipart"
"net/http"
"time"
"github.com/sujit-baniya/fiber-boilerplate/pkg/models"
)
var whisperAsrHTTPClient = &http.Client{Timeout: 120 * time.Second} // audio largo puede tardar
// TranscribirAudioSelfHosted manda un archivo de audio al servicio propio de
// transcripción (whisper-asr-webservice, Basic Auth) y devuelve el texto.
func TranscribirAudioSelfHosted(audioBytes []byte, filename string) (string, error) {
cfg, err := models.GetWhisperAsrConfig()
if err != nil {
return "", fmt.Errorf("el servicio de transcripción no está configurado (Integraciones → Whisper ASR)")
}
if filename == "" {
filename = "audio.wav"
}
var buf bytes.Buffer
w := multipart.NewWriter(&buf)
fw, err := w.CreateFormFile("audio_file", filename)
if err != nil {
return "", err
}
if _, err := fw.Write(audioBytes); err != nil {
return "", err
}
_ = w.WriteField("response_format", "json")
if err := w.Close(); err != nil {
return "", err
}
req, err := http.NewRequest(http.MethodPost, cfg.BaseURL, &buf)
if err != nil {
return "", err
}
req.Header.Set("Content-Type", w.FormDataContentType())
req.SetBasicAuth(cfg.Username, cfg.Password)
resp, err := whisperAsrHTTPClient.Do(req)
if err != nil {
return "", fmt.Errorf("no se pudo contactar el servicio de transcripción: %w", err)
}
defer resp.Body.Close()
raw, _ := io.ReadAll(io.LimitReader(resp.Body, 2*1024*1024))
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return "", fmt.Errorf("el servicio de transcripción respondió %d: %s", resp.StatusCode, string(raw))
}
var out struct {
Text string `json:"text"`
}
if err := json.Unmarshal(raw, &out); err != nil {
return "", fmt.Errorf("respuesta inesperada del servicio de transcripción: %s", string(raw))
}
return out.Text, nil
}