llama.swiftui : avoid data copy via "downloadTask"
This commit is contained in:
parent
9629448716
commit
4ed98b90bc
1 changed files with 13 additions and 10 deletions
|
@ -8,7 +8,7 @@ struct DownloadButton: View {
|
||||||
|
|
||||||
@State private var status: String
|
@State private var status: String
|
||||||
|
|
||||||
@State private var downloadTask: URLSessionDataTask?
|
@State private var downloadTask: URLSessionDownloadTask?
|
||||||
@State private var progress = 0.0
|
@State private var progress = 0.0
|
||||||
@State private var observation: NSKeyValueObservation?
|
@State private var observation: NSKeyValueObservation?
|
||||||
|
|
||||||
|
@ -32,7 +32,10 @@ struct DownloadButton: View {
|
||||||
private func download() {
|
private func download() {
|
||||||
status = "downloading"
|
status = "downloading"
|
||||||
print("Downloading model \(modelName) from \(modelUrl)")
|
print("Downloading model \(modelName) from \(modelUrl)")
|
||||||
downloadTask = URLSession.shared.dataTask(with: URL(string: modelUrl)!) { data, response, error in
|
guard let url = URL(string: modelUrl) else { return }
|
||||||
|
let fileURL = DownloadButton.getFileURL(filename: filename)
|
||||||
|
|
||||||
|
downloadTask = URLSession.shared.downloadTask(with: url) { temporaryURL, response, error in
|
||||||
if let error = error {
|
if let error = error {
|
||||||
print("Error: \(error.localizedDescription)")
|
print("Error: \(error.localizedDescription)")
|
||||||
return
|
return
|
||||||
|
@ -43,24 +46,24 @@ struct DownloadButton: View {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if let data = data {
|
do {
|
||||||
do {
|
if let temporaryURL = temporaryURL {
|
||||||
print("Writing to \(filename)")
|
try FileManager.default.copyItem(at: temporaryURL, to: fileURL)
|
||||||
let fileURL = DownloadButton.getFileURL(filename: filename)
|
print("Writing to \(filename) completed")
|
||||||
try data.write(to: fileURL)
|
|
||||||
|
|
||||||
llamaState.cacheCleared = false
|
llamaState.cacheCleared = false
|
||||||
|
|
||||||
status = "downloaded"
|
status = "downloaded"
|
||||||
try llamaState.loadModel(modelUrl: fileURL)
|
|
||||||
} catch let err {
|
|
||||||
print("Error: \(err.localizedDescription)")
|
|
||||||
}
|
}
|
||||||
|
} catch let err {
|
||||||
|
print("Error: \(err.localizedDescription)")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
observation = downloadTask?.progress.observe(\.fractionCompleted) { progress, _ in
|
observation = downloadTask?.progress.observe(\.fractionCompleted) { progress, _ in
|
||||||
self.progress = progress.fractionCompleted
|
self.progress = progress.fractionCompleted
|
||||||
}
|
}
|
||||||
|
|
||||||
downloadTask?.resume()
|
downloadTask?.resume()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue