Background Fetch API দিয়ে AI মডেলগুলি ডাউনলোড করুন

প্রকাশিত: ফেব্রুয়ারি 20, 2025

নির্ভরযোগ্যভাবে বড় এআই মডেল ডাউনলোড করা একটি চ্যালেঞ্জিং কাজ। ব্যবহারকারীরা তাদের ইন্টারনেট সংযোগ হারিয়ে ফেললে বা আপনার ওয়েবসাইট বা ওয়েব অ্যাপ্লিকেশন বন্ধ করলে, তারা আংশিকভাবে ডাউনলোড করা মডেল ফাইলগুলি হারাবে এবং আপনার পৃষ্ঠায় ফিরে এসে আবার শুরু করতে হবে। একটি প্রগতিশীল বর্ধন হিসাবে ব্যাকগ্রাউন্ড ফেচ API ব্যবহার করে, আপনি ব্যবহারকারীর অভিজ্ঞতা উল্লেখযোগ্যভাবে উন্নত করতে পারেন।

Browser Support

  • ক্রোম: 74।
  • প্রান্ত: 79।
  • ফায়ারফক্স: সমর্থিত নয়।
  • সাফারি: সমর্থিত নয়।

Source

একটি সেবা কর্মী নিবন্ধন

Background Fetch API-এর জন্য আপনার অ্যাপের প্রয়োজন একজন পরিষেবা কর্মী নিবন্ধন করার জন্য।

if ('serviceWorker' in navigator) {
  window.addEventListener('load', async () => {
    const registration = await navigator.serviceWorker.register('sw.js');
    console.log('Service worker registered for scope', registration.scope);
  });
}

একটি পটভূমি আনা ট্রিগার

ব্রাউজারটি নিয়ে আসার সাথে সাথে এটি ব্যবহারকারীর কাছে অগ্রগতি প্রদর্শন করে এবং তাদের ডাউনলোড বাতিল করার একটি পদ্ধতি দেয়। একবার ডাউনলোড সম্পূর্ণ হলে, ব্রাউজারটি পরিষেবা কর্মী শুরু করে এবং অ্যাপ্লিকেশনটি প্রতিক্রিয়া সহ পদক্ষেপ নিতে পারে।

ব্যাকগ্রাউন্ড ফেচ এপিআই এমনকি অফলাইনে শুরু করার জন্য ফেচ প্রস্তুত করতে পারে। ব্যবহারকারী পুনরায় সংযোগ করার সাথে সাথে ডাউনলোড শুরু হয়। ব্যবহারকারী অফলাইনে গেলে, ব্যবহারকারী আবার অনলাইন না হওয়া পর্যন্ত প্রক্রিয়াটি বিরতি দেয়।

নিম্নলিখিত উদাহরণে, ব্যবহারকারী Gemma 2B ডাউনলোড করতে একটি বোতামে ক্লিক করেন। আমরা আনার আগে, আমরা মডেলটি আগে ডাউনলোড এবং ক্যাশে করা হয়েছে কিনা তা পরীক্ষা করি, তাই আমরা অপ্রয়োজনীয় সংস্থান ব্যবহার করি না। এটি ক্যাশে না থাকলে, আমরা ব্যাকগ্রাউন্ড ফেচ শুরু করি।

const FETCH_ID = 'gemma-2b';
const MODEL_URL =
  'https://storage.googleapis.com/jmstore/kaggleweb/grader/g-2b-it-gpu-int4.bin';

downloadButton.addEventListener('click', async (event) => {
  // If the model is already downloaded, return it from the cache.
  const modelAlreadyDownloaded = await caches.match(MODEL_URL);
  if (modelAlreadyDownloaded) {
    const modelBlob = await modelAlreadyDownloaded.blob();
    // Do something with the model.
    console.log(modelBlob);
    return;
  }

  // The model still needs to be downloaded.
  // Feature detection and fallback to classic `fetch()`.
  if (!('BackgroundFetchManager' in self)) {
    try {
      const response = await fetch(MODEL_URL);
      if (!response.ok || response.status !== 200) {
        throw new Error(`Download failed ${MODEL_URL}`);
      }
      const modelBlob = await response.blob();
      // Do something with the model.
      console.log(modelBlob);
      return;
    } catch (err) {
      console.error(err);
    }
  }

  // The service worker registration.
  const registration = await navigator.serviceWorker.ready;

  // Check if there's already a background fetch running for the `FETCH_ID`.
  let bgFetch = await registration.backgroundFetch.get(FETCH_ID);

  // If not, start a background fetch.
  if (!bgFetch) {
    bgFetch = await registration.backgroundFetch.fetch(FETCH_ID, MODEL_URL, {
      title: 'Gemma 2B model',
      icons: [
        {
          src: 'icon.png',
          size: '128x128',
          type: 'image/png',
        },
      ],
      downloadTotal: await getResourceSize(MODEL_URL),
    });
  }
});

getResourceSize() ফাংশন ডাউনলোডের বাইট আকার প্রদান করে। আপনি একটি HEAD অনুরোধ করে এটি বাস্তবায়ন করতে পারেন।

const getResourceSize = async (url) => {
  try {
    const response = await fetch(url, { method: 'HEAD' });
    if (response.ok) {
      return response.headers.get('Content-Length');
    }
    console.error(`HTTP error: ${response.status}`);
    return 0;
  } catch (error) {
    console.error('Error fetching content size:', error);
    return 0;
  }
};

ডাউনলোডের অগ্রগতির প্রতিবেদন করুন

একবার ব্যাকগ্রাউন্ড ফেচ শুরু হলে, ব্রাউজার একটি BackgroundFetchRegistration প্রদান করে। আপনি progress ইভেন্ট সহ ডাউনলোডের অগ্রগতি ব্যবহারকারীকে জানাতে এটি ব্যবহার করতে পারেন।

