Initial commit
This commit is contained in:
@@ -0,0 +1,163 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/hmac"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
|
||||
"speech-learning-api/internal/domain"
|
||||
"speech-learning-api/internal/store"
|
||||
)
|
||||
|
||||
var ErrInvalidCredentials = errors.New("invalid credentials")
|
||||
|
||||
type UserStore interface {
|
||||
CreateUser(ctx context.Context, email, passwordHash, uiLanguage string) (domain.User, error)
|
||||
GetPasswordHashByEmail(ctx context.Context, email string) (domain.User, string, error)
|
||||
GetUser(ctx context.Context, id string) (domain.User, error)
|
||||
GetPrimaryProfile(ctx context.Context, userID string) (domain.LearnerProfile, error)
|
||||
CreateLearnerProfile(ctx context.Context, userID, targetLanguage, cefrLevel string, dailyGoalMinutes int) (domain.LearnerProfile, error)
|
||||
}
|
||||
|
||||
type AuthService struct {
|
||||
store UserStore
|
||||
jwtSecret []byte
|
||||
}
|
||||
|
||||
type AuthResult struct {
|
||||
Token string `json:"token"`
|
||||
User domain.User `json:"user"`
|
||||
Profile domain.LearnerProfile `json:"profile"`
|
||||
}
|
||||
|
||||
func NewAuthService(store UserStore, jwtSecret string) *AuthService {
|
||||
return &AuthService{store: store, jwtSecret: []byte(jwtSecret)}
|
||||
}
|
||||
|
||||
func (s *AuthService) Register(ctx context.Context, email, password, uiLanguage, targetLanguage string, dailyGoalMinutes int) (AuthResult, error) {
|
||||
email = strings.ToLower(strings.TrimSpace(email))
|
||||
if email == "" || !strings.Contains(email, "@") {
|
||||
return AuthResult{}, errors.New("valid email is required")
|
||||
}
|
||||
if len(password) < 8 {
|
||||
return AuthResult{}, errors.New("password must be at least 8 characters")
|
||||
}
|
||||
if uiLanguage == "" {
|
||||
uiLanguage = "de-DE"
|
||||
}
|
||||
if targetLanguage == "" {
|
||||
targetLanguage = "en-US"
|
||||
}
|
||||
if dailyGoalMinutes <= 0 {
|
||||
dailyGoalMinutes = 10
|
||||
}
|
||||
|
||||
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return AuthResult{}, err
|
||||
}
|
||||
user, err := s.store.CreateUser(ctx, email, string(hash), uiLanguage)
|
||||
if err != nil {
|
||||
return AuthResult{}, err
|
||||
}
|
||||
profile, err := s.store.CreateLearnerProfile(ctx, user.ID, targetLanguage, "A1", dailyGoalMinutes)
|
||||
if err != nil {
|
||||
return AuthResult{}, err
|
||||
}
|
||||
token, err := s.IssueToken(user.ID)
|
||||
if err != nil {
|
||||
return AuthResult{}, err
|
||||
}
|
||||
return AuthResult{Token: token, User: user, Profile: profile}, nil
|
||||
}
|
||||
|
||||
func (s *AuthService) Login(ctx context.Context, email, password string) (AuthResult, error) {
|
||||
user, hash, err := s.store.GetPasswordHashByEmail(ctx, email)
|
||||
if err != nil {
|
||||
if errors.Is(err, store.ErrNotFound) {
|
||||
return AuthResult{}, ErrInvalidCredentials
|
||||
}
|
||||
return AuthResult{}, err
|
||||
}
|
||||
if err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password)); err != nil {
|
||||
return AuthResult{}, ErrInvalidCredentials
|
||||
}
|
||||
profile, err := s.store.GetPrimaryProfile(ctx, user.ID)
|
||||
if errors.Is(err, store.ErrNotFound) {
|
||||
profile, err = s.store.CreateLearnerProfile(ctx, user.ID, "en-US", "A1", 10)
|
||||
}
|
||||
if err != nil {
|
||||
return AuthResult{}, err
|
||||
}
|
||||
token, err := s.IssueToken(user.ID)
|
||||
if err != nil {
|
||||
return AuthResult{}, err
|
||||
}
|
||||
return AuthResult{Token: token, User: user, Profile: profile}, nil
|
||||
}
|
||||
|
||||
func (s *AuthService) IssueToken(userID string) (string, error) {
|
||||
header := map[string]string{"alg": "HS256", "typ": "JWT"}
|
||||
payload := map[string]any{
|
||||
"sub": userID,
|
||||
"exp": time.Now().Add(24 * time.Hour).Unix(),
|
||||
"iat": time.Now().Unix(),
|
||||
}
|
||||
headerJSON, err := json.Marshal(header)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
payloadJSON, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
enc := base64.RawURLEncoding
|
||||
unsigned := enc.EncodeToString(headerJSON) + "." + enc.EncodeToString(payloadJSON)
|
||||
sig := hmacSHA256(unsigned, s.jwtSecret)
|
||||
return unsigned + "." + enc.EncodeToString(sig), nil
|
||||
}
|
||||
|
||||
func (s *AuthService) VerifyToken(token string) (string, error) {
|
||||
parts := strings.Split(token, ".")
|
||||
if len(parts) != 3 {
|
||||
return "", errors.New("invalid token")
|
||||
}
|
||||
unsigned := parts[0] + "." + parts[1]
|
||||
expected := hmacSHA256(unsigned, s.jwtSecret)
|
||||
actual, err := base64.RawURLEncoding.DecodeString(parts[2])
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if !hmac.Equal(expected, actual) {
|
||||
return "", errors.New("invalid signature")
|
||||
}
|
||||
payloadJSON, err := base64.RawURLEncoding.DecodeString(parts[1])
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
var claims struct {
|
||||
Subject string `json:"sub"`
|
||||
Expiry int64 `json:"exp"`
|
||||
}
|
||||
if err := json.Unmarshal(payloadJSON, &claims); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if claims.Subject == "" || time.Now().Unix() > claims.Expiry {
|
||||
return "", fmt.Errorf("token expired or missing subject")
|
||||
}
|
||||
return claims.Subject, nil
|
||||
}
|
||||
|
||||
func hmacSHA256(value string, secret []byte) []byte {
|
||||
mac := hmac.New(sha256.New, secret)
|
||||
mac.Write([]byte(value))
|
||||
return mac.Sum(nil)
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package services
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestAuthServiceIssueAndVerifyToken(t *testing.T) {
|
||||
auth := NewAuthService(nil, "test-secret")
|
||||
|
||||
token, err := auth.IssueToken("usr_test")
|
||||
if err != nil {
|
||||
t.Fatalf("issue token: %v", err)
|
||||
}
|
||||
|
||||
userID, err := auth.VerifyToken(token)
|
||||
if err != nil {
|
||||
t.Fatalf("verify token: %v", err)
|
||||
}
|
||||
if userID != "usr_test" {
|
||||
t.Fatalf("expected usr_test, got %s", userID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthServiceRejectsTamperedToken(t *testing.T) {
|
||||
auth := NewAuthService(nil, "test-secret")
|
||||
token, err := auth.IssueToken("usr_test")
|
||||
if err != nil {
|
||||
t.Fatalf("issue token: %v", err)
|
||||
}
|
||||
|
||||
if _, err := auth.VerifyToken(token + "x"); err == nil {
|
||||
t.Fatal("expected tampered token to fail")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,187 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"speech-learning-api/internal/config"
|
||||
"speech-learning-api/internal/domain"
|
||||
)
|
||||
|
||||
type TeacherAgent interface {
|
||||
FeedbackForAttempt(ctx context.Context, targetText string, result domain.PronunciationResult, weakWords []domain.WordScore) (domain.TeacherResponse, error)
|
||||
StartConversation(ctx context.Context, targetLanguage, cefrLevel string) (domain.TeacherResponse, error)
|
||||
ContinueConversation(ctx context.Context, targetLanguage, cefrLevel, userText string) (domain.TeacherResponse, error)
|
||||
}
|
||||
|
||||
type FlowiseTeacher struct {
|
||||
cfg config.Config
|
||||
client *http.Client
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func NewFlowiseTeacher(cfg config.Config, logger *slog.Logger) *FlowiseTeacher {
|
||||
return &FlowiseTeacher{
|
||||
cfg: cfg,
|
||||
client: &http.Client{
|
||||
Timeout: 30 * time.Second,
|
||||
},
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
func (f *FlowiseTeacher) FeedbackForAttempt(ctx context.Context, targetText string, result domain.PronunciationResult, weakWords []domain.WordScore) (domain.TeacherResponse, error) {
|
||||
prompt := map[string]any{
|
||||
"task": "practice_feedback",
|
||||
"target_text": targetText,
|
||||
"recognized_text": result.RecognizedText,
|
||||
"scores": result,
|
||||
"weak_words": weakWords,
|
||||
"rules": "Answer in German. A1 learner. Max two corrections. Return valid JSON only.",
|
||||
}
|
||||
return f.ask(ctx, prompt, mockFeedback(targetText, result))
|
||||
}
|
||||
|
||||
func (f *FlowiseTeacher) StartConversation(ctx context.Context, targetLanguage, cefrLevel string) (domain.TeacherResponse, error) {
|
||||
prompt := map[string]any{
|
||||
"task": "start_conversation",
|
||||
"target_language": targetLanguage,
|
||||
"cefr_level": cefrLevel,
|
||||
"rules": "Create a simple A1 roleplay. Return valid JSON only.",
|
||||
}
|
||||
return f.ask(ctx, prompt, domain.TeacherResponse{
|
||||
ExerciseType: "conversation",
|
||||
TargetText: "Hello. What is your name?",
|
||||
TeacherFeedback: "Wir starten sehr einfach. Antworte mit: My name is ...",
|
||||
TTSText: "Hello. What is your name?",
|
||||
NextRecommendation: "Antworten: My name is ...",
|
||||
})
|
||||
}
|
||||
|
||||
func (f *FlowiseTeacher) ContinueConversation(ctx context.Context, targetLanguage, cefrLevel, userText string) (domain.TeacherResponse, error) {
|
||||
prompt := map[string]any{
|
||||
"task": "continue_conversation",
|
||||
"target_language": targetLanguage,
|
||||
"cefr_level": cefrLevel,
|
||||
"user_text": userText,
|
||||
"rules": "Continue a simple A1 conversation. Return valid JSON only.",
|
||||
}
|
||||
return f.ask(ctx, prompt, domain.TeacherResponse{
|
||||
ExerciseType: "conversation",
|
||||
TargetText: "Nice to meet you. Where are you from?",
|
||||
TeacherFeedback: "Gut. Jetzt uebe einen kurzen Satz mit Herkunft.",
|
||||
TTSText: "Nice to meet you. Where are you from?",
|
||||
NextRecommendation: "Antworten: I am from ...",
|
||||
})
|
||||
}
|
||||
|
||||
func (f *FlowiseTeacher) ask(ctx context.Context, payload any, fallback domain.TeacherResponse) (domain.TeacherResponse, error) {
|
||||
if f.cfg.FlowiseChatflow == "" || f.cfg.FlowiseBaseURL == "" {
|
||||
return fallback, nil
|
||||
}
|
||||
body, _ := json.Marshal(payload)
|
||||
question := string(body)
|
||||
reqBody, _ := json.Marshal(map[string]any{
|
||||
"question": question,
|
||||
})
|
||||
endpoint := strings.TrimRight(f.cfg.FlowiseBaseURL, "/") + "/api/v1/prediction/" + f.cfg.FlowiseChatflow
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, bytes.NewReader(reqBody))
|
||||
if err != nil {
|
||||
return fallback, err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
if f.cfg.FlowiseAPIKey != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+f.cfg.FlowiseAPIKey)
|
||||
}
|
||||
|
||||
resp, err := f.client.Do(req)
|
||||
if err != nil {
|
||||
f.logger.Warn("flowise unavailable, using fallback", "error", err)
|
||||
return fallback, nil
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
respBody, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return fallback, err
|
||||
}
|
||||
if resp.StatusCode >= 300 {
|
||||
f.logger.Warn("flowise returned error, using fallback", "status", resp.StatusCode, "body", string(respBody))
|
||||
return fallback, nil
|
||||
}
|
||||
|
||||
var envelope struct {
|
||||
Text string `json:"text"`
|
||||
JSON json.RawMessage `json:"json"`
|
||||
}
|
||||
if err := json.Unmarshal(respBody, &envelope); err == nil {
|
||||
if envelope.Text != "" {
|
||||
if parsed, ok := parseTeacherResponse(envelope.Text); ok {
|
||||
return parsed, nil
|
||||
}
|
||||
fallback.TeacherFeedback = envelope.Text
|
||||
return fallback, nil
|
||||
}
|
||||
if len(envelope.JSON) > 0 && string(envelope.JSON) != "null" {
|
||||
if parsed, ok := parseTeacherResponseJSON(envelope.JSON); ok {
|
||||
return parsed, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
if parsed, ok := parseTeacherResponse(string(respBody)); ok {
|
||||
return parsed, nil
|
||||
}
|
||||
f.logger.Warn("flowise response not parseable, using fallback", "body", string(respBody))
|
||||
return fallback, nil
|
||||
}
|
||||
|
||||
func parseTeacherResponse(value string) (domain.TeacherResponse, bool) {
|
||||
value = strings.TrimSpace(value)
|
||||
value = strings.TrimPrefix(value, "```json")
|
||||
value = strings.TrimPrefix(value, "```")
|
||||
value = strings.TrimSuffix(value, "```")
|
||||
var out domain.TeacherResponse
|
||||
if err := json.Unmarshal([]byte(strings.TrimSpace(value)), &out); err != nil {
|
||||
return domain.TeacherResponse{}, false
|
||||
}
|
||||
return out, out.TeacherFeedback != "" || out.TargetText != "" || out.TTSText != ""
|
||||
}
|
||||
|
||||
func parseTeacherResponseJSON(value json.RawMessage) (domain.TeacherResponse, bool) {
|
||||
if parsed, ok := parseTeacherResponse(string(value)); ok {
|
||||
return parsed, true
|
||||
}
|
||||
var text string
|
||||
if err := json.Unmarshal(value, &text); err != nil {
|
||||
return domain.TeacherResponse{}, false
|
||||
}
|
||||
return parseTeacherResponse(text)
|
||||
}
|
||||
|
||||
func mockFeedback(targetText string, result domain.PronunciationResult) domain.TeacherResponse {
|
||||
weak := "den roten Woertern"
|
||||
for _, word := range result.WordScores {
|
||||
if word.Highlight == "weak" {
|
||||
weak = fmt.Sprintf("'%s'", word.Word)
|
||||
break
|
||||
}
|
||||
}
|
||||
feedback := fmt.Sprintf("Guter Versuch. Uebe jetzt besonders %s. Sprich langsam und halte die Endlaute klar.", weak)
|
||||
if result.OverallScore >= 85 {
|
||||
feedback = "Sehr solide. Wiederhole den Satz einmal etwas natuerlicher und mit ruhigem Rhythmus."
|
||||
}
|
||||
return domain.TeacherResponse{
|
||||
ExerciseType: "repeat_sentence",
|
||||
TargetText: targetText,
|
||||
TeacherFeedback: feedback,
|
||||
TTSText: targetText,
|
||||
NextRecommendation: "Noch einmal sprechen und auf die markierten Woerter achten.",
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"speech-learning-api/internal/config"
|
||||
"speech-learning-api/internal/domain"
|
||||
)
|
||||
|
||||
func TestParseTeacherResponse(t *testing.T) {
|
||||
response, ok := parseTeacherResponse(`{
|
||||
"exercise_type": "repeat_sentence",
|
||||
"target_text": "Hello.",
|
||||
"teacher_feedback": "Gut.",
|
||||
"tts_text": "Hello.",
|
||||
"ssml": "",
|
||||
"next_recommendation": "Noch einmal."
|
||||
}`)
|
||||
|
||||
if !ok {
|
||||
t.Fatal("expected response to parse")
|
||||
}
|
||||
if response.TargetText != "Hello." {
|
||||
t.Fatalf("unexpected target text %q", response.TargetText)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseTeacherResponseJSONFromObject(t *testing.T) {
|
||||
response, ok := parseTeacherResponseJSON(json.RawMessage(`{
|
||||
"exercise_type": "repeat_sentence",
|
||||
"target_text": "Hello.",
|
||||
"teacher_feedback": "Gut.",
|
||||
"tts_text": "Hello.",
|
||||
"ssml": "",
|
||||
"next_recommendation": "Noch einmal."
|
||||
}`))
|
||||
|
||||
if !ok {
|
||||
t.Fatal("expected response to parse")
|
||||
}
|
||||
if response.TeacherFeedback != "Gut." {
|
||||
t.Fatalf("unexpected feedback %q", response.TeacherFeedback)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseTeacherResponseJSONFromString(t *testing.T) {
|
||||
response, ok := parseTeacherResponseJSON(json.RawMessage(`"{\"exercise_type\":\"repeat_sentence\",\"target_text\":\"Hello.\",\"teacher_feedback\":\"Gut.\",\"tts_text\":\"Hello.\",\"ssml\":\"\",\"next_recommendation\":\"Noch einmal.\"}"`))
|
||||
|
||||
if !ok {
|
||||
t.Fatal("expected response to parse")
|
||||
}
|
||||
if response.NextRecommendation != "Noch einmal." {
|
||||
t.Fatalf("unexpected recommendation %q", response.NextRecommendation)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFlowiseAskSendsQuestionOnly(t *testing.T) {
|
||||
var requestBody map[string]json.RawMessage
|
||||
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/api/v1/prediction/chatflow-id" {
|
||||
t.Fatalf("unexpected path %q", r.URL.Path)
|
||||
}
|
||||
if r.Header.Get("Authorization") != "Bearer api-key" {
|
||||
t.Fatalf("unexpected authorization header %q", r.Header.Get("Authorization"))
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&requestBody); err != nil {
|
||||
t.Fatalf("decode request body: %v", err)
|
||||
}
|
||||
|
||||
response := domain.TeacherResponse{
|
||||
ExerciseType: "conversation",
|
||||
TargetText: "Hello.",
|
||||
TeacherFeedback: "Gut.",
|
||||
TTSText: "Hello.",
|
||||
NextRecommendation: "Noch einmal.",
|
||||
}
|
||||
responseText, err := json.Marshal(response)
|
||||
if err != nil {
|
||||
t.Fatalf("marshal response: %v", err)
|
||||
}
|
||||
if err := json.NewEncoder(w).Encode(map[string]string{"text": string(responseText)}); err != nil {
|
||||
t.Fatalf("encode response: %v", err)
|
||||
}
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
teacher := NewFlowiseTeacher(config.Config{
|
||||
FlowiseBaseURL: server.URL,
|
||||
FlowiseChatflow: "chatflow-id",
|
||||
FlowiseAPIKey: "api-key",
|
||||
}, slog.New(slog.NewTextHandler(io.Discard, nil)))
|
||||
|
||||
response, err := teacher.ask(context.Background(), map[string]any{"task": "start_conversation"}, domain.TeacherResponse{
|
||||
TeacherFeedback: "fallback",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("ask: %v", err)
|
||||
}
|
||||
if response.TeacherFeedback != "Gut." {
|
||||
t.Fatalf("unexpected feedback %q", response.TeacherFeedback)
|
||||
}
|
||||
if _, ok := requestBody["question"]; !ok {
|
||||
t.Fatal("expected question in request body")
|
||||
}
|
||||
if _, ok := requestBody["form"]; ok {
|
||||
t.Fatal("did not expect form in request body")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,290 @@
|
||||
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)
|
||||
}
|
||||
|
||||
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 == "" {
|
||||
return mockPronunciation(input.TargetText), 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,
|
||||
"PhonemeAlphabet": "IPA",
|
||||
}
|
||||
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)
|
||||
}
|
||||
return parseAzurePronunciation(body)
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
if s.cfg.AzureMock || s.cfg.AzureSpeechKey == "" || s.cfg.AzureRegion == "" {
|
||||
return nil, "", nil
|
||||
}
|
||||
|
||||
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))
|
||||
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"`
|
||||
} `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")
|
||||
}
|
||||
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))
|
||||
for _, phoneme := range word.Phonemes {
|
||||
if phoneme.Phoneme != "" {
|
||||
phonemes = append(phonemes, phoneme.Phoneme)
|
||||
}
|
||||
}
|
||||
score := firstScore(word.AccuracyScore, word.PronunciationAssessment.AccuracyScore)
|
||||
result.WordScores = append(result.WordScores, domain.WordScore{
|
||||
Word: word.Word,
|
||||
Accuracy: score,
|
||||
ErrorType: firstNonEmpty(word.PronunciationAssessment.ErrorType, word.ErrorType),
|
||||
Phonemes: phonemes,
|
||||
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 >= 70:
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package services
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestMockPronunciationReturnsWordScores(t *testing.T) {
|
||||
result := mockPronunciation("Hello, my name is Anna.")
|
||||
|
||||
if result.Provider != "mock" {
|
||||
t.Fatalf("expected mock provider, got %s", result.Provider)
|
||||
}
|
||||
if len(result.WordScores) != 5 {
|
||||
t.Fatalf("expected 5 word scores, got %d", len(result.WordScores))
|
||||
}
|
||||
if result.OverallScore <= 0 {
|
||||
t.Fatalf("expected positive score, got %f", result.OverallScore)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseAzurePronunciationTopLevelScores(t *testing.T) {
|
||||
body := []byte(`{
|
||||
"RecognitionStatus": "Success",
|
||||
"DisplayText": "Can I have a coffee please?",
|
||||
"NBest": [{
|
||||
"Display": "Can I have a coffee please?",
|
||||
"AccuracyScore": 94.0,
|
||||
"FluencyScore": 99.0,
|
||||
"CompletenessScore": 100.0,
|
||||
"PronScore": 96.2,
|
||||
"Words": [{
|
||||
"Word": "can",
|
||||
"AccuracyScore": 94.0,
|
||||
"ErrorType": "None",
|
||||
"Phonemes": [{"Phoneme": "k"}, {"Phoneme": "ae"}, {"Phoneme": "n"}]
|
||||
}]
|
||||
}]
|
||||
}`)
|
||||
|
||||
result, err := parseAzurePronunciation(body)
|
||||
if err != nil {
|
||||
t.Fatalf("parse azure pronunciation: %v", err)
|
||||
}
|
||||
if result.Provider != "azure" {
|
||||
t.Fatalf("expected azure provider, got %s", result.Provider)
|
||||
}
|
||||
if result.OverallScore != 96.2 {
|
||||
t.Fatalf("expected overall score 96.2, got %f", result.OverallScore)
|
||||
}
|
||||
if result.AccuracyScore != 94 {
|
||||
t.Fatalf("expected accuracy score 94, got %f", result.AccuracyScore)
|
||||
}
|
||||
if len(result.WordScores) != 1 {
|
||||
t.Fatalf("expected one word score, got %d", len(result.WordScores))
|
||||
}
|
||||
if result.WordScores[0].Accuracy != 94 {
|
||||
t.Fatalf("expected word accuracy 94, got %f", result.WordScores[0].Accuracy)
|
||||
}
|
||||
if result.WordScores[0].Highlight != "good" {
|
||||
t.Fatalf("expected good highlight, got %s", result.WordScores[0].Highlight)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user