Skip to content

Commit

Permalink
missing part of code
Browse files Browse the repository at this point in the history
Moved constants and setting functions to config;
Added bool settings for various ui options;
  • Loading branch information
Lypsilonx committed Feb 25, 2024
1 parent 148bae8 commit 4621ac3
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 72 deletions.
145 changes: 80 additions & 65 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import json
import rumps
import requests
import threading
Expand All @@ -8,13 +7,6 @@

import config


ICON = "resources/icon.png"
ICON_INSTALLED = "resources/installed.png"
ICON_INSTALLING = "resources/installing.png"
ICON_UNINSTALLED = "resources/uninstalled.png"


def setup():
if not os.path.exists(config.model_folder):
if input(f"Model folder {config.model_folder} does not exist. Create it? (y/n) ").lower() == 'y':
Expand Down Expand Up @@ -64,90 +56,112 @@ def is_installing(model):
return os.path.exists(f"{config.model_folder}/{model}.lock")
else:
return False

def set_settings(setting, value):
with open('settings.json', 'r') as f:
settings = json.load(f)

settings[setting] = value

with open('settings.json', 'w') as f:
json.dump(settings, f, indent=4)

def get_settings(setting):
with open('settings.json', 'r') as f:
settings = json.load(f)

return settings[setting]

class ModelPickerApp(rumps.App):
def __init__(self):
super(ModelPickerApp, self).__init__("ModelPickerApp")
super(ModelPickerApp, self).__init__("ModelPickerApp", quit_button=None)

self.rebuild_menu()
self.icon = config.ICON
rumps.Timer(self.update_menu, 5).start()

# Dynamically create menu items from the MENUBAR_OPTIONS
def rebuild_menu(self):
self.menu.clear()
self.menu_items = {}
show_uninstalled = config.get_settings(config.SETTINGS_SHOW_UNINSTALLED)
for option in config.models:
if not show_uninstalled and config.models[option]['type'] == 'local' and not is_installed(option):
continue
self.menu_items[option] = rumps.MenuItem(
title=option, callback=self.pick_model, icon=None)

self.menu_items['Settings'] = rumps.MenuItem(
title='Settings', icon=None)

self.add_settings_menu('Switch', ['Automatic', 'Trigger Offline', 'Manual'])
self.add_settings_menu('Default Online', filter(lambda x: config.models[x]['type'] == 'remote', config.models))
self.add_settings_menu('Default Offline', filter(lambda x: config.models[x]['type'] == 'local', config.models))

self.add_bool_setting(config.SETTINGS_SHOW_UNINSTALLED, True)
self.add_bool_setting(config.SETTINGS_SHOW_STATUS_ICONS, True)
self.add_bool_setting(config.SETTINGS_SHOW_CURRENT_MODEL, True)
self.add_settings_menu(config.SETTINGS_SWITCH, ['Automatic', 'Trigger Offline', 'Manual'])
self.add_settings_menu(config.SETTINGS_DEFAULT_ONLINE, filter(lambda x: config.models[x]['type'] == 'remote', config.models))
self.add_settings_menu(config.SETTINGS_DEFAULT_OFFLINE, filter(lambda x: config.models[x]['type'] == 'local' and (show_uninstalled or is_installed(x)), config.models))

self.menu_items['Quit'] = rumps.MenuItem(title='Quit', callback=rumps.quit_application, icon=None)

self.menu = list(self.menu_items.values())
self.menu_items[get_settings('Default Online')].state = True
self.title = get_settings('Default Online')
self.icon = ICON
rumps.Timer(self.update_menu, 5).start()
self.menu_items[config.get_settings(config.SETTINGS_CURRENT_MODEL)].state = True
self.update_menu(None)

if config.get_settings(config.SETTINGS_SHOW_CURRENT_MODEL):
self.title = config.get_settings(config.SETTINGS_CURRENT_MODEL)
else:
self.title = None

