|
| 1 | +import SwiftUI |
| 2 | + |
| 3 | +struct DownloadButton: View { |
| 4 | + @ObservedObject private var llamaState: LlamaState |
| 5 | + private var modelName: String |
| 6 | + private var modelUrl: String |
| 7 | + private var filename: String |
| 8 | + |
| 9 | + @State private var status: String |
| 10 | + |
| 11 | + @State private var downloadTask: URLSessionDataTask? |
| 12 | + @State private var progress = 0.0 |
| 13 | + @State private var observation: NSKeyValueObservation? |
| 14 | + |
| 15 | + private static func getFileURL(filename: String) -> URL { |
| 16 | + FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent(filename) |
| 17 | + } |
| 18 | + |
| 19 | + private func checkFileExistenceAndUpdateStatus() { |
| 20 | + } |
| 21 | + |
| 22 | + init(llamaState: LlamaState, modelName: String, modelUrl: String, filename: String) { |
| 23 | + self.llamaState = llamaState |
| 24 | + self.modelName = modelName |
| 25 | + self.modelUrl = modelUrl |
| 26 | + self.filename = filename |
| 27 | + |
| 28 | + let fileURL = DownloadButton.getFileURL(filename: filename) |
| 29 | + status = FileManager.default.fileExists(atPath: fileURL.path) ? "downloaded" : "download" |
| 30 | + } |
| 31 | + |
| 32 | + private func download() { |
| 33 | + status = "downloading" |
| 34 | + downloadTask = URLSession.shared.dataTask(with: URL(string: modelUrl)!) { data, response, error in |
| 35 | + if let error = error { |
| 36 | + print("Error: \(error.localizedDescription)") |
| 37 | + return |
| 38 | + } |
| 39 | + |
| 40 | + guard let response = response as? HTTPURLResponse, (200...299).contains(response.statusCode) else { |
| 41 | + print("Server error!") |
| 42 | + return |
| 43 | + } |
| 44 | + |
| 45 | + if let data = data { |
| 46 | + do { |
| 47 | + let fileURL = DownloadButton.getFileURL(filename: filename) |
| 48 | + try data.write(to: fileURL) |
| 49 | + |
| 50 | + llamaState.cacheCleared = false |
| 51 | + |
| 52 | + print("Downloaded model \(modelName) to \(fileURL)") |
| 53 | + status = "downloaded" |
| 54 | + try llamaState.loadModel(modelUrl: fileURL) |
| 55 | + } catch let err { |
| 56 | + print("Error: \(err.localizedDescription)") |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + observation = downloadTask?.progress.observe(\.fractionCompleted) { progress, _ in |
| 61 | + self.progress = progress.fractionCompleted |
| 62 | + } |
| 63 | + downloadTask?.resume() |
| 64 | + } |
| 65 | + |
| 66 | + var body: some View { |
| 67 | + VStack { |
| 68 | + if status == "download" { |
| 69 | + Button(action: download) { |
| 70 | + Text("Download " + modelName) |
| 71 | + } |
| 72 | + } else if status == "downloading" { |
| 73 | + Button(action: { |
| 74 | + downloadTask?.cancel() |
| 75 | + status = "download" |
| 76 | + }) { |
| 77 | + Text("\(modelName) (Downloading \(Int(progress * 100))%)") |
| 78 | + } |
| 79 | + } else if status == "downloaded" { |
| 80 | + Button(action: { |
| 81 | + let fileURL = DownloadButton.getFileURL(filename: filename) |
| 82 | + if !FileManager.default.fileExists(atPath: fileURL.path) { |
| 83 | + download() |
| 84 | + return |
| 85 | + } |
| 86 | + do { |
| 87 | + try llamaState.loadModel(modelUrl: fileURL) |
| 88 | + } catch let err { |
| 89 | + print("Error: \(err.localizedDescription)") |
| 90 | + } |
| 91 | + }) { |
| 92 | + Text("\(modelName) (Downloaded)") |
| 93 | + } |
| 94 | + } else { |
| 95 | + Text("Unknown status") |
| 96 | + } |
| 97 | + } |
| 98 | + .onDisappear() { |
| 99 | + downloadTask?.cancel() |
| 100 | + } |
| 101 | + .onChange(of: llamaState.cacheCleared) { newValue in |
| 102 | + if newValue { |
| 103 | + downloadTask?.cancel() |
| 104 | + let fileURL = DownloadButton.getFileURL(filename: filename) |
| 105 | + status = FileManager.default.fileExists(atPath: fileURL.path) ? "downloaded" : "download" |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +#Preview { |
| 112 | + DownloadButton( |
| 113 | + llamaState: LlamaState(), |
| 114 | + modelName: "TheBloke / TinyLlama-1.1B-1T-OpenOrca-GGUF (Q4_0)", |
| 115 | + modelUrl: "https://huggingface.co/TheBloke/TinyLlama-1.1B-1T-OpenOrca-GGUF/resolve/main/tinyllama-1.1b-1t-openorca.Q4_0.gguf?download=true", |
| 116 | + filename: "tinyllama-1.1b-1t-openorca.Q4_0.gguf", |
| 117 | + ) |
| 118 | +} |
0 commit comments