Skip to content

Commit

Permalink
feat(example): create dir for model cache
Browse files Browse the repository at this point in the history
  • Loading branch information
jhen0409 committed Aug 29, 2023
1 parent 8a4b863 commit 7cde772
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ yarn example android --mode release
This example used [react-native-document-picker](https://github.com/rnmods/react-native-document-picker) for select model.

- iOS: You can move the model to iOS Simulator, or iCloud for real device.
- Android: Selected file will be copied or downloaded to document directory so it may be slow.
- Android: Selected file will be copied or downloaded to cache directory so it may be slow.

## Mock `llama.rn`

Expand Down
8 changes: 6 additions & 2 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,15 @@ export default function App() {
let [file] = res
if (file) {
if (Platform.OS === 'android' && file.uri.startsWith('content://')) {
const filename = `${file.uri.split('/').pop() || 'model'}.gguf`
const filepath = `${ReactNativeBlobUtil.fs.dirs.DocumentDir}/${filename}`
const dir = `${ReactNativeBlobUtil.fs.dirs.CacheDir}/models`
if (!(await ReactNativeBlobUtil.fs.isDir(dir))) await ReactNativeBlobUtil.fs.mkdir(dir)

const filepath = `${dir}/${file.uri.split('/').pop() || 'model'}.gguf`
if (await ReactNativeBlobUtil.fs.exists(filepath)) {
handleInitContext({ uri: filepath } as DocumentPickerResponse)
return
} else {
await ReactNativeBlobUtil.fs.unlink(dir) // Clean up old files in models
}
addSystemMessage('Copying model to internal storage...')
await ReactNativeBlobUtil.MediaCollection.copyToInternal(
Expand Down

0 comments on commit 7cde772

Please sign in to comment.