File size: 1,332 Bytes
241036e
 
 
 
e52146b
241036e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
879455c
241036e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"use server"

import { ImageAnalysisRequest, ImageAnalysisResponse } from "@/types"

const apiUrl = `${process.env.RENDERING_VIDEOCHAIN_API_URL || ""}`

export async function see({
  prompt,
  imageBase64
}: {
  prompt: string
  imageBase64: string
}): Promise<string> {
  if (!prompt) {
    console.error(`cannot call the API without an image, aborting..`)
    throw new Error(`cannot call the API without an image, aborting..`)
  }

  try {
    const request = {
      prompt,
      image: imageBase64

    } as ImageAnalysisRequest

    console.log(`calling ${apiUrl}/analyze called with: `, {
      prompt: request.prompt,
      image: request.image.slice(0, 20)
    })

    const res = await fetch(`${apiUrl}/analyze`, {
      method: "POST",
      headers: {
        Accept: "application/json",
        "Content-Type": "application/json",
        // Authorization: `Bearer ${videochainApi}`,
      },
      body: JSON.stringify(request),
      cache: 'no-store',
    // we can also use this (see https://vercel.com/blog/vercel-cache-api-nextjs-cache)
    // next: { revalidate: 1 }
    })

    if (res.status !== 200) {
      throw new Error('Failed to fetch data')
    }

    const response = (await res.json()) as ImageAnalysisResponse
    return response.result
  } catch (err) {
    console.error(err)
    return ""
  }
}