@@ -10,6 +10,13 @@ const DEFAULT_HUGGING_FACE_OPTIONS = {
1010 } ,
1111} ;
1212
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+
1320/**
1421 * An user friendly API for onnx backend
1522 */
@@ -28,14 +35,17 @@ class UserInferenceSession {
2835 this . outputs = session . outputNames ;
2936 }
3037
31- static async fromUrl ( modelUrl ) {
38+ static async fromUrl ( modelUrl , authorization ) {
3239 if ( modelUrl instanceof URL ) {
3340 modelUrl = modelUrl . toString ( ) ;
3441 }
3542
3643 const encoder = new TextEncoder ( ) ;
3744 const modelUrlBuffer = encoder . encode ( modelUrl ) ;
38- const session = await InferenceSession . fromBuffer ( modelUrlBuffer ) ;
45+ const session = await InferenceSession . fromRequest (
46+ modelUrlBuffer ,
47+ authorization ,
48+ ) ;
3949
4050 return new UserInferenceSession ( session ) ;
4151 }
@@ -61,6 +71,26 @@ class UserInferenceSession {
6171 return await UserInferenceSession . fromUrl ( new URL ( modelPath , hostname ) ) ;
6272 }
6373
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+ }
6494 async run ( inputs ) {
6595 const outputs = await core . ops . op_ai_ort_run_session ( this . id , inputs ) ;
6696
0 commit comments