feat: allow user to register their access token (#2974)
Signed-off-by: James <james@jan.ai> Co-authored-by: James <james@jan.ai>
This commit is contained in:
parent
f0f273bf07
commit
4edef30e0e
12
extensions/model-extension/resources/settings.json
Normal file
12
extensions/model-extension/resources/settings.json
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
[
|
||||||
|
{
|
||||||
|
"key": "hugging-face-access-token",
|
||||||
|
"title": "Hugging Face Access Token",
|
||||||
|
"description": "Access tokens programmatically authenticate your identity to the Hugging Face Hub, allowing applications to perform specific actions specified by the scope of permissions granted.",
|
||||||
|
"controllerType": "input",
|
||||||
|
"controllerProps": {
|
||||||
|
"value": "",
|
||||||
|
"placeholder": "hf_**********************************"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
@ -4,6 +4,7 @@ import typescript from 'rollup-plugin-typescript2'
|
|||||||
import json from '@rollup/plugin-json'
|
import json from '@rollup/plugin-json'
|
||||||
import replace from '@rollup/plugin-replace'
|
import replace from '@rollup/plugin-replace'
|
||||||
|
|
||||||
|
const settingJson = require('./resources/settings.json')
|
||||||
const packageJson = require('./package.json')
|
const packageJson = require('./package.json')
|
||||||
const defaultModelJson = require('./resources/default-model.json')
|
const defaultModelJson = require('./resources/default-model.json')
|
||||||
|
|
||||||
@ -20,6 +21,7 @@ export default [
|
|||||||
replace({
|
replace({
|
||||||
preventAssignment: true,
|
preventAssignment: true,
|
||||||
DEFAULT_MODEL: JSON.stringify(defaultModelJson),
|
DEFAULT_MODEL: JSON.stringify(defaultModelJson),
|
||||||
|
SETTINGS: JSON.stringify(settingJson),
|
||||||
NODE: JSON.stringify(`${packageJson.name}/${packageJson.node}`),
|
NODE: JSON.stringify(`${packageJson.name}/${packageJson.node}`),
|
||||||
}),
|
}),
|
||||||
// Allow json resolution
|
// Allow json resolution
|
||||||
|
|||||||
@ -31,6 +31,11 @@ import { GGUFMetadata, gguf } from '@huggingface/gguf'
|
|||||||
import { NotSupportedModelError } from './@types/NotSupportModelError'
|
import { NotSupportedModelError } from './@types/NotSupportModelError'
|
||||||
import { InvalidHostError } from './@types/InvalidHostError'
|
import { InvalidHostError } from './@types/InvalidHostError'
|
||||||
|
|
||||||
|
declare const SETTINGS: Array<any>
|
||||||
|
enum Settings {
|
||||||
|
huggingFaceAccessToken = 'hugging-face-access-token',
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A extension for models
|
* A extension for models
|
||||||
*/
|
*/
|
||||||
@ -63,6 +68,7 @@ export default class JanModelExtension extends ModelExtension {
|
|||||||
*/
|
*/
|
||||||
async onLoad() {
|
async onLoad() {
|
||||||
// Handle Desktop Events
|
// Handle Desktop Events
|
||||||
|
this.registerSettings(SETTINGS)
|
||||||
this.handleDesktopEvents()
|
this.handleDesktopEvents()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -195,7 +201,21 @@ export default class JanModelExtension extends ModelExtension {
|
|||||||
const sanitizedUrl = this.toHuggingFaceUrl(repoId)
|
const sanitizedUrl = this.toHuggingFaceUrl(repoId)
|
||||||
console.debug('sanitizedUrl', sanitizedUrl)
|
console.debug('sanitizedUrl', sanitizedUrl)
|
||||||
|
|
||||||
const res = await fetch(sanitizedUrl)
|
const huggingFaceAccessToken = (
|
||||||
|
await this.getSetting<string>(Settings.huggingFaceAccessToken, '')
|
||||||
|
).trim()
|
||||||
|
|
||||||
|
const headers = {
|
||||||
|
Accept: 'application/json',
|
||||||
|
}
|
||||||
|
|
||||||
|
if (huggingFaceAccessToken.length > 0) {
|
||||||
|
headers['Authorization'] = `Bearer ${huggingFaceAccessToken}`
|
||||||
|
}
|
||||||
|
|
||||||
|
const res = await fetch(sanitizedUrl, {
|
||||||
|
headers: headers,
|
||||||
|
})
|
||||||
const response = await res.json()
|
const response = await res.json()
|
||||||
if (response['error'] != null) {
|
if (response['error'] != null) {
|
||||||
throw new Error(response['error'])
|
throw new Error(response['error'])
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user