Commit e827156
refactor!: add static factories for remaining models (#937)
## Description
This PR refactors the existing codebase to match what has been recently
done with object detection & semantic segmentation. Namely, it changes
the model initialization flow from `new Module() -> module.load()` to a
single, static factory, for example: `fromModelName(modelName)`
All affected modules now follow the same two-method factory API:
- **`Module.fromModelName(namedSources, onDownloadProgress?)`** — for
built-in models. Requires a `modelName` field (typed literal union, e.g.
`LLMModelName`, `SpeechToTextModelName`) that will be used for telemetry
once that infrastructure is in place.
- **`Module.fromCustomModel(...sources, onDownloadProgress?)`** — for
user-provided model binaries. Does **not** expose a `modelName`
parameter — users with custom models shouldn't need to know about or
satisfy a type constraint that exists purely for internal tracking.
Note: for now `fromCustomModel` internally delegates to `fromModelName`
with a `'custom'` placeholder. The two methods will diverge meaningfully
once telemetry is wired up.
### Modules migrated
| Module | Notes |
|---|---|
| `ClassificationModule` | factory + `fromCustomModel` |
| `StyleTransferModule` | factory + `fromCustomModel` |
| `ImageEmbeddingsModule` | factory + `fromCustomModel` |
| `VADModule` | factory + `fromCustomModel` |
| `TextEmbeddingsModule` | factory + `fromCustomModel` |
| `OCRModule` | factory + `fromCustomModel`, typed `OCRModelName` |
| `VerticalOCRModule` | factory + `fromCustomModel` |
| `LLMModule` | factory + `fromCustomModel`, typed `LLMModelName` |
| `SpeechToTextModule` | factory + `fromCustomModel`, typed
`SpeechToTextModelName` |
| `TextToImageModule` | factory + `fromCustomModel`, typed
`TextToImageModelName` |
| `ObjectDetectionModule` | `fromCustomConfig` → `fromCustomModel`
rename |
| `SemanticSegmentationModule` | `fromCustomConfig` → `fromCustomModel`
rename |
`TextToSpeechModule` is **not** included — handled separately.
### Hook updates
Hooks for modules that have a controller (`OCRModule`,
`VerticalOCRModule`, `LLMModule`) already used the controller directly
and remain unchanged in structure. Hooks for the remaining migrated
modules (`useSpeechToText`, `useTextToImage`) were updated to use the
factory instead of `new Module() + load()`.
All hooks now include `modelName` in their `useEffect` dependency
arrays, enabling correct reload behavior when switching between built-in
models.
### Typed model name unions
Each module now has a corresponding typed union (e.g. `LLMModelName`,
`SpeechToTextModelName`, `OCRModelName`) derived from the existing model
constants. This replaces the previous use of untyped `string` and
provides compile-time safety when passing model identifiers.
### Introduces a breaking change?
- [x] Yes
- [ ] No
### Type of change
- [ ] Bug fix (change which fixes an issue)
- [ ] New feature (change which adds functionality)
- [ ] Documentation update (improves or adds clarity to existing
documentation)
- [x] Other (chores, tests, code style improvements etc.)
### Tested on
- [x] iOS
- [x] Android
### Testing instructions
- [x] Run demo apps and check if they work.
- [x] Try to pass model name that is incorrect to check if error is
correctly risen.
- [x] Try using `fromCustomModel` method.
### Screenshots
<!-- Add screenshots here, if applicable -->
### Related issues
<!-- Link related issues here using #issue-number -->
### Checklist
- [x] I have performed a self-review of my code
- [x] I have commented my code, particularly in hard-to-understand areas
- [x] I have updated the documentation accordingly
- [x] My changes generate no new warnings
### Additional notes
<!-- Include any additional information, assumptions, or context that
reviewers might need to understand this PR. -->
---------
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-authored-by: Norbert Klockiewicz <Nklockiewicz12@gmail.com>1 parent a95a7c1 commit e827156
File tree
58 files changed
+1626
-813
lines changed- docs/docs
- 04-typescript-api
- 01-natural-language-processing
- 02-computer-vision
- 05-utilities
- packages/react-native-executorch/src
- constants
- ocr
- tts
- hooks
- computer_vision
- natural_language_processing
- modules
- computer_vision
- natural_language_processing
- types
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
58 files changed
+1626
-813
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
128 | 128 | | |
129 | 129 | | |
130 | 130 | | |
131 | | - | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
Lines changed: 51 additions & 34 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
26 | 25 | | |
27 | 26 | | |
28 | 27 | | |
| |||
41 | 40 | | |
42 | 41 | | |
43 | 42 | | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
| 43 | + | |
51 | 44 | | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
58 | 53 | | |
59 | | - | |
| 54 | + | |
60 | 55 | | |
61 | | - | |
| 56 | + | |
62 | 57 | | |
63 | 58 | | |
64 | 59 | | |
65 | 60 | | |
66 | 61 | | |
67 | | - | |
| 62 | + | |
68 | 63 | | |
69 | 64 | | |
70 | 65 | | |
| |||
116 | 111 | | |
117 | 112 | | |
118 | 113 | | |
119 | | - | |
| 114 | + | |
120 | 115 | | |
121 | 116 | | |
122 | 117 | | |
123 | 118 | | |
124 | | - | |
125 | | - | |
126 | | - | |
127 | | - | |
128 | | - | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
129 | 124 | | |
130 | 125 | | |
131 | 126 | | |
132 | 127 | | |
133 | 128 | | |
134 | | - | |
135 | | - | |
136 | | - | |
137 | | - | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
138 | 134 | | |
139 | 135 | | |
140 | 136 | | |
| |||
161 | 157 | | |
162 | 158 | | |
163 | 159 | | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
164 | 181 | | |
165 | 182 | | |
166 | 183 | | |
Lines changed: 31 additions & 24 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
17 | | - | |
18 | | - | |
19 | | - | |
20 | | - | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
21 | 23 | | |
22 | 24 | | |
23 | 25 | | |
| |||
40 | 42 | | |
41 | 43 | | |
42 | 44 | | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
| 45 | + | |
47 | 46 | | |
48 | | - | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
49 | 50 | | |
50 | | - | |
| 51 | + | |
51 | 52 | | |
52 | | - | |
| 53 | + | |
53 | 54 | | |
54 | | - | |
| 55 | + | |
55 | 56 | | |
56 | 57 | | |
57 | 58 | | |
| |||
66 | 67 | | |
67 | 68 | | |
68 | 69 | | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
73 | 76 | | |
74 | 77 | | |
75 | 78 | | |
| |||
121 | 124 | | |
122 | 125 | | |
123 | 126 | | |
124 | | - | |
125 | | - | |
126 | | - | |
127 | | - | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
128 | 133 | | |
129 | 134 | | |
130 | 135 | | |
| |||
163 | 168 | | |
164 | 169 | | |
165 | 170 | | |
166 | | - | |
167 | | - | |
168 | | - | |
169 | | - | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
170 | 177 | | |
171 | 178 | | |
172 | 179 | | |
| |||
Lines changed: 7 additions & 12 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
25 | 23 | | |
26 | 24 | | |
27 | 25 | | |
| |||
33 | 31 | | |
34 | 32 | | |
35 | 33 | | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
| 34 | + | |
41 | 35 | | |
42 | | - | |
| 36 | + | |
| 37 | + | |
43 | 38 | | |
44 | | - | |
| 39 | + | |
45 | 40 | | |
46 | 41 | | |
47 | 42 | | |
| |||
Lines changed: 18 additions & 27 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
31 | 25 | | |
32 | 26 | | |
33 | 27 | | |
| |||
39 | 33 | | |
40 | 34 | | |
41 | 35 | | |
42 | | - | |
| 36 | + | |
43 | 37 | | |
44 | | - | |
45 | | - | |
46 | | - | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
47 | 41 | | |
48 | | - | |
| 42 | + | |
49 | 43 | | |
50 | | - | |
| 44 | + | |
51 | 45 | | |
52 | 46 | | |
53 | 47 | | |
| |||
83 | 77 | | |
84 | 78 | | |
85 | 79 | | |
86 | | - | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
87 | 84 | | |
88 | 85 | | |
89 | 86 | | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | 87 | | |
96 | 88 | | |
97 | 89 | | |
| |||
117 | 109 | | |
118 | 110 | | |
119 | 111 | | |
120 | | - | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
121 | 116 | | |
122 | 117 | | |
123 | | - | |
124 | | - | |
125 | 118 | | |
126 | 119 | | |
127 | 120 | | |
| |||
155 | 148 | | |
156 | 149 | | |
157 | 150 | | |
158 | | - | |
159 | | - | |
160 | | - | |
| 151 | + | |
161 | 152 | | |
162 | 153 | | |
163 | 154 | | |
| |||
Lines changed: 9 additions & 9 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
17 | | - | |
18 | | - | |
19 | | - | |
20 | | - | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
21 | 20 | | |
22 | 21 | | |
23 | 22 | | |
| |||
28 | 27 | | |
29 | 28 | | |
30 | 29 | | |
31 | | - | |
| 30 | + | |
32 | 31 | | |
33 | | - | |
34 | | - | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
35 | 35 | | |
36 | | - | |
| 36 | + | |
37 | 37 | | |
38 | | - | |
| 38 | + | |
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
| |||
0 commit comments