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:
NamH 2024-05-31 13:15:06 +07:00 committed by GitHub
parent f0f273bf07
commit 4edef30e0e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 35 additions and 1 deletions

View 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_**********************************"
}
}
]

View File

@ -4,6 +4,7 @@ import typescript from 'rollup-plugin-typescript2'
import json from '@rollup/plugin-json'
import replace from '@rollup/plugin-replace'
const settingJson = require('./resources/settings.json')
const packageJson = require('./package.json')
const defaultModelJson = require('./resources/default-model.json')
@ -20,6 +21,7 @@ export default [
replace({
preventAssignment: true,
DEFAULT_MODEL: JSON.stringify(defaultModelJson),
SETTINGS: JSON.stringify(settingJson),
NODE: JSON.stringify(`${packageJson.name}/${packageJson.node}`),
}),
// Allow json resolution

View File

@ -31,6 +31,11 @@ import { GGUFMetadata, gguf } from '@huggingface/gguf'
import { NotSupportedModelError } from './@types/NotSupportModelError'
import { InvalidHostError } from './@types/InvalidHostError'
declare const SETTINGS: Array<any>
enum Settings {
huggingFaceAccessToken = 'hugging-face-access-token',
}
/**
* A extension for models
*/
@ -63,6 +68,7 @@ export default class JanModelExtension extends ModelExtension {
*/
async onLoad() {
// Handle Desktop Events
this.registerSettings(SETTINGS)
this.handleDesktopEvents()
}
@ -195,7 +201,21 @@ export default class JanModelExtension extends ModelExtension {
const sanitizedUrl = this.toHuggingFaceUrl(repoId)
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()
if (response['error'] != null) {
throw new Error(response['error'])