Skip to content

Commit

Permalink
refactor: moved functions out of general util folder
Browse files Browse the repository at this point in the history
  • Loading branch information
Vali-98 committed Feb 6, 2025
1 parent 911d2b7 commit d2f9171
Show file tree
Hide file tree
Showing 19 changed files with 144 additions and 220 deletions.
3 changes: 2 additions & 1 deletion app/Logs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import Alert from '@components/views/Alert'
import FadeDownView from '@components/views/FadeDownView'
import { FontAwesome } from '@expo/vector-icons'
import { Theme } from '@lib/theme/ThemeManager'
import { Global, Logger, saveStringToDownload } from '@lib/utils/Global'
import { saveStringToDownload } from '@lib/utils/File'
import { Global, Logger } from '@lib/utils/Global'
import { FlashList } from '@shopify/flash-list'
import { Stack } from 'expo-router'
import { Pressable, Text, View } from 'react-native'
Expand Down
3 changes: 2 additions & 1 deletion app/SamplerMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import { APIState as APIStateNew } from '@lib/engine/API/APIManagerState'
import { localSamplerData } from '@lib/engine/LocalInference'
import { SamplersManager } from '@lib/state/SamplerState'
import { Theme } from '@lib/theme/ThemeManager'
import { Global, Logger, saveStringToDownload } from '@lib/utils/Global'
import { saveStringToDownload } from '@lib/utils/File'
import { Global, Logger } from '@lib/utils/Global'
import { useState } from 'react'
import { ScrollView, StyleSheet, Text, View } from 'react-native'
import { useMMKVString } from 'react-native-mmkv'
Expand Down
3 changes: 2 additions & 1 deletion app/screens/APIManager/TemplateItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import Alert from '@components/views/Alert'
import { APIConfiguration } from '@lib/engine/API/APIBuilder.types'
import { APIState } from '@lib/engine/API/APIManagerState'
import { Theme } from '@lib/theme/ThemeManager'
import { Logger, saveStringToDownload } from '@lib/utils/Global'
import { saveStringToDownload } from '@lib/utils/File'
import { Logger } from '@lib/utils/Global'
import { Text, View } from 'react-native'

