How do I use text-to-image huggingface models as an API for my website?

Hey guys, so I am working on a little personal project which necessitates the use of an external AI Image generator for an output. All the public and famous generators I’ve seen (Hotpot.ai, Dreamstudio, e.t.c) are credit-based. This makes it very tough for me to actually test if my idea works without running out of credits, let alone actually host the website and have users generating pics on it. I’ve found this community and am very happy to see that it is so large and open-source. I’ve found some models that I think would be great, such as Openjourney v4 but the only issue is, I’m not actually sure how to use the API on my website as I haven’t seen any detailed documentation. The docs on huggingface about inference API seem to only be about text and computer vision based models. So I was wondering if anyone could point me in the right direction about where I can find the docs for this model or some other text-image models, or information about how to use these services in my project. I would be very grateful, Thank you!

hello @GlitchDreamr

For prototypes and test you can use our Inference API, but it’s rate-limited, and if you’re a Pro user you get higher rate limits.

Here’s is a simple JS example using our huggingface.js Inference library. You can adapt this snipped, run on a localserver or on your page. Note that if you run this on a public website your TOKEN would be exposed, so you might want to run this code on a server or via a proxy.

You might also consider build a Space/Gradio and embed it on your page.

  const HFInference = (
    await import("https://cdn.skypack.dev/@huggingface/inference@2.0.0")
  ).HfInference;
  const TOKEN = "";
  const hf = new HFInference(TOKEN);
  const blob = await hf.textToImage({
    inputs:
      "a woman wearing a poncho oversized puffer jacket, inspired by OffWhite, tumblr, inspired by Yanjun Cheng style, digital art, lofi girl internet meme, trending on dezeen, catalog photo, 3 d render beeple, rhads and lois van baarle, cartoon style illustration, bright pastel colors, a beautiful artwork illustration, retro anime girl <lora:iu_V35:0.5> <lora:epiNoiseoffset_v2:0.5>",
    parameters: {
      negative_prompt: "easynegative",
    },
    model: "prompthero/openjourney-v4",
  });
  const tab = window.open((target = "_blank"));
  tab.location.href = window.URL.createObjectURL(blob);
}
1 Like