Skip to content

Latest commit

History

21 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Hotpot.ai API examples

Stable Diffusion API

Hotpot.ai offers a Stable Diffusion API with three flavors: (1) budget (2) standard and (3) premium.

Art Maker

// Set URL to monitor.consturl='https://hotpot-temporary-images.s3.us-east-2.amazonaws.com/520.txt';pollAndRenderUrl(url);functionpollAndRenderUrl(url){// Set how often to poll @url. If set lower than 70000, your account will incur S3 charges.constpollDuration=70000;// Create an interval to check @url every @pollDuration milliseconds.constinterval=setInterval(async()=>{constresponse=awaitfetch(url,{method: 'GET',cache: 'no-cache',redirect: 'follow'});// File exists at @url?if(response.status===200){// If here, file exists so do something with it.// Clear @interval and stop polling.clearInterval(interval);}},pollDuration);}

Background Remover

The examples below illustrate how to invoke the Background Remover API in different languages.

Curl

curl -H 'Authorization: API_KEY_HERE' \
-F 'image=@/full/path/to/image.jpg' \
-o '/full/path/to/image-nobg.jpg' \
https://api.hotpot.ai/remove-background

Node

Install the form-data library first:

yarn add form-data
'use strict';constfs=require('fs');consthttps=require('https');constFormData=require('form-data');constform=newFormData();// change to a full file path of the image you want to transformform.append('image',fs.createReadStream('/full/path/to/image.jpg'));constcustomHeaders={'Authorization': 'API_KEY_HERE'}// setting a correct MIME type and (multipart/form-data) a boundary for the payloadconstheaders={...form.getHeaders(), ...customHeaders}constoptions={method: 'POST',hostname: 'api.hotpot.ai',port: 443,path: '/remove-background',headers: headers,encoding: null,};constrequest=https.request(options,response=>{constbody=[];response.on('data',chunk=>{body.push(Buffer.from(chunk));})response.on('end',()=>{// change to a full file path where you want to save the resulting imagefs.writeFileSync('/full/path/to/image-nobg.jpg',Buffer.concat(body),'binary');request.end();})});request.on('error',error=>{console.error(error);});form.pipe(request);

Python

Install the requests library first:

pip3 install requests
importrequestsheaders= {
'Authorization': 'API_KEY_HERE',
}
# change to a full file path of the image you want to transformbody= {
'image': open('/full/path/to/image.jpg', 'rb'),
}
response=requests.post('https://api.hotpot.ai/remove-background', headers=headers, files=body)
# change to a full file path where you want to save the resulting imagewithopen('/full/path/to/image-nobg.jpg', 'wb') asfile:
file.write(response.content)

PHP

<?php$ch = curl_init();
// change to a full file path of the image you want to transform$body = [
'image' => newCurlFile('/full/path/to/image.jpg')
];
curl_setopt($ch, CURLOPT_URL, 'https://api.hotpot.ai/remove-background');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
$headers = array('Authorization: API_KEY_HERE');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
curl_close($ch);
// change to a full file path where you want to save the resulting imagefile_put_contents('/full/path/to/image-nobg.jpg', $response);

C# Flurl.Http

Install the Flurl.Http library first:

dotnet add package Flurl.Http
usingvarmemoryStream=newMemoryStream(data);varrequest=await"https://api.hotpot.ai/remove-background".WithHeader("Authorization","API_KEY_HERE")// change to a full file path of the image you want to transform.PostMultipartAsync(builder =>builder.AddFile("image","/full/path/to/image.jpg"));varresponse=awaitrequest.GetBytesAsync();// change to a full file path where you want to save the resulting imageawaitFile.WriteAllBytesAsync("/full/path/to/image-nobg.jpg",response);

C# System.Net.Http

Note: the Add function requires three parameters. Otherwise the binary data will be incorrectly sent as a string.

usingSystem;usingSystem.IO;usingSystem.Net.Http;usingSystem.Threading.Tasks;classProgram{publicstaticasyncTaskMain(string[]args){varclient=newHttpClient();client.DefaultRequestHeaders.Add("Authorization","API KEY HERE");varform=newMultipartFormDataContent();varimage=newByteArrayContent(File.ReadAllBytes("bg.jpg"));form.Add(image,"image","bg.jpg");varresponse=awaitclient.PostAsync("https://api.hotpot.ai/remove-background",form);varresult=awaitresponse.Content.ReadAsByteArrayAsync();System.IO.File.WriteAllBytes("nobg.jpg",result);}}

About

This repository illustrates how to use the Hotpot.ai API. Our API provides Stable Diffusion, image generator, text-to-image generator, background removal, image upscaler, photo restoration, and picture colorization.

Topics

Resources

Stars

55 stars

Watchers

2 watching

Forks

Releases

Packages

Contributors