Skip to content

Commit 9de598b

Browse files
committed
Update README and add local storage in example's fetch callback
1 parent 0bb09f1 commit 9de598b

File tree

2 files changed

+34
-16
lines changed

2 files changed

+34
-16
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ module.exports = {
8181
};
8282
```
8383

84+
You can follow this [document](https://docs.expo.dev/versions/latest/config/babel/) to create config file, and you need to run `npx expo start --clear` to clear the Metro bundler cache.
85+
8486
## Usage
8587

8688
### Text Generation

example/App.tsx

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,39 @@ export default function App() {
2525
await Pipeline.TextGeneration.init(preset.model, preset.onnx_path, {
2626
verbose: true,
2727
fetch: async (url) => {
28-
console.log("downloading... " + url);
29-
const localpath = FileSystem.cacheDirectory + url.split("/").pop()!;
30-
31-
const downloadResumable = FileSystem.createDownloadResumable(
32-
url,
33-
localpath,
34-
{},
35-
({ totalBytesWritten, totalBytesExpectedToWrite }) => {
36-
setProgress(totalBytesWritten / totalBytesExpectedToWrite);
37-
},
38-
);
39-
const result = await downloadResumable.downloadAsync();
40-
if (result === undefined) {
41-
throw new Error("Download failed.");
28+
try {
29+
console.log("Checking file... " + url);
30+
const fileName = url.split("/").pop()!;
31+
const localPath = FileSystem.documentDirectory + fileName;
32+
33+
// Check if the file already exists
34+
const fileInfo = await FileSystem.getInfoAsync(localPath);
35+
if (fileInfo.exists) {
36+
console.log("File already exists: " + localPath);
37+
return localPath;
38+
}
39+
40+
console.log("Downloading... " + url);
41+
const downloadResumable = FileSystem.createDownloadResumable(
42+
url,
43+
localPath,
44+
{},
45+
({ totalBytesWritten, totalBytesExpectedToWrite }) => {
46+
setProgress(totalBytesWritten / totalBytesExpectedToWrite);
47+
}
48+
);
49+
50+
const result = await downloadResumable.downloadAsync();
51+
if (!result) {
52+
throw new Error("Download failed.");
53+
}
54+
55+
console.log("Downloaded to: " + result.uri);
56+
return result.uri;
57+
} catch (error) {
58+
console.error("Download error:", error);
59+
return null;
4260
}
43-
console.log("downloaded as " + result.uri);
44-
return result.uri;
4561
},
4662
...preset.options,
4763
});

0 commit comments

Comments
 (0)