Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ vi.mock('@huggingface/hub', () => {
return {
scanCacheDir: vi.fn(),
snapshotDownload: vi.fn(),
getHFHubCachePath: vi.fn(),
};
});

Expand Down
50 changes: 31 additions & 19 deletions packages/backend/src/models/HuggingFaceModelHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import { ModelHandler } from './ModelHandler';
import type { ModelInfo } from '@shared/models/IModelInfo';
import { Downloader } from '../utils/downloader';
import { scanCacheDir, snapshotDownload } from '@huggingface/hub';
import { scanCacheDir, snapshotDownload, getHFHubCachePath } from '@huggingface/hub';
import type { CompletionEvent } from './baseEvent';
import { getDurationSecondsSince } from '../utils/utils';
import type { ModelsManager } from '../managers/modelsManager';
Expand Down Expand Up @@ -120,26 +120,38 @@ export class HuggingFaceModelHandler extends ModelHandler {
})
.filter(info => info.repo);

scanCacheDir()
.then(hfinfo => {
for (const repo of hfinfo.repos) {
for (const revision of repo.revisions) {
for (const ref of revision.refs) {
const model = hfModels.find(m => m.repo?.repo === repo.id.name && m.repo?.revision === ref);
if (model) {
model.model.file = {
path: revision.path,
file: '',
creation: revision.lastModifiedAt,
size: revision.size,
};
// Only scan cache if we have HF models to check
if (hfModels.length > 0) {
// Check if cache directory exists before scanning
try {
const cachePath = getHFHubCachePath();
await fs.access(cachePath);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

praise: It was once stated that using existsSync should be preferred to async access as the cost of throwing exception could be greater.

} catch {
// Cache directory doesn't exist, skip scanning
return;
}

scanCacheDir()
.then(hfinfo => {
for (const repo of hfinfo.repos) {
for (const revision of repo.revisions) {
for (const ref of revision.refs) {
const model = hfModels.find(m => m.repo?.repo === repo.id.name && m.repo?.revision === ref);
if (model) {
model.model.file = {
path: revision.path,
file: '',
creation: revision.lastModifiedAt,
size: revision.size,
};
}
}
}
}
}
})
.catch((err: unknown): void => {
console.error('Something went wrong while scanning cache.', err);
});
})
.catch((err: unknown): void => {
console.error('Something went wrong while scanning cache.', err);
});
}
}
}
Loading