Ariver
2026-05-17 be028ca034f5bfd84c9a18d63121b20e9965fff9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package model
 
import (
    "fmt"
    "io"
    "net/http"
    "os"
    "os/exec"
    "path/filepath"
    "strings"
 
    "voicesnap/internal/logger"
)
 
// ProgressCallback is called with download progress (percent, downloaded bytes, total bytes).
type ProgressCallback func(percent float64, downloaded, total int64)
 
// Download downloads and extracts the ASR model.
// It tries the primary URL first, then falls back to the fallback URL.
func Download(primaryURL, fallbackURL, modelsDir string, progress ProgressCallback) error {
    if err := os.MkdirAll(modelsDir, 0755); err != nil {
        return fmt.Errorf("failed to create models dir: %w", err)
    }
 
    tempFile := filepath.Join(modelsDir, "model_package.tar.bz2")
 
    // Try primary URL first
    logger.Info("Downloading model from primary URL: %s", primaryURL)
    err := downloadFile(primaryURL, tempFile, progress)
    if err != nil {
        logger.Info("Primary URL failed: %v, trying fallback", err)
        err = downloadFile(fallbackURL, tempFile, progress)
        if err != nil {
            return fmt.Errorf("download failed from both URLs: %w", err)
        }
    }
 
    // Extract
    logger.Info("Extracting model archive...")
    if err := extractModel(tempFile, modelsDir); err != nil {
        return fmt.Errorf("extraction failed: %w", err)
    }
 
    // Cleanup temp file
    os.Remove(tempFile)
 
    logger.Info("Model download and extraction complete")
    return nil
}
 
func downloadFile(url, destPath string, progress ProgressCallback) error {
    resp, err := http.Get(url)
    if err != nil {
        return err
    }
    defer resp.Body.Close()
 
    if resp.StatusCode != http.StatusOK {
        return fmt.Errorf("HTTP %d: %s", resp.StatusCode, resp.Status)
    }
 
    totalBytes := resp.ContentLength
    out, err := os.Create(destPath)
    if err != nil {
        return err
    }
    defer out.Close()
 
    buf := make([]byte, 32*1024)
    var downloaded int64
    lastPercent := -1.0
 
    for {
        n, readErr := resp.Body.Read(buf)
        if n > 0 {
            if _, writeErr := out.Write(buf[:n]); writeErr != nil {
                return writeErr
            }
            downloaded += int64(n)
 
            if totalBytes > 0 && progress != nil {
                pct := float64(downloaded) / float64(totalBytes) * 100
                if pct-lastPercent >= 0.5 {
                    lastPercent = pct
                    progress(pct, downloaded, totalBytes)
                }
            }
        }
        if readErr != nil {
            if readErr == io.EOF {
                break
            }
            return readErr
        }
    }
 
    return nil
}
 
func extractModel(archivePath, destDir string) error {
    // Use system tar to extract
    cmd := exec.Command("tar", "-xf", archivePath, "-C", destDir)
    output, err := cmd.CombinedOutput()
    if err != nil {
        return fmt.Errorf("tar extraction failed: %v, output: %s", err, string(output))
    }
 
    // Rename extracted directory to "sensevoice"
    entries, err := os.ReadDir(destDir)
    if err != nil {
        return err
    }
 
    for _, entry := range entries {
        if entry.IsDir() && strings.HasPrefix(entry.Name(), "sherpa-onnx-sense-voice") {
            targetDir := filepath.Join(destDir, "sensevoice")
            srcDir := filepath.Join(destDir, entry.Name())
 
            // Remove existing target
            os.RemoveAll(targetDir)
 
            if err := os.Rename(srcDir, targetDir); err != nil {
                return fmt.Errorf("failed to rename model dir: %w", err)
            }
            break
        }
    }
 
    return nil
}