@@ -10,6 +10,13 @@ const DEFAULT_HUGGING_FACE_OPTIONS = {
10
10
} ,
11
11
} ;
12
12
13
+ const DEFAULT_STORAGE_OPTIONS = ( ) => ( {
14
+ hostname : Deno . env . get ( 'SUPABASE_URL' ) ,
15
+ mode : {
16
+ authorization : Deno . env . get ( 'SUPABASE_SERVICE_ROLE_KEY' ) ,
17
+ } ,
18
+ } ) ;
19
+
13
20
/**
14
21
* An user friendly API for onnx backend
15
22
*/
@@ -28,14 +35,17 @@ class UserInferenceSession {
28
35
this . outputs = session . outputNames ;
29
36
}
30
37
31
- static async fromUrl ( modelUrl ) {
38
+ static async fromUrl ( modelUrl , authorization ) {
32
39
if ( modelUrl instanceof URL ) {
33
40
modelUrl = modelUrl . toString ( ) ;
34
41
}
35
42
36
43
const encoder = new TextEncoder ( ) ;
37
44
const modelUrlBuffer = encoder . encode ( modelUrl ) ;
38
- const session = await InferenceSession . fromBuffer ( modelUrlBuffer ) ;
45
+ const session = await InferenceSession . fromRequest (
46
+ modelUrlBuffer ,
47
+ authorization ,
48
+ ) ;
39
49
40
50
return new UserInferenceSession ( session ) ;
41
51
}
@@ -61,6 +71,26 @@ class UserInferenceSession {
61
71
return await UserInferenceSession . fromUrl ( new URL ( modelPath , hostname ) ) ;
62
72
}
63
73
74
+ static async fromStorage ( modelPath , opts = { } ) {
75
+ const defaultOpts = DEFAULT_STORAGE_OPTIONS ( ) ;
76
+ const hostname = opts ?. hostname ?? defaultOpts . hostname ;
77
+ const mode = opts ?. mode ?? defaultOpts . mode ;
78
+
79
+ const assetPath = mode === 'public' ? `public/${ modelPath } ` : `authenticated/${ modelPath } ` ;
80
+
81
+ const storageUrl = `/storage/v1/object/${ assetPath } ` ;
82
+
83
+ if ( ! URL . canParse ( storageUrl , hostname ) ) {
84
+ throw Error (
85
+ `[Invalid URL] Couldn't parse the model path: "${ storageUrl } "` ,
86
+ ) ;
87
+ }
88
+
89
+ return await UserInferenceSession . fromUrl (
90
+ new URL ( storageUrl , hostname ) ,
91
+ mode ?. authorization ,
92
+ ) ;
93
+ }
64
94
async run ( inputs ) {
65
95
const outputs = await core . ops . op_ai_ort_run_session ( this . id , inputs ) ;
66
96
0 commit comments