-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathApp.tsx
More file actions
102 lines (93 loc) · 2.72 KB
/
App.tsx
File metadata and controls
102 lines (93 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import React from 'react';
import {
StyleSheet,
Text,
Button,
TextInput,
SafeAreaView,
} from 'react-native';
import * as FileSystem from 'expo-file-system';
import { Pipeline } from 'react-native-transformers';
import presets from './presets.json';
export default function App() {
const [progress, setProgress] = React.useState<number>();
const [input, setInput] = React.useState<string>('We love local LLM');
const [output, setOutput] = React.useState<string>();
const loadModel = async (preset: {
name: string;
model: string;
onnx_path: string;
options?: any;
}) => {
console.log('loading');
await Pipeline.TextGeneration.init(preset.model, preset.onnx_path, {
verbose: true,
fetch: async (url) => {
try {
console.log('Checking file... ' + url);
const fileName = url.split('/').pop()!;
const localPath = FileSystem.documentDirectory + fileName;
// Check if the file already exists
const fileInfo = await FileSystem.getInfoAsync(localPath);
if (fileInfo.exists) {
console.log('File already exists: ' + localPath);
return localPath;
}
console.log('Downloading... ' + url);
const downloadResumable = FileSystem.createDownloadResumable(
url,
localPath,
{},
({ totalBytesWritten, totalBytesExpectedToWrite }) => {
setProgress(totalBytesWritten / totalBytesExpectedToWrite);
}
);
const result = await downloadResumable.downloadAsync();
if (!result) {
throw new Error('Download failed.');
}
console.log('Downloaded to: ' + result.uri);
return result.uri;
} catch (error) {
console.error('Download error:', error);
return null;
}
},
...preset.options,
});
console.log('loaded');
};
const AutoComplete = () => {
Pipeline.TextGeneration.generate(input, setOutput);
};
return (
<SafeAreaView style={styles.container}>
<Text>Select a model</Text>
{presets.map((preset) => (
<Button
key={preset.name}
title={preset.name}
onPress={() => {
loadModel(preset);
}}
/>
))}
<Text>Input: </Text>
<TextInput value={input} onChangeText={setInput} style={styles.input} />
<Text>Output: {output}</Text>
<Text>{progress}</Text>
<Button title="Run" onPress={AutoComplete} />
</SafeAreaView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
input: {
borderWidth: 1,
borderColor: 'black',
},
});