File size: 1,631 Bytes
94b17d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
 * Hugging Face API Client
 *
 * Uses the official @huggingface/hub library to search and retrieve models
 */

import { listModels } from "@huggingface/hub";
import type { ModelEntry, PipelineType } from "@huggingface/hub";
import { config as dotenvConfig } from "dotenv";

// Load environment variables from .env file
dotenvConfig();

export interface SearchModelsOptions {
  task?: PipelineType;
  search?: string;
  limit?: number;
}

/**
 * Search models on Hugging Face Hub
 */
export async function searchModels(options: SearchModelsOptions = {}): Promise<ModelEntry[]> {
  const { task, search, limit } = options;

  const resultGenerator = listModels({
    search: {
      task: task,
      query: search,
      tags: ["transformers.js"]
    },
    limit: limit,
    credentials: {
      accessToken: process.env.HF_TOKEN,
    },
  });

  const models: ModelEntry[] = [];
  // Fetch models using the official HF Hub library
  for await (const model of resultGenerator) {
    // Filter out private and gated models
    if (!model.private && !model.gated) {
      models.push(model);
    }

    // Stop when we reach the limit (if specified)
    if (limit !== undefined && models.length >= limit) {
      break;
    }
  }

  return models;
}

/**
 * Format model for display
 */
export function formatModel(model: ModelEntry): string {
  const downloads = model.downloads ? `${(model.downloads / 1000).toFixed(1)}k` : "N/A";
  const likes = model.likes || 0;
  const task = model.task || "unknown";
  const name = model.name || model.id;

  return `${name} | Task: ${task} | Downloads: ${downloads} | Likes: ${likes}`;
}