bgFetch.addEventListener('progress', (e) => {
  // There's no download progress yet.
  if (!bgFetch.downloadTotal) {
    return;
  }
  // Something went wrong.
  if (bgFetch.failureReason) {
    console.error(bgFetch.failureReason);
  }
  if (bgFetch.result === 'success') {
    return;
  }
  // Update the user about progress.
  console.log(`${bgFetch.downloaded} / ${bgFetch.downloadTotal}`);
});

আনয়ন সমাপ্তির ব্যবহারকারী এবং ক্লায়েন্টকে অবহিত করুন

যখন ব্যাকগ্রাউন্ড ফেচ সফল হয়, তখন আপনার অ্যাপের সার্ভিস কর্মী একটি backgroundfetchsuccess ইভেন্ট পায়।

নিম্নলিখিত কোড পরিষেবা কর্মী অন্তর্ভুক্ত করা হয়. শেষের কাছাকাছি updateUI() কলটি আপনাকে সফল ব্যাকগ্রাউন্ড আনার ব্যবহারকারীকে অবহিত করতে ব্রাউজারের ইন্টারফেস আপডেট করতে দেয়। অবশেষে, সমাপ্ত ডাউনলোড সম্পর্কে ক্লায়েন্টকে জানান, উদাহরণস্বরূপ, postMessage() ব্যবহার করে।

self.addEventListener('backgroundfetchsuccess', (event) => {
  // Get the background fetch registration.
  const bgFetch = event.registration;

  event.waitUntil(
    (async () => {
      // Open a cache named 'downloads'.
      const cache = await caches.open('downloads');
      // Go over all records in the background fetch registration.
      // (In the running example, there's just one record, but this way
      // the code is future-proof.)
      const records = await bgFetch.matchAll();
      // Wait for the response(s) to be ready, then cache it/them.
      const promises = records.map(async (record) => {
        const response = await record.responseReady;
        await cache.put(record.request, response);
      });
      await Promise.all(promises);

      // Update the browser UI.
      event.updateUI({ title: 'Model downloaded' });

      // Inform the clients that the model was downloaded.
      self.clients.matchAll().then((clientList) => {
        for (const client of clientList) {
          client.postMessage({
            message: 'download-complete',
            id: bgFetch.id,
          });
        }
      });
    })(),
  );
});

পরিষেবা কর্মীর কাছ থেকে বার্তা পান

ক্লায়েন্টে সম্পূর্ণ ডাউনলোড সম্পর্কে প্রেরিত সাফল্যের বার্তা পেতে, message ইভেন্টগুলির জন্য শুনুন। একবার আপনি পরিষেবা কর্মীর কাছ থেকে বার্তাটি পেয়ে গেলে, আপনি AI মডেলের সাথে কাজ করতে পারেন এবং এটি ক্যাশে API এর সাথে সংরক্ষণ করতে পারেন।

navigator.serviceWorker.addEventListener('message', async (event) => {
  const cache = await caches.open('downloads');
  const keys = await cache.keys();
  for (const key of keys) {
    const modelBlob = await cache
      .match(key)
      .then((response) => response.blob());
    // Do something with the model.
    console.log(modelBlob);
  }
});

একটি পটভূমি আনা বাতিল করুন

ব্যবহারকারীকে একটি চলমান ডাউনলোড বাতিল করতে দিতে, BackgroundFetchRegistration এর abort() পদ্ধতি ব্যবহার করুন।

const registration = await navigator.serviceWorker.ready;
const bgFetch = await registration.backgroundFetch.get(FETCH_ID);
if (!bgFetch) {
  return;
}
await bgFetch.abort();

মডেল ক্যাশে

ক্যাশে ডাউনলোড করা মডেল , তাই আপনার ব্যবহারকারীরা শুধুমাত্র একবার মডেল ডাউনলোড করুন। যদিও Background Fetch API ডাউনলোডের অভিজ্ঞতা উন্নত করে, আপনার সর্বদা লক্ষ্য থাকা উচিত ক্লায়েন্ট-সাইড এআই-তে সম্ভাব্য সবচেয়ে ছোট মডেলটি ব্যবহার করা।

একসাথে, এই APIগুলি আপনাকে আপনার ব্যবহারকারীদের জন্য একটি ভাল ক্লায়েন্ট-সাইড এআই অভিজ্ঞতা তৈরি করতে সহায়তা করে।

ডেমো

আপনি ডেমো এবং এর সোর্স কোডে এই পদ্ধতির সম্পূর্ণ বাস্তবায়ন দেখতে পারেন।

Chrome DevTools অ্যাপ্লিকেশান প্যানেল ব্যাকগ্রাউন্ড ফেচ ডাউনলোডের জন্য খোলা।
Chrome DevTools-এর সাহায্যে, আপনি চলমান ব্যাকগ্রাউন্ড আনার সাথে সম্পর্কিত ইভেন্টগুলির পূর্বরূপ দেখতে পারেন। ডেমোটি একটি চলমান ডাউনলোড দেখায় যা 17.54M মেগাবাইট সম্পন্ন হয়েছে, মোট 1.26 গিগাবাইট। ব্রাউজারের ডাউনলোড সূচকটি চলমান ডাউনলোডও দেখায়।

স্বীকৃতি

ফ্রাঙ্কোইস বিউফোর্ট , আন্দ্রে বান্দারা , সেবাস্টিয়ান বেঞ্জ , মাউড নাল্পাস , এবং আলেকজান্দ্রা ক্লেপার এই নির্দেশিকাটি পর্যালোচনা করেছেন।