Skip to content

Commit

Permalink
fix: fs not supported in react and change fileId to file_id
Browse files Browse the repository at this point in the history
  • Loading branch information
shahrear33 committed Feb 7, 2025
1 parent 2d3b13b commit b328076
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ const file = await client.files.upload({

// Process a document (using file id)
const response = await client.document.generate({
fileId: file.id,
file_id: file.id,
model: "vlm-1",
domain: "document.invoice",
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vlmrun",
"version": "0.1.11",
"version": "0.1.12",
"description": "The official TypeScript library for the VlmRun API",
"author": "VlmRun <[email protected]>",
"main": "dist/index.js",
Expand Down
9 changes: 6 additions & 3 deletions src/client/files.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { createHash } from "crypto";
import { readFile } from "fs/promises";
import { Client, APIRequestor } from "./base_requestor";
import { FileResponse, ListParams, FileUploadParams } from "./types";
import path from "path";
import { readFileFromPathAsFile } from "../utils/file";

export class Files {
Expand All @@ -24,7 +22,12 @@ export class Files {
}

private async calculateMD5(filePath: string): Promise<string> {
const fileBuffer = await readFile(filePath);
if (typeof window !== "undefined") {
throw new Error("File hashing is not supported in the browser");
}

const fs = require("fs/promises");
const fileBuffer = await fs.readFile(filePath);
return createHash("md5").update(fileBuffer).digest("hex");
}

Expand Down
4 changes: 2 additions & 2 deletions src/client/predictions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export class FilePredictions extends Predictions {

async generate(params: FilePredictionParams): Promise<PredictionResponse> {
const {
fileId,
file_id,
url,
model,
domain,
Expand All @@ -116,7 +116,7 @@ export class FilePredictions extends Predictions {
`/${this.route}/generate`,
undefined,
{
...(fileId ? { file_id: fileId } : { url }),
...(file_id ? { file_id } : { url }),
model,
domain,
batch,
Expand Down
2 changes: 1 addition & 1 deletion src/client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export interface ImagePredictionParams extends PredictionGenerateParams {
}

export interface FilePredictionParams extends PredictionGenerateParams {
fileId?: string;
file_id?: string;
url?: string;
}

Expand Down
20 changes: 12 additions & 8 deletions src/utils/file.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import path from "path";
import fs from "fs/promises";

export const readFileFromPathAsFile = async (filePath: string): Promise<File> => {
try {
const fileBuffer = await fs.readFile(filePath);
const fileName = path.basename(filePath);
return new File([fileBuffer], fileName, {
type: 'application/pdf',
});
if (typeof window === 'undefined') {
const fs = require("fs/promises");
const path = require('path');

const fileBuffer = await fs.readFile(filePath);
const fileName = path.basename(filePath);
return new File([fileBuffer], fileName, {
type: 'application/pdf',
});
} else {
throw new Error('File reading is not supported in the browser');
}
} catch (error: any) {
throw new Error(`Error reading file at ${filePath}: ${error.message}`);
}
Expand Down
5 changes: 4 additions & 1 deletion src/utils/image.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { readFileSync } from 'fs';
import { extname } from 'path';

/**
Expand All @@ -7,6 +6,10 @@ import { extname } from 'path';
* @returns Base64 encoded image with data URI prefix
*/
export function encodeImage(imagePath: string): string {
if (typeof window !== 'undefined') {
throw new Error('Image encoding is not supported in the browser');
}
const { readFileSync } = require('fs');
const imageBuffer = readFileSync(imagePath);
const ext = extname(imagePath).toLowerCase().slice(1);
const mimeType = `image/${ext === 'jpg' ? 'jpeg' : ext}`;
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/client/predictions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe("Integration: Predictions", () => {
});

const result = await client.document.generate({
fileId: uploadedDocument.id,
file_id: uploadedDocument.id,
model: "vlm-1",
domain: "document.invoice",
});
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/client/predictions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ describe("Predictions", () => {
.mockResolvedValue([mockResponse, 200, {}]);

const result = await documentPredictions.generate({
fileId: "doc1.pdf",
file_id: "doc1.pdf",
model: "model1",
domain: "domain1",
});
Expand Down Expand Up @@ -181,7 +181,7 @@ describe("Predictions", () => {
.mockResolvedValue([mockResponse, 200, {}]);

const result = await audioPredictions.generate({
fileId: "audio1.mp3",
file_id: "audio1.mp3",
model: "model1",
domain: "domain1",
});
Expand Down Expand Up @@ -229,7 +229,7 @@ describe("Predictions", () => {
.mockResolvedValue([mockResponse, 200, {}]);

const result = await videoPredictions.generate({
fileId: "video1.mp4",
file_id: "video1.mp4",
model: "model1",
domain: "domain1",
});
Expand Down

0 comments on commit b328076

Please sign in to comment.