Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

docs(ai): complete hugging face third party tool doc

Greg Richardsond4a7cef3f412
diff --git a/apps/docs/pages/guides/ai/hugging-face.mdx b/apps/docs/pages/guides/ai/hugging-face.mdx
index 16819842e6a..f56e87291a8 100644
--- a/apps/docs/pages/guides/ai/hugging-face.mdx
+++ b/apps/docs/pages/guides/ai/hugging-face.mdx
@@ -61,11 +61,11 @@ To ensure you don't experience any unexpected downtime or errors, we recommend c
## Integrate with Supabase
-Supabase aims to provide all the necessary components to make it easy to integrate with third-party tools like Hugging Face. The way you integrate Hugging Face models into your Supabase project depends on the type of task you are using. Below are some common scenarios you will likely encounter:
+Supabase aims to make it as easy as possible to integrate with third-party tools like Hugging Face.
-### Web or Mobile app
+### Edge functions
-When building a web or mobile app, you should never trust the client with secrets or credentials. This means you should never use your Hugging Face access token directly from your frontend. Supabase solves this using [Edge Functions](/docs/guides/functions), server side TypeScript functions that spin up on-demand only as you need them. Since Edge Functions run on a server, you can safely give them access to your Hugging Face access token.
+When building any application, you should never trust the client frontend with secrets or credentials. Here this means you should never use your Hugging Face access token directly from your frontend. Supabase solves this using [Edge Functions](/docs/guides/functions), server side TypeScript functions that spin up on-demand only as you need them. Since Edge Functions run on a server, you can safely give them access to your Hugging Face access token.
<Admonition type="info">
@@ -82,10 +82,75 @@ supabase init
Then create an edge function:
```shell
-supabase functions new image-to-text
+supabase functions new text-to-image
```
-WIP...
+Create a file called `.env.local` to store your Hugging Face access token:
+
+```shell
+HUGGING_FACE_ACCESS_TOKEN=<your-token-here>
+```
+
+Let's modify the edge function to import Hugging Face's inference client and perform a text-to-image request:
+
+```ts
+import { serve } from 'https://deno.land/[email protected]/http/server.ts'
+import { HfInference } from 'https://esm.sh/@huggingface/[email protected]'
+
+const hf = new HfInference(Deno.env.get('HUGGING_FACE_ACCESS_TOKEN'))
+
+serve(async (req) => {
+ const { prompt } = await req.json()
+
+ const image = await hf.textToImage(
+ {
+ inputs: prompt,
+ model: 'stabilityai/stable-diffusion-2',
+ },
+ {
+ use_cache: false,
+ }
+ )
+
+ return new Response(image)
+})
+```
+
+This function creates a new instance of `HfInference` using the `HUGGING_FACE_ACCESS_TOKEN` environment variable.
+
+It expects a POST request that includes a JSON request body. The JSON body should include a parameter called `prompt` that represents the text-to-image prompt that we will pass to Hugging Face's inference API.
+
+Next we call `textToImage()`, passing in the user's prompt along with the model that we would like to use for the image generation. Today Hugging Face recommends `stabilityai/stable-diffusion-2`, but you can change this to any other text-to-image model. You can see a list of which models are supported for each task by navigating to their [models page](https://huggingface.co/models?pipeline_tag=text-to-image) and filtering by task.
+
+We set `use_cache` to `false` so that repeat queries with the same prompt will produce new images. If the task and model you are using is deterministic (will always produce the same result based on the same input), consider setting `use_cache` to `true` for faster responses.
+
+The `image` result returned from the API will be a `Blob`. We can pass the `Blob` directly into a `new Response()` which will automatically set the content type and body of the response from the `image`.
+
+Finally let's serve the edge function locally to test it:
+
+```shell
+supabase functions serve --env-file .env.local --no-verify-jwt
+```
+
+Remember to pass in the `.env.local` file using the `--env-file` parameter so that the edge function can access the `HUGGING_FACE_ACCESS_TOKEN`.
+
+<Admonition type="info">
+
+For demo purposes we set `--no-verify-jwt` to make it easy to test the edge function without passing in a JWT token. In a real application you will need to pass the JWT as a `Bearer` token in the `Authorization` header.
+
+</Admonition>
+
+At this point, you can make an API request to your edge function using your preferred frontend framework (Next.js, React, Expo, etc). We can also test from the terminal using `curl`:
+
+```shell
+curl --output result.jpg --location --request POST 'http://localhost:54321/functions/v1/text-to-image' \
+ --header 'Content-Type: application/json' \
+ --data '{"query":"Llama wearing sunglasses"}'
+```
+
+Your generated image will save to `result.jpg`:
+
+<img src="/docs/img/ai/hugging-face/llama-sunglasses-example.png" width="400" height="400" />
## Resources
diff --git a/apps/docs/public/img/ai/hugging-face/llama-sunglasses-example.png b/apps/docs/public/img/ai/hugging-face/llama-sunglasses-example.png
new file mode 100644
index 00000000000..6fd4ff55638
Binary files /dev/null and b/apps/docs/public/img/ai/hugging-face/llama-sunglasses-example.png differ