Direkte DeepSeek- und Mistral-Anbindung mit versionierten Aktionsprofilen, Budgetreservierung, Tokenabrechnung und administrativen Qualitätsprüfungen ersetzen die Flowise-Abhängigkeit im regulären Betrieb. 840 geprüfte A1-Aufgaben mit geschützter Testauswertung und konkreten offenen Arbeitsaufträgen ergänzen den Kurs. Inhalts- und Sprachbewertung bleiben von Azure-Aussprachewerten getrennt; historische Aufgaben und Antworten bleiben erhalten. Echte Wort- und Phonemmessungen werden mit Herkunft, Aktualität und Nachweisdichte gebündelt. Go bestimmt Trainingsziele und Wiederholungen, das Modell formuliert dazu passende Nachsprechübungen. Profilansicht und Auswahlgründe werden angezeigt. Validierung: Go-Race-Tests mit lokaler PostgreSQL-Integration, go vet, Web-Build, Audit aller 840 Aufgaben und drei Browser-Smokes mit lokalen Modell-/Speech-Fixtures bestanden. Keine Produktionsänderungen oder Live-Aufrufe.
409 lines
13 KiB
Go
409 lines
13 KiB
Go
package services
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"log/slog"
|
|
"math"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
"time"
|
|
|
|
"speech-learning-api/internal/config"
|
|
"speech-learning-api/internal/domain"
|
|
)
|
|
|
|
type SpeechService interface {
|
|
AssessPronunciation(ctx context.Context, input PronunciationInput) (domain.PronunciationResult, error)
|
|
Synthesize(ctx context.Context, locale, voice, text string) ([]byte, string, error)
|
|
SynthesizeExercise(ctx context.Context, locale, voice, text, mode, focusWord string) ([]byte, string, error)
|
|
}
|
|
|
|
type PronunciationInput struct {
|
|
Audio []byte
|
|
ContentType string
|
|
TargetText string
|
|
Locale string
|
|
}
|
|
|
|
type AzureSpeechService struct {
|
|
cfg config.Config
|
|
client *http.Client
|
|
logger *slog.Logger
|
|
}
|
|
|
|
func NewAzureSpeechService(cfg config.Config, logger *slog.Logger) *AzureSpeechService {
|
|
return &AzureSpeechService{
|
|
cfg: cfg,
|
|
client: &http.Client{
|
|
Timeout: 45 * time.Second,
|
|
},
|
|
logger: logger,
|
|
}
|
|
}
|
|
|
|
func (s *AzureSpeechService) AssessPronunciation(ctx context.Context, input PronunciationInput) (domain.PronunciationResult, error) {
|
|
if input.TargetText == "" {
|
|
return domain.PronunciationResult{}, errors.New("target text is required")
|
|
}
|
|
if s.cfg.AzureMock || s.cfg.AzureSpeechKey == "" || s.cfg.AzureRegion == "" {
|
|
result := mockPronunciation(input.TargetText)
|
|
result.Locale = input.Locale
|
|
result.Method = "scripted"
|
|
result.MeasurementVersion = "demo-v1"
|
|
return result, nil
|
|
}
|
|
|
|
endpoint := fmt.Sprintf("https://%s.stt.speech.microsoft.com/speech/recognition/conversation/cognitiveservices/v1?language=%s",
|
|
s.cfg.AzureRegion, url.QueryEscape(valueOr(input.Locale, "en-US")))
|
|
|
|
assessmentConfig := map[string]any{
|
|
"ReferenceText": input.TargetText,
|
|
"GradingSystem": "HundredMark",
|
|
"Granularity": "Phoneme",
|
|
"Dimension": "Comprehensive",
|
|
"EnableMiscue": true,
|
|
}
|
|
alphabet := ""
|
|
switch valueOr(input.Locale, "en-US") {
|
|
case "en-US":
|
|
alphabet = "IPA"
|
|
case "zh-CN":
|
|
alphabet = "SAPI"
|
|
}
|
|
if alphabet != "" {
|
|
assessmentConfig["PhonemeAlphabet"] = alphabet
|
|
}
|
|
assessmentJSON, _ := json.Marshal(assessmentConfig)
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, bytes.NewReader(input.Audio))
|
|
if err != nil {
|
|
return domain.PronunciationResult{}, err
|
|
}
|
|
req.Header.Set("Ocp-Apim-Subscription-Key", s.cfg.AzureSpeechKey)
|
|
req.Header.Set("Accept", "application/json;text/xml")
|
|
req.Header.Set("Content-Type", contentTypeForAzure(input.ContentType))
|
|
req.Header.Set("Pronunciation-Assessment", base64.StdEncoding.EncodeToString(assessmentJSON))
|
|
|
|
resp, err := s.client.Do(req)
|
|
if err != nil {
|
|
return domain.PronunciationResult{}, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return domain.PronunciationResult{}, err
|
|
}
|
|
if resp.StatusCode >= 300 {
|
|
s.logger.Warn("azure pronunciation failed", "status", resp.StatusCode, "body", string(body))
|
|
return domain.PronunciationResult{}, fmt.Errorf("azure pronunciation status %d", resp.StatusCode)
|
|
}
|
|
result, err := parseAzurePronunciation(body)
|
|
result.Locale = valueOr(input.Locale, "en-US")
|
|
result.Alphabet = alphabet
|
|
result.Method = "scripted"
|
|
result.MeasurementVersion = "azure-rest-v1"
|
|
return result, err
|
|
}
|
|
|
|
func (s *AzureSpeechService) Synthesize(ctx context.Context, locale, voice, text string) ([]byte, string, error) {
|
|
if strings.TrimSpace(text) == "" {
|
|
return nil, "", errors.New("tts text is required")
|
|
}
|
|
locale = valueOr(locale, "en-US")
|
|
voice = valueOr(voice, "en-US-JennyNeural")
|
|
ssml := fmt.Sprintf(`<speak version="1.0" xml:lang="%s"><voice name="%s">%s</voice></speak>`, locale, voice, xmlEscape(text))
|
|
return s.synthesizeSSML(ctx, ssml)
|
|
}
|
|
|
|
func (s *AzureSpeechService) SynthesizeExercise(ctx context.Context, locale, voice, text, mode, focusWord string) ([]byte, string, error) {
|
|
if strings.TrimSpace(text) == "" {
|
|
return nil, "", errors.New("tts text is required")
|
|
}
|
|
if mode == "normal" {
|
|
return s.Synthesize(ctx, locale, voice, text)
|
|
}
|
|
locale = valueOr(locale, "en-US")
|
|
voice = valueOr(voice, "en-US-JennyNeural")
|
|
var content string
|
|
switch mode {
|
|
case "slow":
|
|
content = fmt.Sprintf(`<prosody rate="0.8">%s</prosody>`, xmlEscape(text))
|
|
case "focus":
|
|
if strings.TrimSpace(focusWord) == "" {
|
|
return nil, "", errors.New("focus word is required")
|
|
}
|
|
content = fmt.Sprintf(`<prosody rate="0.7">%s</prosody><break time="400ms"/><prosody rate="0.8">%s</prosody>`, xmlEscape(focusWord), xmlEscape(text))
|
|
default:
|
|
return nil, "", errors.New("unsupported audio mode")
|
|
}
|
|
ssml := fmt.Sprintf(`<speak version="1.0" xml:lang="%s"><voice name="%s">%s</voice></speak>`, locale, voice, content)
|
|
return s.synthesizeSSML(ctx, ssml)
|
|
}
|
|
|
|
func (s *AzureSpeechService) synthesizeSSML(ctx context.Context, ssml string) ([]byte, string, error) {
|
|
if s.cfg.AzureMock || s.cfg.AzureSpeechKey == "" || s.cfg.AzureRegion == "" {
|
|
return nil, "", nil
|
|
}
|
|
endpoint := fmt.Sprintf("https://%s.tts.speech.microsoft.com/cognitiveservices/v1", s.cfg.AzureRegion)
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, strings.NewReader(ssml))
|
|
if err != nil {
|
|
return nil, "", err
|
|
}
|
|
req.Header.Set("Ocp-Apim-Subscription-Key", s.cfg.AzureSpeechKey)
|
|
req.Header.Set("Content-Type", "application/ssml+xml")
|
|
req.Header.Set("X-Microsoft-OutputFormat", "audio-24khz-48kbitrate-mono-mp3")
|
|
req.Header.Set("User-Agent", "speech-learning-app")
|
|
|
|
resp, err := s.client.Do(req)
|
|
if err != nil {
|
|
return nil, "", err
|
|
}
|
|
defer resp.Body.Close()
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return nil, "", err
|
|
}
|
|
if resp.StatusCode >= 300 {
|
|
return nil, "", fmt.Errorf("azure tts status %d: %s", resp.StatusCode, string(body))
|
|
}
|
|
return body, "audio/mpeg", nil
|
|
}
|
|
|
|
type azurePronunciationResponse struct {
|
|
RecognitionStatus string `json:"RecognitionStatus"`
|
|
DisplayText string `json:"DisplayText"`
|
|
NBest []struct {
|
|
Display string `json:"Display"`
|
|
PronScore float64 `json:"PronScore"`
|
|
AccuracyScore float64 `json:"AccuracyScore"`
|
|
FluencyScore float64 `json:"FluencyScore"`
|
|
CompletenessScore float64 `json:"CompletenessScore"`
|
|
ProsodyScore float64 `json:"ProsodyScore"`
|
|
PronunciationAssessment struct {
|
|
PronScore float64 `json:"PronScore"`
|
|
AccuracyScore float64 `json:"AccuracyScore"`
|
|
FluencyScore float64 `json:"FluencyScore"`
|
|
CompletenessScore float64 `json:"CompletenessScore"`
|
|
ProsodyScore float64 `json:"ProsodyScore"`
|
|
} `json:"PronunciationAssessment"`
|
|
Words []struct {
|
|
Word string `json:"Word"`
|
|
ErrorType string `json:"ErrorType"`
|
|
AccuracyScore *float64 `json:"AccuracyScore"`
|
|
PronunciationAssessment struct {
|
|
AccuracyScore *float64 `json:"AccuracyScore"`
|
|
ErrorType string `json:"ErrorType"`
|
|
} `json:"PronunciationAssessment"`
|
|
Phonemes []struct {
|
|
Phoneme string `json:"Phoneme"`
|
|
AccuracyScore *float64 `json:"AccuracyScore"`
|
|
PronunciationAssessment struct {
|
|
AccuracyScore *float64 `json:"AccuracyScore"`
|
|
} `json:"PronunciationAssessment"`
|
|
} `json:"Phonemes"`
|
|
} `json:"Words"`
|
|
} `json:"NBest"`
|
|
}
|
|
|
|
func parseAzurePronunciation(body []byte) (domain.PronunciationResult, error) {
|
|
var raw azurePronunciationResponse
|
|
if err := json.Unmarshal(body, &raw); err != nil {
|
|
return domain.PronunciationResult{}, err
|
|
}
|
|
if len(raw.NBest) == 0 {
|
|
return domain.PronunciationResult{}, fmt.Errorf("azure response has no NBest result")
|
|
}
|
|
if raw.RecognitionStatus != "" && raw.RecognitionStatus != "Success" {
|
|
return domain.PronunciationResult{}, fmt.Errorf("Azure recognition did not succeed: %s", raw.RecognitionStatus)
|
|
}
|
|
best := raw.NBest[0]
|
|
result := domain.PronunciationResult{
|
|
RecognizedText: firstNonEmpty(best.Display, raw.DisplayText),
|
|
OverallScore: firstScore(best.PronScore, best.PronunciationAssessment.PronScore),
|
|
AccuracyScore: firstScore(best.AccuracyScore, best.PronunciationAssessment.AccuracyScore),
|
|
FluencyScore: firstScore(best.FluencyScore, best.PronunciationAssessment.FluencyScore),
|
|
CompletenessScore: firstScore(best.CompletenessScore, best.PronunciationAssessment.CompletenessScore),
|
|
ProsodyScore: firstScore(best.ProsodyScore, best.PronunciationAssessment.ProsodyScore),
|
|
Provider: "azure",
|
|
}
|
|
for _, word := range best.Words {
|
|
phonemes := make([]string, 0, len(word.Phonemes))
|
|
phonemeScores := make([]domain.PhonemeScore, 0, len(word.Phonemes))
|
|
for position, phoneme := range word.Phonemes {
|
|
if phoneme.Phoneme != "" {
|
|
phonemes = append(phonemes, phoneme.Phoneme)
|
|
accuracy := phoneme.PronunciationAssessment.AccuracyScore
|
|
if accuracy == nil {
|
|
accuracy = phoneme.AccuracyScore
|
|
}
|
|
if accuracy != nil {
|
|
phonemeScores = append(phonemeScores, domain.PhonemeScore{
|
|
Phoneme: phoneme.Phoneme, Accuracy: *accuracy, Highlight: highlightFor(*accuracy), Position: &position,
|
|
})
|
|
}
|
|
}
|
|
}
|
|
measured := word.PronunciationAssessment.AccuracyScore
|
|
if measured == nil {
|
|
measured = word.AccuracyScore
|
|
}
|
|
hasScore := measured != nil
|
|
score := 0.0
|
|
if measured != nil {
|
|
score = *measured
|
|
}
|
|
result.WordScores = append(result.WordScores, domain.WordScore{
|
|
Word: word.Word,
|
|
AccuracyMeasured: &hasScore,
|
|
Accuracy: score,
|
|
ErrorType: firstNonEmpty(word.PronunciationAssessment.ErrorType, word.ErrorType),
|
|
Phonemes: phonemes,
|
|
PhonemeScores: phonemeScores,
|
|
Highlight: highlightFor(score),
|
|
})
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func mockPronunciation(target string) domain.PronunciationResult {
|
|
words := strings.Fields(target)
|
|
wordScores := make([]domain.WordScore, 0, len(words))
|
|
total := 0.0
|
|
for i, word := range words {
|
|
clean := strings.Trim(word, ".,!?;:")
|
|
score := 92.0 - float64((len(clean)+i*7)%32)
|
|
if strings.Contains(strings.ToLower(clean), "th") {
|
|
score -= 10
|
|
}
|
|
score = math.Max(48, math.Min(97, score))
|
|
total += score
|
|
wordScores = append(wordScores, domain.WordScore{
|
|
Word: clean,
|
|
Accuracy: score,
|
|
Highlight: highlightFor(score),
|
|
})
|
|
}
|
|
avg := 82.0
|
|
if len(wordScores) > 0 {
|
|
avg = total / float64(len(wordScores))
|
|
}
|
|
return domain.PronunciationResult{
|
|
RecognizedText: target,
|
|
OverallScore: avg,
|
|
AccuracyScore: avg,
|
|
FluencyScore: math.Min(98, avg+4),
|
|
CompletenessScore: math.Min(100, avg+6),
|
|
ProsodyScore: math.Max(50, avg-3),
|
|
WordScores: wordScores,
|
|
Provider: "mock",
|
|
}
|
|
}
|
|
|
|
func highlightFor(score float64) string {
|
|
switch {
|
|
case score >= 85:
|
|
return "good"
|
|
case score >= 75:
|
|
return "ok"
|
|
default:
|
|
return "weak"
|
|
}
|
|
}
|
|
|
|
func contentTypeForAzure(contentType string) string {
|
|
if strings.Contains(contentType, "wav") {
|
|
return "audio/wav; codecs=audio/pcm; samplerate=16000"
|
|
}
|
|
if strings.TrimSpace(contentType) != "" {
|
|
return contentType
|
|
}
|
|
return "audio/wav; codecs=audio/pcm; samplerate=16000"
|
|
}
|
|
|
|
func xmlEscape(value string) string {
|
|
value = strings.ReplaceAll(value, "&", "&")
|
|
value = strings.ReplaceAll(value, "<", "<")
|
|
value = strings.ReplaceAll(value, ">", ">")
|
|
value = strings.ReplaceAll(value, `"`, """)
|
|
value = strings.ReplaceAll(value, "'", "'")
|
|
return value
|
|
}
|
|
|
|
func firstNonEmpty(values ...string) string {
|
|
for _, value := range values {
|
|
if strings.TrimSpace(value) != "" {
|
|
return value
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func firstScore(values ...float64) float64 {
|
|
for _, value := range values {
|
|
if value != 0 {
|
|
return value
|
|
}
|
|
}
|
|
return 0
|
|
}
|
|
|
|
func valueOr(value, fallback string) string {
|
|
if strings.TrimSpace(value) == "" {
|
|
return fallback
|
|
}
|
|
return value
|
|
}
|
|
|
|
// Transcribe is separate from reference-based assessment: free speech must not
|
|
// be forced to match an invented model answer. No mock transcript is fabricated.
|
|
func (s *AzureSpeechService) Transcribe(ctx context.Context, audio []byte, contentType, locale string) (string, error) {
|
|
if s.cfg.AzureMock || s.cfg.AzureSpeechKey == "" || s.cfg.AzureRegion == "" {
|
|
return "", errors.New("free speech transcription requires Azure Speech")
|
|
}
|
|
endpoint := fmt.Sprintf("https://%s.stt.speech.microsoft.com/speech/recognition/conversation/cognitiveservices/v1?language=%s&format=detailed", s.cfg.AzureRegion, url.QueryEscape(locale))
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, bytes.NewReader(audio))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
req.Header.Set("Ocp-Apim-Subscription-Key", s.cfg.AzureSpeechKey)
|
|
req.Header.Set("Content-Type", contentTypeForAzure(contentType))
|
|
req.Header.Set("Accept", "application/json")
|
|
resp, err := s.client.Do(req)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode != http.StatusOK {
|
|
return "", fmt.Errorf("Azure transcription HTTP %d", resp.StatusCode)
|
|
}
|
|
var result struct {
|
|
Status string `json:"RecognitionStatus"`
|
|
Text string `json:"DisplayText"`
|
|
NBest []struct {
|
|
Display string `json:"Display"`
|
|
} `json:"NBest"`
|
|
}
|
|
if err = json.NewDecoder(io.LimitReader(resp.Body, 1<<20)).Decode(&result); err != nil {
|
|
return "", err
|
|
}
|
|
if result.Status != "Success" {
|
|
return "", errors.New("no speech recognized; please record again")
|
|
}
|
|
if len(result.NBest) > 0 && result.NBest[0].Display != "" {
|
|
return result.NBest[0].Display, nil
|
|
}
|
|
if strings.TrimSpace(result.Text) == "" {
|
|
return "", errors.New("no speech recognized")
|
|
}
|
|
return result.Text, nil
|
|
}
|