File size: 9,339 Bytes
f555806
 
84fde91
f555806
 
 
 
 
f1ba847
f555806
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f1ba847
f555806
 
 
 
f1ba847
 
 
 
 
f555806
 
f1ba847
 
 
 
 
84fde91
f1ba847
 
 
 
 
 
 
 
5c34458
84fde91
4ff2db9
 
f1ba847
 
 
 
 
 
 
 
 
 
 
5c34458
f1ba847
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5c34458
f1ba847
 
 
 
 
 
 
 
 
 
 
f555806
f1ba847
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84fde91
 
 
 
f555806
 
f1ba847
 
f555806
 
f1ba847
 
 
 
 
 
f555806
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f1ba847
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import { NextRequest, NextResponse } from 'next/server';
import { whoAmI, createRepo, uploadFiles, datasetInfo } from '@huggingface/hub';
import { readdir, stat, readFile } from 'fs/promises';
import path from 'path';

export async function POST(request: NextRequest) {
  try {
    const body = await request.json();
    const { action, token, namespace, datasetName, datasetPath, datasetId, artifacts, manifest } = body;

    if (!token) {
      return NextResponse.json({ error: 'HF token is required' }, { status: 400 });
    }

    switch (action) {
      case 'whoami':
        try {
          const user = await whoAmI({ accessToken: token });
          return NextResponse.json({ user });
        } catch (error) {
          return NextResponse.json({ error: 'Invalid token or network error' }, { status: 401 });
        }

      case 'createDataset':
        try {
          if (!namespace || !datasetName) {
            return NextResponse.json({ error: 'Namespace and dataset name required' }, { status: 400 });
          }

          const repoId = `datasets/${namespace}/${datasetName}`;
          
          // Create repository
          await createRepo({
            repo: repoId,
            accessToken: token,
            private: false,
          });

          return NextResponse.json({ success: true, repoId });
        } catch (error: any) {
          if (error.message?.includes('already exists')) {
            return NextResponse.json({ success: true, repoId: `${namespace}/${datasetName}`, exists: true });
          }
          return NextResponse.json({ error: error.message || 'Failed to create dataset' }, { status: 500 });
        }

      case 'uploadDataset':
        try {
          if (!namespace || !datasetName) {
            return NextResponse.json({ error: 'Missing required parameters' }, { status: 400 });
          }

          const repoId = `datasets/${namespace}/${datasetName}`;
          const structuredArtifacts = Array.isArray(artifacts) ? artifacts : [];
          const hasStructuredArtifacts = structuredArtifacts.length > 0;

          if (!hasStructuredArtifacts && !datasetPath) {
            return NextResponse.json({ error: 'Dataset path could not be resolved' }, { status: 400 });
          }

          const filesToUpload: { path: string; content: any }[] = [];
          const uploadedPaths = new Set<string>();

          const normalizeRepoPath = (value: string) => value.replace(/\\/g, '/').replace(/^\/+/, '');

          const addUploadContent = (repoFilePath: string, content: Blob) => {
            const normalizedRepoPath = normalizeRepoPath(repoFilePath);
            if (!normalizedRepoPath || uploadedPaths.has(normalizedRepoPath)) {
              return;
            }
            uploadedPaths.add(normalizedRepoPath);
            filesToUpload.push({ path: normalizedRepoPath, content });
          };

          const addUploadFile = async (absolutePath: string, repoFilePath: string) => {
            const buffer = await readFile(absolutePath);
            const blob = new Blob([buffer]);
            addUploadContent(repoFilePath, blob);
          };

          const walkDirectory = async (basePath: string, repoPrefix: string) => {
            const entries = await readdir(basePath, { withFileTypes: true });
            for (const entry of entries) {
              const entryPath = path.join(basePath, entry.name);
              if (entry.isDirectory()) {
                const nextPrefix = repoPrefix ? `${repoPrefix}/${entry.name}` : entry.name;
                await walkDirectory(entryPath, nextPrefix);
              } else if (entry.isFile()) {
                const repoFilePath = repoPrefix ? `${repoPrefix}/${entry.name}` : entry.name;
                await addUploadFile(entryPath, repoFilePath);
              }
            }
          };

          const processArtifact = async (localPath: string, repoPath: string) => {
            const resolvedPath = path.resolve(localPath);
            let stats;
            try {
              stats = await stat(resolvedPath);
            } catch {
              throw new Error(`Dataset path does not exist: ${localPath}`);
            }

            const normalizedRepoPrefix = repoPath ? normalizeRepoPath(repoPath) : '';

            if (stats.isDirectory()) {
              await walkDirectory(resolvedPath, normalizedRepoPrefix);
            } else if (stats.isFile()) {
              let destination = normalizedRepoPrefix;
              if (!destination || destination.endsWith('/')) {
                destination = `${destination}${path.basename(resolvedPath)}`;
              } else if (!path.posix.extname(destination)) {
                destination = `${destination}/${path.basename(resolvedPath)}`;
              }
              await addUploadFile(resolvedPath, destination);
            } else {
              throw new Error(`Unsupported artifact type for path: ${localPath}`);
            }
          };

          if (hasStructuredArtifacts) {
            for (const artifact of structuredArtifacts) {
              if (!artifact?.localPath || !artifact?.repoPath) {
                continue;
              }
              await processArtifact(artifact.localPath, artifact.repoPath);
            }
          } else {
            const resolvedDatasetPath = path.resolve(datasetPath);
            let datasetStats;
            try {
              datasetStats = await stat(resolvedDatasetPath);
            } catch {
              return NextResponse.json({ error: 'Dataset path does not exist' }, { status: 400 });
            }

            if (!datasetStats.isDirectory()) {
              return NextResponse.json({ error: 'Dataset path must be a directory' }, { status: 400 });
            }

            await walkDirectory(resolvedDatasetPath, '');
          }

          if (manifest) {
            const manifestBlob = new Blob([
              JSON.stringify(manifest, null, 2)
            ], { type: 'application/json' });
            addUploadContent('manifest.json', manifestBlob);
          }

          if (filesToUpload.length === 0) {
            return NextResponse.json({ error: 'No files found to upload for dataset' }, { status: 400 });
          }

          await uploadFiles({
            repo: repoId,
            accessToken: token,
            files: filesToUpload,
          });

          return NextResponse.json({ success: true, repoId });
        } catch (error: any) {
          console.error('Upload error:', error);
          return NextResponse.json({ error: error.message || 'Failed to upload dataset' }, { status: 500 });
        }

      case 'listFiles':
        try {
          if (!datasetPath) {
            return NextResponse.json({ error: 'Dataset path required' }, { status: 400 });
          }

          const files = await readdir(datasetPath, { withFileTypes: true });
          const imageExtensions = ['.jpg', '.jpeg', '.png', '.webp', '.bmp'];
          
          const imageFiles = files
            .filter(file => file.isFile())
            .filter(file => imageExtensions.some(ext => file.name.toLowerCase().endsWith(ext)))
            .map(file => ({
              name: file.name,
              path: path.join(datasetPath, file.name),
            }));

          const captionFiles = files
            .filter(file => file.isFile())
            .filter(file => file.name.endsWith('.txt'))
            .map(file => ({
              name: file.name,
              path: path.join(datasetPath, file.name),
            }));

          return NextResponse.json({ 
            images: imageFiles, 
            captions: captionFiles,
            total: imageFiles.length 
          });
        } catch (error: any) {
          return NextResponse.json({ error: error.message || 'Failed to list files' }, { status: 500 });
        }

      case 'validateDataset':
        try {
          if (!datasetId) {
            return NextResponse.json({ error: 'Dataset ID required' }, { status: 400 });
          }

          // Try to get dataset info to validate it exists and is accessible
          const dataset = await datasetInfo({
            name: datasetId,
            accessToken: token,
          });

          return NextResponse.json({ 
            exists: true, 
            dataset: {
              id: dataset.id,
              author: dataset.author,
              downloads: dataset.downloads,
              likes: dataset.likes,
              private: dataset.private,
            }
          });
        } catch (error: any) {
          if (error.message?.includes('404') || error.message?.includes('not found')) {
            return NextResponse.json({ exists: false }, { status: 200 });
          }
          if (error.message?.includes('401') || error.message?.includes('403')) {
            return NextResponse.json({ error: 'Dataset not accessible with current token' }, { status: 403 });
          }
          return NextResponse.json({ error: error.message || 'Failed to validate dataset' }, { status: 500 });
        }

      default:
        return NextResponse.json({ error: 'Invalid action' }, { status: 400 });
    }
  } catch (error: any) {
    console.error('HF Hub API error:', error);
    return NextResponse.json({ error: error.message || 'Internal server error' }, { status: 500 });
  }
}