def add_settings_menu(self, name, options):
def add_settings_menu(self, name, options, triggerRebuild = False):
self.menu_items["Settings"].add(rumps.MenuItem(title=name, icon=None))
selected_option = get_settings(name)
selected_option = config.get_settings(name)

for option in options:
self.menu_items["Settings"][name].add(
rumps.MenuItem(title=option, callback=lambda sender: self.set_setting(sender, name), icon=None))
rumps.MenuItem(title=option, callback=lambda sender: self.set_setting(sender, name, triggerRebuild), icon=None))
if option == selected_option:
self.menu_items["Settings"][name][option].state = True

def set_setting(self, sender, setting):
def add_bool_setting(self, name, triggerRebuild = False):
self.menu_items["Settings"].add(
rumps.MenuItem(title=name, callback=lambda sender: self.set_bool_setting(sender, name, triggerRebuild), icon=None))
self.menu_items["Settings"][name].state = config.get_settings(name)

def set_setting(self, sender, setting, triggerRebuild = False):
if sender.state:
return

set_settings(setting, sender.title)
config.set_settings(setting, sender.title)

if triggerRebuild:
self.rebuild_menu()
else:
for item in self.menu['Settings'][setting]:
self.menu_items['Settings'][setting][item].state = item == sender.title

def set_bool_setting(self, sender, setting, triggerRebuild = False):
config.set_settings(setting, not sender.state)

for item in self.menu['Settings'][setting]:
self.menu_items['Settings'][setting][item].state = item == sender.title
if triggerRebuild:
self.rebuild_menu()
else:
sender.state = config.get_settings(setting)


def update_menu(self, sender):
status_icons = config.get_settings(config.SETTINGS_SHOW_STATUS_ICONS)
for option in self.menu_items:
if not option in config.models:
continue
if is_installing(option):
self.menu_items[option].icon = ICON_INSTALLING
elif is_installed(option):
self.menu_items[option].icon = ICON_INSTALLED
if status_icons:
if is_installing(option):
self.menu_items[option].icon = config.ICON_INSTALLING
elif is_installed(option):
self.menu_items[option].icon = config.ICON_INSTALLED
else:
self.menu_items[option].icon = config.ICON_UNINSTALLED
else:
self.menu_items[option].icon = ICON_UNINSTALLED
self.menu_items[option].icon = None

currently_online = config.models[self.title]['type'] == 'remote'
currently_online = config.models[config.get_settings(config.SETTINGS_CURRENT_MODEL)]['type'] == 'remote'

if currently_online and get_settings('Switch') == 'Automatic' or get_settings('Switch') == 'Trigger Offline':
if currently_online and config.get_settings(config.SETTINGS_SWITCH) == 'Automatic' or config.get_settings(config.SETTINGS_SWITCH) == 'Trigger Offline':
try:
response = requests.get("http://google.com", timeout=1.0)
if response.status_code != 200:
self.pick_model(self.menu_items[get_settings('Default Offline')])
self.pick_model(self.menu_items[config.get_settings(config.SETTINGS_DEFAULT_OFFLINE)])
except requests.RequestException as e:
self.pick_model(self.menu_items[get_settings('Default Offline')])
self.pick_model(self.menu_items[config.get_settings(config.SETTINGS_DEFAULT_OFFLINE)])

if not currently_online and get_settings('Switch') == 'Automatic':
if not currently_online and config.get_settings(config.SETTINGS_SWITCH) == 'Automatic':
try:
response = requests.get("http://google.com", timeout=1.0)
if response.status_code == 200:
self.pick_model(self.menu_items[get_settings('Default Online')])
self.pick_model(self.menu_items[config.get_settings(config.SETTINGS_DEFAULT_ONLINE)])
except requests.RequestException as e:
pass

Expand All @@ -169,22 +183,23 @@ def pick_model(self, sender):
return

