enzostvs HF staff commited on
Commit
82cb3c3
1 Parent(s): e2bd146

preview textgen

Browse files
src/lib/components/image-generation/Preview.svelte ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ export let body: Record<string, any>;
3
+ export let res: string;
4
+ </script>
5
+
6
+ <div class="text-white p-6 font-code text-xs !leading-loose">
7
+ <div class="max-w-max relative ">
8
+ <img src={res} alt="" class="w-full max-w-lg object-contain" />
9
+ <div class="absolute bottom-3 left-3 bg-white/20 backdrop-blur-sm px-2 py-0.5 rounded-lg text-slate-900">{body?.inputs}</div>
10
+ </div>
11
+ </div>
src/lib/components/image-generation/Response.svelte CHANGED
@@ -1,9 +1,12 @@
1
  <script lang="ts">
2
  import Icon from '@iconify/svelte';
 
 
 
3
 
4
  import Loading from "$lib/components/Loading.svelte"
5
  import CodePreview from '$lib/components/CodePreview.svelte';
6
- import Preview from '$lib/components/text-generation/Preview.svelte';
7
 
8
  export let loading: boolean;
9
  export let res: any;
@@ -25,9 +28,24 @@
25
  icon: "carbon:code",
26
  },
27
  ];
 
 
28
  </script>
29
 
30
  <div class="lg:h-screen flex flex-col relative">
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  <div class="bg-slate-950 overflow-auto flex-1">
32
  <div class="w-full uppercase text-zinc-400 text-sm font-semibold border-t border-slate-900 flex items-start sticky top-0 bg-slate-950">
33
  {#each TABS as { label, icon }, idx }
 
1
  <script lang="ts">
2
  import Icon from '@iconify/svelte';
3
+ import Highlight, { LineNumbers } from "svelte-highlight";
4
+ import json from "svelte-highlight/languages/json";
5
+ import "svelte-highlight/styles/night-owl.css"
6
 
7
  import Loading from "$lib/components/Loading.svelte"
8
  import CodePreview from '$lib/components/CodePreview.svelte';
9
+ import Preview from '$lib/components/image-generation/Preview.svelte';
10
 
11
  export let loading: boolean;
12
  export let res: any;
 
28
  icon: "carbon:code",
29
  },
30
  ];
31
+
32
+ $: console.log(res)
33
  </script>
34
 
35
  <div class="lg:h-screen flex flex-col relative">
36
+ <!-- <div class="w-full jsonResponse relative">
37
+ <div class="bg-slate-950 w-full uppercase px-5 py-4 text-zinc-400 text-sm font-semibold border-b border-slate-900 flex items-center justify-start gap-2">
38
+ <Icon icon="carbon:json" class="w-5 h-5" />
39
+ Response
40
+ </div>
41
+ <Highlight language={json} {code}>
42
+ </Highlight>
43
+ {#if loading}
44
+ <Loading>
45
+ <p class="text-slate-400 text-lg mt-4">Processing...</p>
46
+ </Loading>
47
+ {/if}
48
+ </div> -->
49
  <div class="bg-slate-950 overflow-auto flex-1">
50
  <div class="w-full uppercase text-zinc-400 text-sm font-semibold border-t border-slate-900 flex items-start sticky top-0 bg-slate-950">
51
  {#each TABS as { label, icon }, idx }
src/routes/api/image-generation/+server.ts CHANGED
@@ -18,7 +18,7 @@ export async function POST({ request }: RequestEvent) {
18
  headers: {
19
  Authorization: `Bearer ${env.SECRET_HF_TOKEN}`,
20
  'Content-Type': 'application/json',
21
- ['x-use-cache']: "0"
22
  },
23
  body: JSON.stringify({
24
  ...body,
@@ -34,13 +34,12 @@ export async function POST({ request }: RequestEvent) {
34
  })
35
 
36
  if ("error" in response) {
37
- error(400, response.error as string)
38
  }
39
-
40
- return {
41
  headers: {
42
  'Content-Type': 'image/png',
43
  },
44
- body: response,
45
- }
46
  }
 
18
  headers: {
19
  Authorization: `Bearer ${env.SECRET_HF_TOKEN}`,
20
  'Content-Type': 'application/json',
21
+ // ['x-use-cache']: "0"
22
  },
23
  body: JSON.stringify({
24
  ...body,
 
34
  })
35
 
36
  if ("error" in response) {
37
+ throw error(400, response.error as string)
38
  }
39
+
40
+ return new Response(response, {
41
  headers: {
42
  'Content-Type': 'image/png',
43
  },
44
+ })
 
45
  }
src/routes/image-generation/+page.svelte CHANGED
@@ -33,13 +33,20 @@
33
  })
34
  const blob = await request?.clone()?.blob()
35
 
36
- const reader = new FileReader()
37
- reader.readAsDataURL(blob)
38
- reader.onloadend = () => {
39
- const base64data = reader.result
40
- data = base64data
 
 
 
 
 
 
 
41
  }
42
-
43
  bodyRequest = form
44
  loading = false
45
  }
 
33
  })
34
  const blob = await request?.clone()?.blob()
35
 
36
+ if (blob) {
37
+ const reader = new FileReader()
38
+ reader.readAsDataURL(blob)
39
+ reader.onloadend = () => {
40
+ const base64data = reader.result
41
+ data = base64data
42
+ }
43
+ }
44
+
45
+ const res = await request.clone().json().catch(() => ({}))
46
+ if (res) {
47
+ data = res
48
  }
49
+
50
  bodyRequest = form
51
  loading = false
52
  }