-
Notifications
You must be signed in to change notification settings - Fork 6
/
App.tsx
154 lines (142 loc) · 4.44 KB
/
App.tsx
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
*/
import React, { useState, useEffect, useCallback } from 'react';
import {
SafeAreaView,
ScrollView,
StatusBar,
StyleSheet,
useColorScheme,
Text,
View,
} from 'react-native';
import { pipeline } from '@xenova/transformers';
import { useColor } from './utils/style';
import InlineSection from './components/form/InlineSection';
import Section from './components/form/Section';
import SelectField from './components/form/SelectField';
import Progress from './components/Progress';
import Models from './components/models';
import * as logger from './utils/logger';
const tasks = Object.keys(Models);
const taskDisplayNames = tasks.map((task) => Models[task].title);
function App(): JSX.Element {
const isDarkMode = useColorScheme() === 'dark';
const backgroundColor = useColor('background');
const color = useColor('foreground');
const textColor = { color };
const [task, setTask] = useState<Nullable<string>>(null);
const [settings, setSettings] = useState<object>({});
const [params, setParams] = useState<Nullable<object>>(null);
const [download, setDownload] = useState<object>({});
const [isLoading, setLoading] = useState<boolean>(false);
const backgroundStyle = { backgroundColor };
useEffect(() => {
setDownload({});
setLoading(false);
}, [task]);
const onProgress = useCallback((event: any) => {
if (event?.file) {
const { file, status, progress } = event;
setLoading(true);
setDownload((prev) => ({
...prev,
[file]: { status, progress },
}));
}
if (event?.status === 'ready') {
setLoading(false);
}
}, []);
const run = useCallback(async (useTask, model, modelOpt, ...args) => {
if (!task || !useTask || !args?.length) return;
let pipe;
try {
logger.time('LOAD');
pipe = await pipeline(useTask, model, { ...modelOpt, progress_callback: onProgress });
logger.timeEnd('LOAD');
logger.time('INFER');
const result = await pipe._call(...args);
logger.timeEnd('INFER');
await pipe.dispose();
logger.log('Result:', result);
return result;
} catch (e) {
console.error(e.stack);
await pipe?.dispose();
throw e;
}
}, [task, onProgress]);
const SettingsComponent = Models[task]?.Settings;
const ParametersComponent = Models[task]?.Parameters;
const InteractComponent = Models[task]?.Interact;
return (
<SafeAreaView style={backgroundStyle}>
<StatusBar
barStyle={isDarkMode ? 'light-content' : 'dark-content'}
backgroundColor={backgroundColor}
/>
<ScrollView
contentInsetAdjustmentBehavior="automatic"
style={backgroundStyle}>
<View style={styles.container}>
<Text style={[styles.title, textColor]}># transformers.js</Text>
<InlineSection title="Task">
<SelectField
options={Object.keys(Models)}
value={task}
onChange={setTask}
optionLabels={taskDisplayNames}
placeholder="Select a task"
/>
</InlineSection>
<InlineSection title="Settings">
{SettingsComponent ? (
<SettingsComponent onChange={setSettings} />
) : (
<Text style={textColor}>Select task first</Text>
)}
</InlineSection>
<InlineSection title="Parameters">
{ParametersComponent ? (
<ParametersComponent onChange={setParams} />
) : (
<Text style={textColor}>N/A</Text>
)}
</InlineSection>
<Section title="Interact">
{InteractComponent ? (
<InteractComponent settings={settings} params={params} runPipe={run} />
) : (
<Text style={textColor}>N/A</Text>
)}
</Section>
{isLoading && (
<Section title="Progress">
{Object.entries(download).map(([key, { progress, status }]) => (
<Progress key={key} title={key} value={progress} status={status} />
))}
</Section>
)}
</View>
</ScrollView>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
title: {
fontSize: 28,
fontWeight: 'bold',
textAlign: 'center',
},
container: {
flex: 1,
paddingTop: 20,
paddingBottom: 80,
},
});
export default App;