-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathApp.tsx
More file actions
163 lines (150 loc) · 4.88 KB
/
App.tsx
File metadata and controls
163 lines (150 loc) · 4.88 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
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
155
156
157
158
159
160
161
162
163
/**
* 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, PipelineType } from '@huggingface/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';
import type { Settings, InputParams } from './components/models/common/types';
function App(): React.JSX.Element {
const isDarkMode = useColorScheme() === 'dark';
const backgroundColor = useColor('background');
const color = useColor('foreground');
const textColor = { color };
const [task, setTask] = useState<keyof typeof Models | null>(null);
const [settings, setSettings] = useState<Settings>({});
const [params, setParams] = useState<any>({});
const [download, setDownload] = useState<Record<string, any>>({});
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: string, model: string, modelOpt: object, ...args: any[]) => {
if (!task || !useTask || !args?.length) {return;}
let pipe;
try {
logger.time('LOAD');
pipe = await pipeline(useTask as PipelineType, model, { ...modelOpt, progress_callback: onProgress });
logger.timeEnd('LOAD');
logger.time('INFER');
// @ts-ignore
const result = await pipe._call(...args);
logger.timeEnd('INFER');
await pipe.dispose();
logger.log('Result:', result);
return result;
} catch (e) {
console.error((e as Error).stack);
await pipe?.dispose();
throw e;
}
}, [task, onProgress]);
const handleSettingsChange = useCallback((newSettings: Settings) => {
setSettings(newSettings);
}, []);
const handleParamsChange = useCallback((newParams: InputParams) => {
setParams(newParams);
}, []);
const SettingsComponent = task && Models[task]?.Settings;
const ParametersComponent = task && Models[task]?.Parameters;
const InteractComponent = task && 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.entries(Models).map(([key, value]) => ({
label: value.title,
value: key,
}))}
value={task}
onChange={(value) => setTask(value)}
placeholder="Select a task"
/>
</InlineSection>
<InlineSection title="Settings">
{SettingsComponent ? (
<SettingsComponent onChange={handleSettingsChange} />
) : (
<Text style={textColor}>Select task first</Text>
)}
</InlineSection>
<InlineSection title="Parameters">
{ParametersComponent ? (
<ParametersComponent onChange={handleParamsChange} />
) : (
<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 }]: [string, any]) => (
<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;