# Send the choice to the local proxy app
if sender.state:
choice = sender.title
try:
response = requests.post(
"http://localhost:5001/set_target", json={"target": choice}, timeout=1.0)
if response.status_code == 200:
print(f"Successfully sent selection: {choice}.")
else:
rumps.alert(
"Error", f"Failed to send selection. Server responded with: {response.status_code}.")
except requests.RequestException as e:
rumps.alert("Error", f"Failed to send selection. Error: {e}.")
return
choice = sender.title
try:
response = requests.post(
"http://localhost:5001/set_target", json={"target": choice}, timeout=1.0)
if response.status_code == 200:
print(f"Successfully sent selection: {choice}.")
else:
rumps.alert(
"Error", f"Failed to send selection. Server responded with: {response.status_code}.")
except requests.RequestException as e:
rumps.alert("Error", f"Failed to send selection. Error: {e}.")
return

# Toggle the checked status of the clicked menu item
self.title = sender.title
if config.get_settings(config.SETTINGS_SHOW_CURRENT_MODEL):
self.title = sender.title
config.set_settings(config.SETTINGS_CURRENT_MODEL, sender.title)

# If other options were previously selected, deselect them
for item in self.menu:
Expand Down
38 changes: 38 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
import os
import json

ICON = "resources/icon.png"
ICON_INSTALLED = "resources/installed.png"
ICON_INSTALLING = "resources/installing.png"
ICON_UNINSTALLED = "resources/uninstalled.png"

SETTINGS_CURRENT_MODEL = "Current Model"
SETTINGS_SHOW_UNINSTALLED = "Show Uninstalled"
SETTINGS_SHOW_STATUS_ICONS = "Show Status Icons"
SETTINGS_SHOW_CURRENT_MODEL = "Show Current Model"
SETTINGS_DEFAULT_ONLINE = "Default Online"
SETTINGS_DEFAULT_OFFLINE = "Default Offline"
SETTINGS_SWITCH = "Switch"

SWITCH_AUTOMATIC = "Automatic"
SWITCH_TRIGGER_OFFLINE = "Trigger Offline"
SWITCH_MANUAL = "Manual"

models = {
'GitHub': {
Expand All @@ -15,6 +33,11 @@
'type': 'local',
'filename': 'mistral-7b-instruct-v0.1.Q5_K_M.gguf',
},
"stable-code-3b": {
"url": "https://huggingface.co/stabilityai/stable-code-3b/resolve/main/stable-code-3b-Q5_K_M.gguf",
"type": "local",
"filename": "stable-code-3b-Q5_K_M.gguf",
},
'CodeLlama-34b': {
'url': 'https://huggingface.co/TheBloke/CodeLlama-34B-Instruct-GGUF/resolve/main/codellama-34b-instruct.Q4_K_M.gguf',
'type': 'local',
Expand All @@ -23,3 +46,18 @@
}

model_folder = os.path.expanduser('~/models')

def set_settings(setting, value):
with open('settings.json', 'r') as f:
settings = json.load(f)

settings[setting] = value

with open('settings.json', 'w') as f:
json.dump(settings, f, indent=4)

def get_settings(setting):
with open('settings.json', 'r') as f:
settings = json.load(f)

return settings[setting]
9 changes: 2 additions & 7 deletions proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,11 @@
import logging
from starlette import applications, responses, exceptions
from starlette.requests import Request
import json

def get_settings(setting):
with open('settings.json', 'r') as f:
settings = json.load(f)

return settings[setting]
import config

app = applications.Starlette()
state = config.models[get_settings("Default Online")]
state = config.models[config.get_settings(config.SETTINGS_DEFAULT_ONLINE)]
local_server_process = None
logging.basicConfig(level=logging.DEBUG)

Expand Down
4 changes: 4 additions & 0 deletions settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
{
"Current Model": "GitHub",
"Show Uninstalled": false,
"Show Status Icons": true,
"Show Current Model": true,
"Default Online": "GitHub",
"Default Offline": "CodeLlama-7b",
"Switch": "Automatic"
Expand Down

0 comments on commit 4621ac3

Please sign in to comment.