File size: 2,076 Bytes
eda28cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
67
68
import { ProviderInfo, ModelData } from "../types/heatmap";

export async function fetchUserData(authors: string[]) {
  const primaryAuthor = authors[0];
  try {
    const response = await fetch(`https://huggingface.co/api/users/${primaryAuthor}/overview`);
    const data = await response.json();
    return {
      fullName: data.fullname || primaryAuthor,
      avatarUrl: data.avatarUrl || null, 
    };
  } catch (error) {
    console.error(`Error fetching user data for ${primaryAuthor}:`, error);
    return {
      fullName: primaryAuthor,
      avatarUrl: null, 
    };
  }
}

export async function fetchAllProvidersData(providers: ProviderInfo[]): Promise<ProviderInfo[]> {
  return Promise.all(providers.map(async (providerInfo) => {
    const { fullName, avatarUrl } = await fetchUserData(providerInfo.authors);
    return { 
      ...providerInfo, 
      fullName, 
      avatarUrl: avatarUrl || null 
    };
  }));
}

export async function fetchAuthorData(author: string): Promise<ModelData[]> {
  const entityTypes = ["models", "datasets", "spaces"] as const;
  try {
    const allData = await Promise.all(
      entityTypes.map(async (type) => {
        const response = await fetch(
          `https://huggingface.co/api/${type}?author=${author}&sort=createdAt&direction=-1`
        );
        if (!response.ok) {
          throw new Error(`HTTP error! status: ${response.status}`);
        }
        const data = await response.json();
        return data.map((item: any): ModelData => ({
          createdAt: item.createdAt,
          id: item.id,
        }));
      })
    );

    return allData.flat();
  } catch (error) {
    console.error(`Error fetching data for author ${author}:`, error);
    return [];
  }
}

export async function fetchAllAuthorsData(authors: string[]): Promise<ModelData[]> {
  try {
    const allData = await Promise.all(
      authors.map(async (author) => await fetchAuthorData(author))
    );
    return allData.flat();
  } catch (error) {
    console.error("Error fetching data for all authors:", error);
    return [];
  }
}