Integrate Hugging Face model library and separate web actions
This commit is contained in:
+32
-16
@@ -43,6 +43,37 @@ actor MLXProvider: InferenceProvider {
|
||||
unload()
|
||||
}
|
||||
|
||||
func load(_ directory: String) async throws {
|
||||
guard !active else { throw StudioError("MLX is busy.") }
|
||||
active = true
|
||||
eviction?.cancel()
|
||||
defer {
|
||||
active = false
|
||||
scheduleEviction()
|
||||
}
|
||||
_ = try await container(for: directory)
|
||||
}
|
||||
|
||||
private func container(for directory: String) async throws -> ModelContainer {
|
||||
let model: ModelContainer
|
||||
if let loaded, loaded.directory == directory {
|
||||
model = loaded.container
|
||||
} else {
|
||||
loaded = nil
|
||||
let url = URL(fileURLWithPath: directory, isDirectory: true)
|
||||
guard FileManager.default.fileExists(atPath: url.appending(path: "config.json").path) else {
|
||||
throw StudioError("Select an MLX model folder containing config.json and model weights.")
|
||||
}
|
||||
if let weightLimit, try MLXRuntimeLimits.weightBytes(in: url) > weightLimit {
|
||||
throw StudioError("The model’s weight files exceed the configured MLX weight limit.")
|
||||
}
|
||||
model = try await LLMModelFactory.shared.loadContainer(configuration: .init(directory: url))
|
||||
try Task.checkCancellation()
|
||||
loaded = (directory, model)
|
||||
}
|
||||
return model
|
||||
}
|
||||
|
||||
nonisolated func generate(_ request: InferenceRequest) -> AsyncThrowingStream<Delta, Error> {
|
||||
streaming { continuation in
|
||||
try await self.generate(request, continuation: continuation)
|
||||
@@ -60,22 +91,7 @@ actor MLXProvider: InferenceProvider {
|
||||
scheduleEviction()
|
||||
}
|
||||
let directory = request.model
|
||||
let model: ModelContainer
|
||||
if let loaded, loaded.directory == directory {
|
||||
model = loaded.container
|
||||
} else {
|
||||
loaded = nil
|
||||
let url = URL(fileURLWithPath: directory, isDirectory: true)
|
||||
guard FileManager.default.fileExists(atPath: url.appending(path: "config.json").path) else {
|
||||
throw StudioError("Select an MLX model folder containing config.json and model weights.")
|
||||
}
|
||||
if let weightLimit, try MLXRuntimeLimits.weightBytes(in: url) > weightLimit {
|
||||
throw StudioError("The model’s weight files exceed the configured MLX weight limit.")
|
||||
}
|
||||
model = try await LLMModelFactory.shared.loadContainer(configuration: .init(directory: url))
|
||||
try Task.checkCancellation()
|
||||
loaded = (directory, model)
|
||||
}
|
||||
let model = try await container(for: directory)
|
||||
let history: [Chat.Message] = request.messages.map {
|
||||
switch $0.role {
|
||||
case .system: .system($0.content)
|
||||
|
||||
Reference in New Issue
Block a user