File size: 1,194 Bytes
eb29a95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/** @type {import('./$types').RequestHandler} */

import { json, type RequestEvent } from '@sveltejs/kit';
import { env } from '$env/dynamic/private'

export async function POST({ request } : RequestEvent) {
  const generation = await request.json()

  if (!generation?.model?.id) {
    return json({
      error: {
        token: "A model id is required"
      }
    }, { status: 400 })
  }

  if (!generation?.inputs) {
    return json({
      error: {
        token: "An inputs is required"
      }
    }, { status: 400 })
  }

  const response = await fetch(env.SECRET_INFERENCE_API_URL + "/models/" + generation?.model?.id, {
    method: "POST",
    headers: {
      Authorization: `Bearer ${env.SECRET_HF_TOKEN}`,
      'Content-Type': 'application/json',
      ['x-use-cache']: "0"
    },
    body: JSON.stringify(generation),
  })
  .then((res) => res.blob())
  .then((blob) => blob)
  .catch((error) => {
    return {
      error: error.message,
    }
  })

  if ("error" in response) {
    return json({
      error: {
        token: response.error
      }
    }, { status: 400 })
  }

  return new Response(response, {
    headers: {
      'Content-Type': 'image/png',
    },
  })
}