Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
Update menu structure and settings view

Fix SwiftUI sync issue in SwiftUI views

Add Show Cache… option
  • Loading branch information
ronaldmannak committed Mar 22, 2024
1 parent 01ebca8 commit 1f07bda
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 58 deletions.
12 changes: 8 additions & 4 deletions PicoMLXServer.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
F818D9702B96A4DE00BA4664 /* PicoMLXServerUITestsLaunchTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F818D96F2B96A4DE00BA4664 /* PicoMLXServerUITestsLaunchTests.swift */; };
F818D97D2B96A7B400BA4664 /* MenuExtra.swift in Sources */ = {isa = PBXBuildFile; fileRef = F818D97C2B96A7B400BA4664 /* MenuExtra.swift */; };
F818D97F2B96A80500BA4664 /* SettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F818D97E2B96A80500BA4664 /* SettingsView.swift */; };
F846FDC92BACBFB5005EE38A /* Bundle.swift in Sources */ = {isa = PBXBuildFile; fileRef = F846FDC82BACBFB5005EE38A /* Bundle.swift */; };
F84D2A6A2BAA4C5900FB2D41 /* ServerCellView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F84D2A692BAA4C5900FB2D41 /* ServerCellView.swift */; };
F861C3842BAA67E4004BBB86 /* ServerLogView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F861C3832BAA67E4004BBB86 /* ServerLogView.swift */; };
F88F11102BA92D5B004743A5 /* ServerManagerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F88F110F2BA92D5B004743A5 /* ServerManagerView.swift */; };
Expand Down Expand Up @@ -97,6 +98,7 @@
F818D96F2B96A4DE00BA4664 /* PicoMLXServerUITestsLaunchTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PicoMLXServerUITestsLaunchTests.swift; sourceTree = "<group>"; };
F818D97C2B96A7B400BA4664 /* MenuExtra.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MenuExtra.swift; sourceTree = "<group>"; };
F818D97E2B96A80500BA4664 /* SettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsView.swift; sourceTree = "<group>"; };
F846FDC82BACBFB5005EE38A /* Bundle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Bundle.swift; sourceTree = "<group>"; };
F84D2A692BAA4C5900FB2D41 /* ServerCellView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ServerCellView.swift; sourceTree = "<group>"; };
F861C3832BAA67E4004BBB86 /* ServerLogView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ServerLogView.swift; sourceTree = "<group>"; };
F88F110F2BA92D5B004743A5 /* ServerManagerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ServerManagerView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -158,6 +160,7 @@
children = (
F8145FD72BA16DD90086CBE7 /* String.swift */,
F8145FF02BA226170086CBE7 /* NSApplication.swift */,
F846FDC82BACBFB5005EE38A /* Bundle.swift */,
);
path = Extensions;
sourceTree = "<group>";
Expand Down Expand Up @@ -439,6 +442,7 @@
buildActionMask = 2147483647;
files = (
F8907A3C2BA8B42200246735 /* ServerOperation.swift in Sources */,
F846FDC92BACBFB5005EE38A /* Bundle.swift in Sources */,
F8145FFE2BA27C960086CBE7 /* DependencyState.swift in Sources */,
F88F11122BA92D75004743A5 /* ServerListMenu.swift in Sources */,
F818D97F2B96A80500BA4664 /* SettingsView.swift in Sources */,
Expand Down Expand Up @@ -623,7 +627,7 @@
CODE_SIGN_ENTITLEMENTS = PicoMLXServer/PicoMLXServer.entitlements;
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 1;
CURRENT_PROJECT_VERSION = 3;
DEVELOPMENT_ASSET_PATHS = "\"PicoMLXServer/Preview Content\"";
DEVELOPMENT_TEAM = TPKP4XK352;
ENABLE_HARDENED_RUNTIME = YES;
Expand All @@ -636,7 +640,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 14.0;
MARKETING_VERSION = 0.1.1;
MARKETING_VERSION = 0.1.2;
PRODUCT_BUNDLE_IDENTIFIER = com.starlingprotocol.PicoMLXServer;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
Expand All @@ -652,7 +656,7 @@
CODE_SIGN_ENTITLEMENTS = PicoMLXServer/PicoMLXServer.entitlements;
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 1;
CURRENT_PROJECT_VERSION = 3;
DEVELOPMENT_ASSET_PATHS = "\"PicoMLXServer/Preview Content\"";
DEVELOPMENT_TEAM = TPKP4XK352;
ENABLE_HARDENED_RUNTIME = YES;
Expand All @@ -665,7 +669,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 14.0;
MARKETING_VERSION = 0.1.1;
MARKETING_VERSION = 0.1.2;
PRODUCT_BUNDLE_IDENTIFIER = com.starlingprotocol.PicoMLXServer;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
Expand Down
17 changes: 17 additions & 0 deletions PicoMLXServer/Extensions/Bundle.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// Bundle.swift
// PicoMLXServer
//
// Created by Ronald Mannak on 3/21/24.
//

import Foundation

extension Bundle {
var releaseVersionNumber: String? {
return infoDictionary?["CFBundleShortVersionString"] as? String
}
var buildVersionNumber: String? {
return infoDictionary?["CFBundleVersion"] as? String
}
}
12 changes: 8 additions & 4 deletions PicoMLXServer/Menu/MenuExtra.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,21 @@ struct MenuExtra: View {

Divider()

Button("Dependency Setup...") {
NSApplication.show()
openWindow(id: "setup")
}

SettingsLink()
.keyboardShortcut(",", modifiers: .command)

Button("Setup...") {
NSApplication.show()
openWindow(id: "setup")
Button("Check for Updates...") {
openURL(URL(string: "https://github.com/ronaldmannak/PicoMLXServer/tags")!)
}

Divider()

Button("Stop all servers") {
Button("Stop all Servers") {
serverController.stopAllServers()
}
.keyboardShortcut(".", modifiers: .command)
Expand Down
24 changes: 11 additions & 13 deletions PicoMLXServer/Menu/ServerListMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
//

import SwiftUI
import AppKit

struct ServerListMenu: View {

@Environment(ServerController.self) private var serverController
@Environment(\.openWindow) var openWindow
@Environment(\.openURL) var openURL

var body: some View {
Menu("Servers") {
Expand All @@ -29,6 +31,12 @@ struct ServerListMenu: View {
NSApplication.show()
openWindow(id: "servers")
}

Button("Show cache...") {
let url = FileManager.default.homeDirectoryForCurrentUser.appending(path: ".cache/huggingface/hub/")
NSWorkspace.shared.selectFile(nil, inFileViewerRootedAtPath: url.path)
}
.controlSize(.small)
}
}
}
Expand All @@ -40,27 +48,17 @@ struct ServerListMenu: View {

struct MenuToggle: View {

var server: Server
let server: Server
@State var isOn: Bool

var body: some View {
Toggle(isOn: $isOn) {
@Bindable var server = server
Toggle(isOn: $server.isOn) {
Text("\(server.model):\(String(server.port))")
.truncationMode(.head)
}
.onAppear {
print(server.isOn)
}
.onChange(of: isOn, initial: false) { _, newValue in
if newValue == false, server.isOn == true {
server.stop()
} else if newValue == true, server.isOn == false {
do {
try server.start()
} catch {
print(error)
}
}
}
}
}
5 changes: 0 additions & 5 deletions PicoMLXServer/PicoMLXServerApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@ struct PicoMLXServerApp: App {
.environment(serverController)
}

// Window("Server Logs", id: "serverLog") {
// ServerLogView()
// .environment(serverController)
// }

WindowGroup(id: "serverLog", for: UUID.self) { $serverUUID in
ServerLogView(serverUUID: serverUUID)
.environment(serverController)
Expand Down
22 changes: 12 additions & 10 deletions PicoMLXServer/Server Manager/Models/Server.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,38 +25,40 @@ final class Server: Identifiable {
var log: String = ""

var isOn: Bool {
if let operation {
if operation.isFinished || operation.isCancelled {
return false
} else if operation.isExecuting == false {
return false
didSet {
if isOn == true {
// TODO: Create a server state that stores error?
try? start()
} else {
return true
stop()
}
} else {
return false
}

}

/// Points to the ServerOperation in the server queue
/// If not nil, this server is running
@ObservationIgnored
weak var operation: ServerOperation? = nil

init(model: String, port: Int) {
self.model = model
self.port = port
self.isOn = false
}

func start() throws {
private func start() throws {
if operation == nil {
let operation = try serverOperation()
Queue.shared.serverQueue.addOperation(operation)
self.operation = operation
isOn = true
}
}

func stop() {
private func stop() {
operation?.cancel()
isOn = false
}

/// Creates a server operation
Expand Down
13 changes: 3 additions & 10 deletions PicoMLXServer/Server Manager/Models/ServerController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,13 @@ import SwiftUI
@Observable
final class ServerController {

private (set) var servers = [Server]()
var servers = [Server]()

// private (set) var activeServers = [Server]()

func addServer(model: String = "mlx-community/Nous-Hermes-2-Mistral-7B-DPO-4bit-MLX", port: Int = 8080) throws {

// Make sure port isn't in use
// guard activeServers.filter({ $0.port == port && $0.operation != nil }).isEmpty else {
// throw PicoError.portInUse(port)
// }
func addServer(model: String = "mlx-community/Nous-Hermes-2-Mistral-7B-DPO-4bit-MLX", port: Int = 8080) throws {

let server = Server(model: model, port: port)
servers.append(server)
try server.start()
server.isOn = true
}

func stopAllServers() {
Expand Down
11 changes: 1 addition & 10 deletions PicoMLXServer/Server Manager/ServerCellView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,7 @@ struct ServerCellView: View {
Text("\(String(server.port))")

Button(server.isOn ? "Stop" : "Start") {
if server.isOn {
server.stop()
} else {
do {
try server.start()
} catch {
self.error = error
showError = true
}
}
server.isOn.toggle()
}
.controlSize(.small)

Expand Down
1 change: 1 addition & 0 deletions PicoMLXServer/Server Manager/ServerManagerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ struct ServerManagerView: View {
// TODO: https://x.com/ronaldmannak/status/1770123553666711778?s=20
let models = [
"mlx-community/Nous-Hermes-2-Mistral-7B-DPO-4bit-MLX",
"mlx-community/NeuralBeagle14-7B-4bit-mlx",
"mlx-community/Mistral-7B-v0.1-hf-4bit-mlx",
"mlx-community/starcoder2-15b-4bit",
"mlx-community/quantized-gemma-2b-it",
Expand Down
18 changes: 16 additions & 2 deletions PicoMLXServer/SettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,24 @@ struct SettingsView: View {

var body: some View {

Form {
Section("Pico MLX Settings") {
Form {
Section {
TextField("Chat client url scheme", text: $chatClientURL)
Divider()
Toggle("Use Conda (recommended)", isOn: $useConda)
Divider()

} header: {
Text("Pico MLX Settings")
} footer: {
if let version = Bundle.main.releaseVersionNumber,
let build = Bundle.main.buildVersionNumber {
Text("Version \(version) (\(build))")
.foregroundColor(.gray)
.font(.footnote)
} else {
EmptyView()
}
}
}
.padding()
Expand Down

0 comments on commit 1f07bda

Please sign in to comment.