File size: 6,719 Bytes
d325ede
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import { Client, handle_file } from '@gradio/client';
import type { ModelStats, QueueStatus, QueueStage } from '../types';

const HF_SPACE_ID = import.meta.env.VITE_HF_SPACE_ID || 'your-hf-username/your-space-name';
const HF_TOKEN = import.meta.env.VITE_HF_TOKEN;

let cachedClientPromise: Promise<Awaited<ReturnType<typeof Client.connect>>> | null = null;

interface Generate3DPbrArgs {
  images: File[];
  onQueueStatus?: (status: QueueStatus) => void;
}

interface Generate3DPbrResult {
  modelUrl: string;
  stats: ModelStats | null;
}

function isRecord(value: unknown): value is Record<string, unknown> {
  return typeof value === 'object' && value !== null;
}

function toNumber(value: unknown): number | null {
  if (typeof value === 'number' && Number.isFinite(value)) {
    return value;
  }

  return null;
}

function toStringValue(value: unknown): string | null {
  if (typeof value === 'string' && value.length > 0) {
    return value;
  }

  return null;
}

function normalizeStage(value: string | null): QueueStage {
  if (!value) {
    return 'unknown';
  }

  const normalized = value.toLowerCase();

  if (normalized.includes('pending') || normalized.includes('queue')) {
    return 'pending';
  }

  if (normalized.includes('process') || normalized.includes('generating') || normalized.includes('running')) {
    return 'generating';
  }

  if (normalized.includes('complete') || normalized.includes('finished') || normalized.includes('done')) {
    return 'complete';
  }

  if (normalized.includes('error') || normalized.includes('failed')) {
    return 'error';
  }

  return 'unknown';
}

function normalizeProgress(progressValue: number | null): number | null {
  if (progressValue === null) {
    return null;
  }

  if (progressValue >= 0 && progressValue <= 1) {
    return Math.round(progressValue * 100);
  }

  if (progressValue > 1 && progressValue <= 100) {
    return Math.round(progressValue);
  }

  return null;
}

function getProgressFromStatus(record: Record<string, unknown>): number | null {
  const directProgress = normalizeProgress(toNumber(record.progress));

  if (directProgress !== null) {
    return directProgress;
  }

  const progressData = record.progress_data;

  if (!Array.isArray(progressData) || progressData.length === 0) {
    return null;
  }

  const lastEntry = progressData[progressData.length - 1];

  if (!isRecord(lastEntry)) {
    return null;
  }

  const asProgress = normalizeProgress(toNumber(lastEntry.progress));
  if (asProgress !== null) {
    return asProgress;
  }

  const index = toNumber(lastEntry.index);
  const length = toNumber(lastEntry.length);

  if (index === null || length === null || length <= 0) {
    return null;
  }

  return Math.max(0, Math.min(100, Math.round((index / length) * 100)));
}

function normalizeQueueStatus(rawStatus: unknown): QueueStatus {
  if (!isRecord(rawStatus)) {
    return {
      stage: 'unknown',
      rank: null,
      queueSize: null,
      etaSeconds: null,
      progress: null,
      message: null,
    };
  }

  const rawStage = toStringValue(rawStatus.stage) ?? toStringValue(rawStatus.status);

  return {
    stage: normalizeStage(rawStage),
    rank: toNumber(rawStatus.rank) ?? toNumber(rawStatus.position),
    queueSize: toNumber(rawStatus.queue_size) ?? toNumber(rawStatus.size),
    etaSeconds: toNumber(rawStatus.eta),
    progress: getProgressFromStatus(rawStatus),
    message: toStringValue(rawStatus.message),
  };
}

function extractOutputData(rawResult: unknown): unknown[] {
  if (isRecord(rawResult) && Array.isArray(rawResult.data)) {
    return rawResult.data;
  }

  if (Array.isArray(rawResult)) {
    return rawResult;
  }

  return [rawResult];
}

function resolveModelUrl(candidate: unknown): string | null {
  if (typeof candidate === 'string' && candidate.length > 0) {
    return candidate;
  }

  if (!isRecord(candidate)) {
    return null;
  }

  const directUrl = toStringValue(candidate.url);

  if (directUrl) {
    return directUrl;
  }

  const nestedData = candidate.data;

  if (isRecord(nestedData)) {
    return toStringValue(nestedData.url);
  }

  return null;
}

function parseStats(candidate: unknown): ModelStats | null {
  if (!isRecord(candidate)) {
    return null;
  }

  const vertices = toNumber(candidate.vertices);
  const faces = toNumber(candidate.faces);
  const fileSizeBytes = toNumber(candidate.file_size_bytes);
  const fileSizeMb = toNumber(candidate.file_size_mb);

  if (vertices === null || faces === null || fileSizeBytes === null) {
    return null;
  }

  const pbrChannelsRaw = candidate.pbr_channels;
  const pbrChannels = Array.isArray(pbrChannelsRaw)
    ? pbrChannelsRaw.filter((item): item is string => typeof item === 'string')
    : undefined;

  const paintPipeline = toStringValue(candidate.paint_pipeline) ?? undefined;

  return {
    vertices,
    faces,
    file_size_bytes: fileSizeBytes,
    file_size_mb: fileSizeMb ?? Number((fileSizeBytes / (1024 * 1024)).toFixed(3)),
    pbr_channels: pbrChannels,
    paint_pipeline: paintPipeline,
  };
}

async function getClient() {
  if (HF_SPACE_ID.includes('your-hf-username/your-space-name')) {
    throw new Error('Set VITE_HF_SPACE_ID to your Hugging Face Space id, for example: yourname/twoth-hunyuan3d');
  }

  if (!cachedClientPromise) {
    cachedClientPromise = HF_TOKEN
      ? Client.connect(HF_SPACE_ID, { hf_token: HF_TOKEN })
      : Client.connect(HF_SPACE_ID);
  }

  return cachedClientPromise;
}

export async function generate3DPBR({ images, onQueueStatus }: Generate3DPbrArgs): Promise<Generate3DPbrResult> {
  if (images.length < 4 || images.length > 6) {
    throw new Error('Please provide 4 to 6 orthographic images.');
  }

  const app = await getClient();
  const payload = {
    images: images.map((file) => handle_file(file)),
  };

  const submission = app.submit('/generate_3d_pbr', payload);
  let finalPayload: unknown[] | null = null;

  for await (const event of submission) {
    if (!isRecord(event)) {
      continue;
    }

    const eventType = toStringValue(event.type);

    if (eventType === 'status' && onQueueStatus) {
      onQueueStatus(normalizeQueueStatus(event));
      continue;
    }

    if (eventType === 'data' && Array.isArray(event.data)) {
      finalPayload = event.data;
    }
  }

  if (!finalPayload || finalPayload.length === 0) {
    throw new Error('The Space completed but returned no payload data.');
  }

  const outputs = extractOutputData(finalPayload);
  const modelUrl = resolveModelUrl(outputs[0] ?? finalPayload);

  if (!modelUrl) {
    throw new Error('The Space did not return a downloadable GLB URL.');
  }

  return {
    modelUrl,
    stats: parseStats(outputs[1] ?? null),
  };
}