mishig HF staff commited on
Commit
79df262
1 Parent(s): 498cea5

wip: upload files

Browse files
Files changed (1) hide show
  1. src/routes/+page.svelte +36 -9
src/routes/+page.svelte CHANGED
@@ -19,6 +19,7 @@
19
  let sketchEl: HTMLCanvasElement;
20
  let isShowSketch = false;
21
  let outputImgs: CanvasImageSource[] = [];
 
22
 
23
  const animImageDuration = 500 as const;
24
  const animNoiseDuration = 3000 as const;
@@ -96,11 +97,11 @@
96
  noiseTs = performance.now();
97
  drawNoise();
98
 
99
- const { imgFile, imgBitmap: initialSketchBitmap } = await getCanvasSnapshot(canvas);
100
  const form = new FormData();
101
  form.append('prompt', promptTxt);
102
  form.append('strength', '0.85');
103
- form.append('image', imgFile);
104
 
105
  try {
106
  const response = await fetch('https://sdb.pcuenca.net/i2i', {
@@ -131,6 +132,20 @@
131
  )) as CanvasImageSource[];
132
  outputImgs.push(initialSketchBitmap);
133
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134
  isShowSketch = true;
135
  let i = 0;
136
  imageTs = performance.now();
@@ -327,9 +342,6 @@
327
  }
328
 
329
  async function uploadFile(file: File): Promise<string> {
330
- // const delay = ms => new Promise(res => setTimeout(res, ms));
331
- // await delay(5000);
332
- // return "abc.zyx";
333
  const UPLOAD_URL = "https://huggingface.co/uploads";
334
  const response = await fetch(UPLOAD_URL, {
335
  method: "POST",
@@ -345,12 +357,27 @@
345
 
346
  async function createCommunityPost() {
347
  isUploading = true;
348
- const { imgFile } = await getCanvasSnapshot(canvas);
349
- const url = await uploadFile(imgFile);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
350
 
351
  const params = new URLSearchParams({
352
  title: promptTxt,
353
- description: `![diffuse-the-rest.png](${url})`,
354
  });
355
 
356
  const paramsStr = params.toString();
@@ -417,7 +444,7 @@
417
  {#if canvas}
418
  <div>
419
  <div class="w-full flex justify-end">
420
- <!-- <ShareWithCommunity on:createCommunityPost={createCommunityPost} {isUploading}/> -->
421
  </div>
422
  <div class="flex gap-x-2 mt-3 items-start justify-center {isLoading ? 'animate-pulse' : ''}">
423
  <span
 
19
  let sketchEl: HTMLCanvasElement;
20
  let isShowSketch = false;
21
  let outputImgs: CanvasImageSource[] = [];
22
+ let outputFiles: {sketch: File, generations: File[]};
23
 
24
  const animImageDuration = 500 as const;
25
  const animNoiseDuration = 3000 as const;
 
97
  noiseTs = performance.now();
98
  drawNoise();
99
 
100
+ const { imgFile: sketch, imgBitmap: initialSketchBitmap } = await getCanvasSnapshot(canvas);
101
  const form = new FormData();
102
  form.append('prompt', promptTxt);
103
  form.append('strength', '0.85');
104
+ form.append('image', sketch);
105
 
106
  try {
107
  const response = await fetch('https://sdb.pcuenca.net/i2i', {
 
132
  )) as CanvasImageSource[];
133
  outputImgs.push(initialSketchBitmap);
134
 
135
+ outputFiles = {
136
+ sketch,
137
+ generations: (await Promise.all(
138
+ imagesBase64Strs.map(async (imgBase64Str) => {
139
+ const dataUrl = `data:image/png;base64, ${imgBase64Str}`;
140
+ const res: Response = await fetch(dataUrl);
141
+ const blob: Blob = await res.blob();
142
+ const imgId = Date.now() % 200;
143
+ const fileName = `diffuse-the-rest-${imgId}.png`;
144
+ return new File([blob], fileName, { type: 'image/png' });
145
+ })
146
+ )) as File[]
147
+ };
148
+
149
  isShowSketch = true;
150
  let i = 0;
151
  imageTs = performance.now();
 
342
  }
343
 
344
  async function uploadFile(file: File): Promise<string> {
 
 
 
345
  const UPLOAD_URL = "https://huggingface.co/uploads";
346
  const response = await fetch(UPLOAD_URL, {
347
  method: "POST",
 
357
 
358
  async function createCommunityPost() {
359
  isUploading = true;
360
+ // was there successful generation dawg
361
+
362
+ const files = [outputFiles.sketch, ...outputFiles.generations];
363
+ const urls = await Promise.all(files.map((f) => uploadFile(f)));
364
+ const htmlImgs = urls.map(url => `<img src="${url}" width="400" height="400">`);
365
+ const descriptionMd = `#### Prompt:
366
+ ${promptTxt}
367
+
368
+ #### Sketch:
369
+ <div style="display: flex; overflow: scroll; column-gap: 0.75rem;">
370
+ ${htmlImgs[0]}
371
+ </div>
372
+
373
+ #### Generations:
374
+ <div style="display: flex; flex-wrap: wrap; column-gap: 0.75rem;">
375
+ ${htmlImgs.slice(1).join("\n")}
376
+ </div>`;
377
 
378
  const params = new URLSearchParams({
379
  title: promptTxt,
380
+ description: descriptionMd,
381
  });
382
 
383
  const paramsStr = params.toString();
 
444
  {#if canvas}
445
  <div>
446
  <div class="w-full flex justify-end">
447
+ <ShareWithCommunity on:createCommunityPost={createCommunityPost} {isUploading}/>
448
  </div>
449
  <div class="flex gap-x-2 mt-3 items-start justify-center {isLoading ? 'animate-pulse' : ''}">
450
  <span