Skip to content

Commit

Permalink
Add downloadFileFromHuggingFaceRepository
Browse files Browse the repository at this point in the history
  • Loading branch information
pH-7 committed Jun 6, 2024
1 parent b338d56 commit efa4494
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions server/downloadFileFromHuggingFaceRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import path from "node:path";
import fs from "node:fs";
import { downloadFile } from "@huggingface/hub";

export async function downloadFileFromHuggingFaceRepository(
hfRepo: string,
hfRepoFile: string,
localFilePath: string,
): Promise<void> {
if (fs.existsSync(localFilePath)) return;

const downloadResponse = await downloadFile({
repo: hfRepo,
path: hfRepoFile,
});

if (!downloadResponse) {
throw new Error(`Failed to download file from ${hfRepo}/${hfRepoFile}`);
}

const fileArrayBuffer = await downloadResponse.arrayBuffer();

const fileBuffer = Buffer.from(fileArrayBuffer);

const fileDirectory = path.dirname(localFilePath);

if (!fs.existsSync(fileDirectory)) {
fs.mkdirSync(fileDirectory, { recursive: true });
}

fs.writeFileSync(localFilePath, fileBuffer);
}

0 comments on commit efa4494

Please sign in to comment.