chore: add comments and clean unused imports

This commit is contained in:
Louis 2024-10-24 15:41:10 +07:00
parent 8f778ee90f
commit 90c7420c34
No known key found for this signature in database
GPG Key ID: 44FA9F4D33C37DE2
5 changed files with 37 additions and 3 deletions

View File

@ -200,7 +200,6 @@ const ModelDropdown = ({
if (model)
updateModelParameter(activeThread, {
params: modelParams,
// modelPath: model.file_path,
modelId: model.id,
engine: model.engine,
})

View File

@ -1,6 +1,5 @@
import { memo } from 'react'
import { InferenceEngine } from '@janhq/core'
import { Button } from '@janhq/joi'
import { useAtomValue, useSetAtom } from 'jotai'

View File

@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { Fragment, useEffect, useState } from 'react'
import { useEffect, useState } from 'react'
import { Accept, useDropzone } from 'react-dropzone'

View File

@ -1,5 +1,13 @@
import { AllQuantizations, getFileSize, HuggingFaceRepoData } from '@janhq/core'
/**
* Fetches data from a Hugging Face repository.
*
* @param repoId - The ID of the Hugging Face repository.
* @param huggingFaceAccessToken - Optional access token for Hugging Face API.
* @returns A promise that resolves to the HuggingFaceRepoData.
* @throws Will throw an error if the repository is not supported or if there is an error in the response.
*/
export const fetchHuggingFaceRepoData = async (
repoId: string,
huggingFaceAccessToken?: string
@ -60,31 +68,53 @@ export const fetchHuggingFaceRepoData = async (
return data
}
/**
* Converts a repository ID or URL to a valid Hugging Face API URL.
*
* @param repoId - The repository ID or URL to convert.
* @returns A string representing the Hugging Face API URL.
* @throws {InvalidHostError} If the URL is invalid or not from huggingface.co.
* @throws {Error} If the URL cannot be parsed.
*/
export function toHuggingFaceUrl(repoId: string): string {
try {
// Attempt to create a URL object from the repoId
const url = new URL(repoId)
// Check if the host is huggingface.co
if (url.host !== 'huggingface.co') {
throw new InvalidHostError(`Invalid Hugging Face repo URL: ${repoId}`)
}
// Split the pathname into parts and filter out empty strings
const paths = url.pathname.split('/').filter((e) => e.trim().length > 0)
// Ensure there are at least two parts in the path (user/repo)
if (paths.length < 2) {
throw new InvalidHostError(`Invalid Hugging Face repo URL: ${repoId}`)
}
// Construct and return the API URL
return `${url.origin}/api/models/${paths[0]}/${paths[1]}`
} catch (err) {
// Re-throw InvalidHostError if it was caught
if (err instanceof InvalidHostError) {
throw err
}
// If repoId starts with 'https' but couldn't be parsed, throw an error
if (repoId.startsWith('https')) {
throw new Error(`Cannot parse url: ${repoId}`)
}
// If repoId is not a URL, assume it's a valid repo ID and construct the API URL
return `https://huggingface.co/api/models/${repoId}`
}
}
/**
* Error thrown when the host of a URL is invalid or not from huggingface.co.
*/
export class InvalidHostError extends Error {
constructor(message: string) {
super(message)

View File

@ -1,3 +1,9 @@
/**
* Extracts and normalizes the model ID from a given download URL.
*
* @param downloadUrl - The URL from which to extract the model ID.
* @returns The extracted model ID, or the original URL if extraction fails.
*/
export const normalizeModelId = (downloadUrl: string): string => {
return downloadUrl.split('/').pop() ?? downloadUrl
}