-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathMaiaEngineContext.tsx
More file actions
153 lines (139 loc) · 3.77 KB
/
MaiaEngineContext.tsx
File metadata and controls
153 lines (139 loc) · 3.77 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
import Maia from '../lib/engine/maia'
import { MaiaStatus, MaiaEngine } from 'src/types'
import React, {
ReactNode,
useState,
useMemo,
useCallback,
useEffect,
useRef,
} from 'react'
import toast from 'react-hot-toast'
const MAIA_LOADING_TOAST_DELAY_MS = 800
export const MaiaEngineContext = React.createContext<MaiaEngine>({
maia: undefined,
status: 'loading',
progress: 0,
downloadModel: async () => {
throw new Error('poorly provided MaiaEngineContext, missing downloadModel')
},
})
export const MaiaEngineContextProvider: React.FC<{ children: ReactNode }> = ({
children,
}: {
children: ReactNode
}) => {
const [status, setStatus] = useState<MaiaStatus>('loading')
const [progress, setProgress] = useState(0)
const [error, setError] = useState<string | null>(null)
const toastId = useRef<string | null>(null)
const loadingToastTimerRef = useRef<number | null>(null)
const maia = useMemo(() => {
const model = new Maia({
model:
process.env.NEXT_PUBLIC_MAIA_MODEL_URL ??
'/maia3/maia3_simplified.onnx',
modelVersion: process.env.NEXT_PUBLIC_MAIA_MODEL_VERSION ?? '3',
setStatus: setStatus,
setProgress: setProgress,
setError: setError,
})
return model
}, [])
const downloadModel = useCallback(async () => {
try {
setStatus('downloading')
await maia.downloadModel()
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to download model')
setStatus('error')
}
}, [maia])
const getStorageInfo = useCallback(async () => {
return await maia.getStorageInfo()
}, [maia])
const clearStorage = useCallback(async () => {
return await maia.clearStorage()
}, [maia])
// Toast notifications for Maia model status
useEffect(() => {
return () => {
if (
loadingToastTimerRef.current !== null &&
typeof window !== 'undefined'
) {
window.clearTimeout(loadingToastTimerRef.current)
loadingToastTimerRef.current = null
}
if (toastId.current) {
toast.dismiss(toastId.current)
toastId.current = null
}
}
}, [])
useEffect(() => {
if (status === 'loading') {
if (
typeof window === 'undefined' ||
toastId.current ||
loadingToastTimerRef.current !== null
) {
return
}
loadingToastTimerRef.current = window.setTimeout(() => {
loadingToastTimerRef.current = null
if (!toastId.current) {
toastId.current = toast.loading('Loading Maia...')
}
}, MAIA_LOADING_TOAST_DELAY_MS)
return
}
if (
loadingToastTimerRef.current !== null &&
typeof window !== 'undefined'
) {
window.clearTimeout(loadingToastTimerRef.current)
loadingToastTimerRef.current = null
}
if (status === 'no-cache' || status === 'downloading') {
if (toastId.current) {
toast.dismiss(toastId.current)
toastId.current = null
}
return
}
if (status === 'ready') {
// Only show success if a loading toast was visible.
if (toastId.current) {
toast.success('Loaded Maia! Analysis is ready', {
id: toastId.current,
})
toastId.current = null
}
} else if (status === 'error') {
const message = error
? `Failed to load Maia model: ${error}`
: 'Failed to load Maia model'
if (toastId.current) {
toast.error(message, {
id: toastId.current,
})
toastId.current = null
} else {
toast.error(message)
}
}
}, [status, error])
return (
<MaiaEngineContext.Provider
value={{
maia,
status,
progress,
downloadModel,
}}
>
{children}
</MaiaEngineContext.Provider>
)
}