From 59cb6aa6680c05901c7dc9515e770622f4a21020 Mon Sep 17 00:00:00 2001 From: Greg Richardson Date: Thu, 1 Jun 2023 13:10:10 -0600 Subject: [PATCH 1/4] docs(ai): link to hf edge function docs --- .../MDX/ai/quickstart_hf_deployment.mdx | 5 +++ apps/docs/pages/guides/ai/hugging-face.mdx | 44 ++++++++++--------- .../guides/ai/quickstarts/face-similarity.mdx | 1 - .../guides/ai/quickstarts/hello-world.mdx | 1 - .../ai/quickstarts/text-deduplication.mdx | 4 +- 5 files changed, 32 insertions(+), 23 deletions(-) create mode 100644 apps/docs/components/MDX/ai/quickstart_hf_deployment.mdx diff --git a/apps/docs/components/MDX/ai/quickstart_hf_deployment.mdx b/apps/docs/components/MDX/ai/quickstart_hf_deployment.mdx new file mode 100644 index 00000000000..e0265c4df6d --- /dev/null +++ b/apps/docs/components/MDX/ai/quickstart_hf_deployment.mdx @@ -0,0 +1,5 @@ +## Deployment + +If you have your own infrastructure for deploying Python apps, you can continue to use `vecs` as described in this guide. + +Alternatively if you would like to quickly deployment using Supabase, check out our guide on using the [Hugging Face Inference API](/docs/guides/ai/hugging-face) in Edge Functions using TypeScript. diff --git a/apps/docs/pages/guides/ai/hugging-face.mdx b/apps/docs/pages/guides/ai/hugging-face.mdx index b87c724a55b..8886a741b92 100644 --- a/apps/docs/pages/guides/ai/hugging-face.mdx +++ b/apps/docs/pages/guides/ai/hugging-face.mdx @@ -9,6 +9,8 @@ export const meta = { [Hugging Face](https://huggingface.co) is an open source hub for AI/ML models and tools. With over 100,000 machine learning models available, Hugging Face provides a great way to integrate specialized AI & ML tasks into your application. +Hugging Face exposes and [Inference API](https://huggingface.co/inference-api) you can use to execute AI tasks remotely on Hugging Face servers. This opens the doors to using Hugging Face with languages like TypeScript and can be deployed using [Edge Functions](/docs/guides/functions). + ## AI Tasks Below are some of the types of tasks you can perform with Hugging Face: @@ -40,7 +42,7 @@ See a [full list of tasks](https://huggingface.co/tasks). ## Access token -You should first generate a Hugging Face access token for your app: +First generate a Hugging Face access token for your app: https://huggingface.co/settings/tokens @@ -59,13 +61,9 @@ To ensure you don't experience any unexpected downtime or errors, we recommend c -## Integrate with Supabase +## Edge Functions -Supabase aims to make it as easy as possible to integrate with third-party tools like Hugging Face. - -### Edge functions - -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. +Edge Functions are 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. @@ -73,13 +71,13 @@ You will need the `supabase` CLI [installed](/docs/guides/cli) for the following -To create a new edge function, navigate to your local project and initialize Supabase if you haven't already: +To create a new Edge Function, navigate to your local project and initialize Supabase if you haven't already: ```shell supabase init ``` -Then create an edge function: +Then create an Edge Function: ```shell supabase functions new text-to-image @@ -91,7 +89,7 @@ Create a file called `.env.local` to store your Hugging Face access token: HUGGING_FACE_ACCESS_TOKEN= ``` -Let's modify the edge function to import Hugging Face's inference client and perform a text-to-image request: +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/std@0.168.0/http/server.ts' @@ -116,31 +114,31 @@ serve(async (req) => { }) ``` -This function creates a new instance of `HfInference` using the `HUGGING_FACE_ACCESS_TOKEN` environment variable. +1. 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. +1. 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. +1. 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. +1. 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`. +1. 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: +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`. +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`. -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. +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. -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`: +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' \ @@ -148,7 +146,7 @@ curl --output result.jpg --location --request POST 'http://localhost:54321/funct --data '{"query":"Llama wearing sunglasses"}' ``` -Your generated image will save to `result.jpg`: +In this example, your generated image will save to `result.jpg`: +## Next steps + +You can now create an Edge Function that invokes a Hugging Face task using your model of choice. + +Try running some other [AI tasks](#ai-tasks). + ## Resources - Official [Hugging Face site](https://huggingface.co/). diff --git a/apps/docs/pages/guides/ai/quickstarts/face-similarity.mdx b/apps/docs/pages/guides/ai/quickstarts/face-similarity.mdx index e32dc1a7f21..9bc0be7c9c2 100644 --- a/apps/docs/pages/guides/ai/quickstarts/face-similarity.mdx +++ b/apps/docs/pages/guides/ai/quickstarts/face-similarity.mdx @@ -1,5 +1,4 @@ import Layout from '~/layouts/DefaultGuideLayout' -import StepHikeCompact from '~/components/StepHikeCompact' export const meta = { id: 'ai-vecs-python-client', diff --git a/apps/docs/pages/guides/ai/quickstarts/hello-world.mdx b/apps/docs/pages/guides/ai/quickstarts/hello-world.mdx index 61c2d3fd448..7b678dc1d20 100644 --- a/apps/docs/pages/guides/ai/quickstarts/hello-world.mdx +++ b/apps/docs/pages/guides/ai/quickstarts/hello-world.mdx @@ -1,5 +1,4 @@ import Layout from '~/layouts/DefaultGuideLayout' -import StepHikeCompact from '~/components/StepHikeCompact' export const meta = { id: 'ai-vecs-python-client', diff --git a/apps/docs/pages/guides/ai/quickstarts/text-deduplication.mdx b/apps/docs/pages/guides/ai/quickstarts/text-deduplication.mdx index 1a417bc6719..8d20cf9cca4 100644 --- a/apps/docs/pages/guides/ai/quickstarts/text-deduplication.mdx +++ b/apps/docs/pages/guides/ai/quickstarts/text-deduplication.mdx @@ -1,5 +1,5 @@ import Layout from '~/layouts/DefaultGuideLayout' -import StepHikeCompact from '~/components/StepHikeCompact' +import HuggingFaceDeployment from '~/components/MDX/ai/quickstart_hf_deployment.mdx' export const meta = { id: 'ai-vecs-python-client', @@ -54,6 +54,8 @@ You can view the inserted items in the [Table Editor](https://app.supabase.com/p ![Colab documents](/docs/img/ai/google-colab/colab-documents.png) + + ## Next steps You can now start building your own applications with Vecs. Check our [examples](/docs/guides/ai#examples) for ideas. From b76e3333c7c336b14bbe00c61a8469e992d28229 Mon Sep 17 00:00:00 2001 From: Greg Richardson Date: Thu, 1 Jun 2023 13:15:04 -0600 Subject: [PATCH 2/4] docs(ai): hugging face doc wording Co-authored-by: Copple <10214025+kiwicopple@users.noreply.github.com> --- apps/docs/pages/guides/ai/hugging-face.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/docs/pages/guides/ai/hugging-face.mdx b/apps/docs/pages/guides/ai/hugging-face.mdx index 8886a741b92..8fbb027a22d 100644 --- a/apps/docs/pages/guides/ai/hugging-face.mdx +++ b/apps/docs/pages/guides/ai/hugging-face.mdx @@ -63,7 +63,7 @@ To ensure you don't experience any unexpected downtime or errors, we recommend c ## Edge Functions -Edge Functions are 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. +Edge Functions are server-side TypeScript functions that run on-demand. Since Edge Functions run on a server, you can safely give them access to your Hugging Face access token. From b144be588b262559f4bc4e0d197ddaff78105975 Mon Sep 17 00:00:00 2001 From: Greg Richardson Date: Thu, 1 Jun 2023 13:16:02 -0600 Subject: [PATCH 3/4] docs(ai): fix typo --- apps/docs/pages/guides/ai/hugging-face.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/docs/pages/guides/ai/hugging-face.mdx b/apps/docs/pages/guides/ai/hugging-face.mdx index 8fbb027a22d..c198e7b2349 100644 --- a/apps/docs/pages/guides/ai/hugging-face.mdx +++ b/apps/docs/pages/guides/ai/hugging-face.mdx @@ -9,7 +9,7 @@ export const meta = { [Hugging Face](https://huggingface.co) is an open source hub for AI/ML models and tools. With over 100,000 machine learning models available, Hugging Face provides a great way to integrate specialized AI & ML tasks into your application. -Hugging Face exposes and [Inference API](https://huggingface.co/inference-api) you can use to execute AI tasks remotely on Hugging Face servers. This opens the doors to using Hugging Face with languages like TypeScript and can be deployed using [Edge Functions](/docs/guides/functions). +Hugging Face exposes an [Inference API](https://huggingface.co/inference-api) you can use to execute AI tasks remotely on Hugging Face servers. This opens the doors to using Hugging Face with languages like TypeScript and can be deployed using [Edge Functions](/docs/guides/functions). ## AI Tasks From 9d0f18111935edf43a113d9bad1d45fa8abcf4de Mon Sep 17 00:00:00 2001 From: Greg Richardson Date: Thu, 1 Jun 2023 13:17:49 -0600 Subject: [PATCH 4/4] docs(ai): fix typo --- apps/docs/components/MDX/ai/quickstart_hf_deployment.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/docs/components/MDX/ai/quickstart_hf_deployment.mdx b/apps/docs/components/MDX/ai/quickstart_hf_deployment.mdx index e0265c4df6d..595b1f8f25b 100644 --- a/apps/docs/components/MDX/ai/quickstart_hf_deployment.mdx +++ b/apps/docs/components/MDX/ai/quickstart_hf_deployment.mdx @@ -2,4 +2,4 @@ If you have your own infrastructure for deploying Python apps, you can continue to use `vecs` as described in this guide. -Alternatively if you would like to quickly deployment using Supabase, check out our guide on using the [Hugging Face Inference API](/docs/guides/ai/hugging-face) in Edge Functions using TypeScript. +Alternatively if you would like to quickly deploy using Supabase, check out our guide on using the [Hugging Face Inference API](/docs/guides/ai/hugging-face) in Edge Functions using TypeScript.