type TemplateItemProps = {
Expand Down
3 changes: 2 additions & 1 deletion app/screens/ChatMenu/ChatEditPopup.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import Alert from '@components/views/Alert'
import PopupMenu, { MenuRef } from '@components/views/PopupMenu'
import TextBoxModal from '@components/views/TextBoxModal'
import { Characters, Chats, Logger, saveStringToDownload } from '@lib/utils/Global'
import { saveStringToDownload } from '@lib/utils/File'
import { Characters, Chats, Logger } from '@lib/utils/Global'
import React, { useState } from 'react'
import { View } from 'react-native'

Expand Down
3 changes: 2 additions & 1 deletion app/screens/FormattingManager/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import PopupMenu from '@components/views/PopupMenu'
import TextBoxModal from '@components/views/TextBoxModal'
import useAutosave from '@lib/hooks/AutoSave'
import { Theme } from '@lib/theme/ThemeManager'
import { Instructs, Logger, MarkdownStyle, saveStringToDownload } from '@lib/utils/Global'
import { saveStringToDownload } from '@lib/utils/File'
import { Instructs, Logger, MarkdownStyle } from '@lib/utils/Global'
import { useLiveQuery } from 'drizzle-orm/expo-sqlite'
import { useState } from 'react'
import { SafeAreaView, ScrollView, Text, View } from 'react-native'
Expand Down
3 changes: 2 additions & 1 deletion app/screens/ModelManager/ModelItem.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import Alert from '@components/views/Alert'
import TextBoxModal from '@components/views/TextBoxModal'
import { AntDesign } from '@expo/vector-icons'
import { GGMLNameMap, Llama, readableFileSize } from '@lib/engine/LlamaLocal'
import { GGMLNameMap, Llama } from '@lib/engine/LlamaLocal'
import { Theme } from '@lib/theme/ThemeManager'
import { readableFileSize } from '@lib/utils/File'
import { Global } from '@lib/utils/Global'
import { ModelDataType } from 'db/schema'
import { useState } from 'react'
Expand Down
3 changes: 2 additions & 1 deletion app/screens/ModelManager/ModelSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import ThemedSlider from '@components/input/ThemedSlider'
import ThemedSwitch from '@components/input/ThemedSwitch'
import SectionTitle from '@components/text/SectionTitle'
import Alert from '@components/views/Alert'
import { Llama, LlamaPreset, readableFileSize } from '@lib/engine/LlamaLocal'
import { Llama, LlamaPreset } from '@lib/engine/LlamaLocal'
import { readableFileSize } from '@lib/utils/File'
import { AppSettings, Global, Logger } from '@lib/utils/Global'
import { useFocusEffect } from 'expo-router'
import React, { useEffect, useState } from 'react'
Expand Down
13 changes: 1 addition & 12 deletions app/screens/TTSMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,13 @@ import ThemedTextInput from '@components/input/ThemedTextInput'
import SectionTitle from '@components/text/SectionTitle'
import HeaderTitle from '@components/views/HeaderTitle'
import { Theme } from '@lib/theme/ThemeManager'
import { groupBy } from '@lib/utils/Array'
import { Global, Logger } from '@lib/utils/Global'
import * as Speech from 'expo-speech'
import { useEffect, useState } from 'react'
import { View } from 'react-native'
import { useMMKVBoolean, useMMKVObject } from 'react-native-mmkv'

function groupBy(array: any[], key: string) {
if (array.length === 0) return []
return array.reduce((result, obj) => {
const keyValue = obj[key]
if (!result[keyValue]) {
result[keyValue] = []
}
result[keyValue].push(obj)
return result
}, {})
}

type LanguageListItem = {
[key: string]: Speech.Voice[]
}
Expand Down
26 changes: 10 additions & 16 deletions lib/constants/GlobalValues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ export const enum Global {

// UserID = 'userid', // moved to zustand state

// Presets
// Removed on preset rework
// PresetID = 'presetID',
// PresetData = 'presetdata',
// PresetName = 'presetdame',

// Lorebooks
// LorebookNames = 'lorebooknames', // removed with temp menu

// APIs

/**
Expand Down Expand Up @@ -84,21 +93,6 @@ export const enum Global {
Logs = 'logs',

AppMode = 'appmode',
// User

// Chat

// Instruct

// Presets
// TODO: Remove once migration complete
PresetID = 'presetID',
PresetData = 'presetdata',
PresetName = 'presetdame',

// Lorebooks

LorebookNames = 'lorebooknames',

// Local

Expand All @@ -109,7 +103,7 @@ export const enum Global {
// whether or not a KV cache has been loaded
LocalSessionLoaded = 'localsessionloaded',

// TODO move to AppSettings
// TODO move to zustaind state
TTSSpeaker = 'ttsspeaker',
TTSEnable = 'ttsenable',
TTSAuto = `ttsauto`,
Expand Down
2 changes: 1 addition & 1 deletion lib/engine/API/ContextBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { AppMode, AppSettings } from '@lib/constants/GlobalValues'
import { Llama } from '@lib/engine/LlamaLocal'
import { Tokenizer } from '@lib/engine/Tokenizer'
import { replaceMacros } from '@lib/state/Characters'
import { Characters, Chats, Global, Instructs, Logger, mmkv } from '@lib/utils/Global'
import { replaceMacros } from '@lib/utils/Macros'

import { APIConfiguration, APIValues } from './APIBuilder.types'

Expand Down
22 changes: 3 additions & 19 deletions lib/engine/LlamaLocal.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { db } from '@db'
import { CompletionParams, ContextParams, LlamaContext, initLlama } from 'cui-llama.rn'
import { readableFileSize } from '@lib/utils/File'
import { CompletionParams, ContextParams, initLlama, LlamaContext } from 'cui-llama.rn'
import { model_data, ModelDataType } from 'db/schema'
import { eq } from 'drizzle-orm'
import { getDocumentAsync } from 'expo-document-picker'
Expand Down Expand Up @@ -411,6 +412,7 @@ export namespace Llama {
.returning({ id: model_data.id })

const modelContext = await initLlama({ model: file_path, vocab_only: true })

const modelInfo: any = modelContext.model
const modelType = modelInfo.metadata?.['general.architecture']
const modelDataEntry = {
Expand Down Expand Up @@ -595,24 +597,6 @@ export const GGMLNameMap = {
[GGMLType.LLAMA_FTYPE_MOSTLY_TQ2_0]: 'TQ2_0',
}

const gb = 1000 ** 3
const mb = 1000 ** 2

/**
* Gets a human friendly version of file size
* @param size size in bytes
* @returns string containing readable file size
*/
export const readableFileSize = (size: number) => {
if (size < gb) {
const sizeInMB = size / mb
return `${sizeInMB.toFixed(2)} MB`
} else {
const sizeInGB = size / gb
return `${sizeInGB.toFixed(2)} GB`
}
}

const checkGGMLDeprecated = (model: ModelDataType) => {
const type = parseInt(model.quantization)
return (
Expand Down
3 changes: 1 addition & 2 deletions lib/state/Chat.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { db as database } from '@db'
import { Tokenizer } from '@lib/engine/Tokenizer'
import { useTTSState } from '@lib/state/TTS'
import { replaceMacros } from '@lib/utils/Macros'
import { convertToFormatInstruct } from '@lib/utils/TextFormat'
import { replaceMacros } from '@lib/utils/Utils'
import { chatEntries, chatSwipes, chats } from 'db/schema'
import { count, desc, eq, getTableColumns } from 'drizzle-orm'
import * as Notifications from 'expo-notifications'
Expand All @@ -11,7 +11,6 @@ import { useShallow } from 'zustand/react/shallow'

import { Characters } from './Characters'
import { Logger } from './Logger'
import { API } from '../constants/API'
import { AppMode, AppSettings, Global } from '../constants/GlobalValues'
import { Llama } from '../engine/LlamaLocal'
import { mmkv } from '../storage/MMKV'
Expand Down
2 changes: 1 addition & 1 deletion lib/state/Instructs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Logger } from './Logger'
import { AppMode, Global } from '../constants/GlobalValues'
import { Llama } from '../engine/LlamaLocal'
import { mmkv, mmkvStorage } from '../storage/MMKV'
import { replaceMacros } from '../utils/Utils'
import { replaceMacros } from '../utils/Macros'

const defaultBooleans = {
wrap: false,
Expand Down
11 changes: 11 additions & 0 deletions lib/utils/Array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function groupBy(array: any[], key: string) {
if (array.length === 0) return []
return array.reduce((result, obj) => {
const keyValue = obj[key]
if (!result[keyValue]) {
result[keyValue] = []
}
result[keyValue].push(obj)
return result
}, {})
}
34 changes: 34 additions & 0 deletions lib/utils/File.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { DownloadDirectoryPath, writeFile } from 'cui-fs'

/**
*
* @param data string data of file
* @param filename filename to be written, include extension
* @param encoding encoding of file
*/
export const saveStringToDownload = async (
data: string,
filename: string,
encoding: 'ascii' | 'base64' | `utf8`
) => {
if (encoding === 'utf8') data = btoa(data)
await writeFile(`${DownloadDirectoryPath}/${filename}`, data, { encoding: encoding })
}

const gb = 1000 ** 3
const mb = 1000 ** 2

/**
* Gets a human friendly version of file size
* @param size size in bytes
* @returns string containing readable file size
*/
export const readableFileSize = (size: number) => {
if (size < gb) {
const sizeInMB = size / mb
return `${sizeInMB.toFixed(2)} MB`
} else {
const sizeInGB = size / gb
return `${sizeInGB.toFixed(2)} GB`
}
}
Loading

0 comments on commit d2f9171

Please sign in to comment.