Back

image classification function (templates) using huggingface

  • 0
  • Self Hosted
  • Functions
  • Web
discorrrdbomba
13 Jul, 2024, 11:17

Can it be the case that there was a change on the abstraction/syntax of the return value from getFile (which eventually stems from getfileDownload in /setup.js)?! HEre is my Code: ```import { HfInference } from '@huggingface/inference'; import { throwIfMissing } from './utils.js'; import AppwriteService from './appwrite.js';

export default async ({ req, res, log, error }) => { throwIfMissing(process.env, ['HUGGINGFACE_ACCESS_TOKEN', 'APPWRITE_API_KEY']);

const databaseId = process.env.APPWRITE_DATABASE_ID ?? 'ai'; const collectionId = process.env.APPWRITE_COLLECTION_ID ?? 'image_classification'; const bucketId = process.env.APPWRITE_BUCKET_ID ?? 'image_classification';

if (req.method !== 'POST') { return res.json({ ok: false, error: 'Method not allowed' }, 405); }

...other code...

const appwrite = new AppwriteService();

let file; try { file = await appwrite.getFile(bucketId, fileId); log('BucketId: ' + bucketId); log('FileId: ' + fileId); log('File-START:'); log(file); log('File-END.'); } catch (err) { if (err.code === 404) { error(err); return res.json({ ok: false, error: 'File not found' }, 404); }

TypeScript
error(err);
return res.json({ ok: false, error: 'Bad request' }, 400);

}

const hf = new HfInference(process.env.HUGGINGFACE_ACCESS_TOKEN);

const result = await hf.imageClassification({ data: file, model: 'microsoft/resnet-50', }); log('Result: ' + result);

...other code...

log('Image ' + fileId + ' classified', result); return res.json({ "result":result, "fileId":fileId, "fileList": fileList, "file": file }); };```

...which returns this log/response (empty file obj):

TypeScript
FileId: 669250c300280ebeb5fe
File-START:
{}
File-END.
Result: [object Object],[object Object],[object Object],[object Object],[object Object]
Image 669250c300280ebeb5fe classified```
TL;DR
Developers are trying to pass a file object to a Huggingface function within a template, but the file object returned from `getFileDownload` is empty. They suspect a change in the syntax of the return value. The current code suggests the file object is not being retrieved correctly. Potential Solution: Make sure the file object retrieved from `appwrite.getFile` is correctly passed to the Huggingface function `imageClassification`. Double-check the variable assignment, permissions, and ensure the file object contains the expected data.
discorrrdbomba
13 Jul, 2024, 11:27

as i have read in the docs the "file" returned from getFileDownload should respond with headers attached that would tell a browser to initiate a download (...The endpoint response return with a 'Content-Disposition: attachment' header that tells the browser to start downloading the file to user downloads directory...) - so far so good, but how could this be passed as a file object to the huggingface function within the offical template, it should have been tested...?! Or am missing some part?

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more