├── LICENSE └── README.md /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Hotpot.ai 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Hotpot.ai API examples 2 | 3 | ## Stable Diffusion API 4 | Hotpot.ai offers a [Stable Diffusion API](https://hotpot.ai/stable-diffusion-api) with three flavors: (1) budget (2) standard and (3) premium. 5 | 6 | ## Art Maker 7 | 8 | ```Javascript 9 | // Set URL to monitor. 10 | const url = 'https://hotpot-temporary-images.s3.us-east-2.amazonaws.com/520.txt'; 11 | pollAndRenderUrl(url); 12 | 13 | function pollAndRenderUrl(url) { 14 | // Set how often to poll @url. If set lower than 70000, your account will incur S3 charges. 15 | const pollDuration = 70000; 16 | 17 | // Create an interval to check @url every @pollDuration milliseconds. 18 | const interval = setInterval(async () => { 19 | const response = await fetch(url, { 20 | method: 'GET', 21 | cache: 'no-cache', 22 | redirect: 'follow' 23 | }); 24 | 25 | // File exists at @url? 26 | if (response.status === 200) { 27 | // If here, file exists so do something with it. 28 | 29 | // Clear @interval and stop polling. 30 | clearInterval(interval); 31 | } 32 | 33 | }, pollDuration); 34 | } 35 | ``` 36 | 37 | ## Background Remover 38 | 39 | The examples below illustrate how to invoke the [Background Remover](https://hotpot.ai/remove-background) API in different languages. 40 | 41 | ### Curl 42 | 43 | ```bash 44 | curl -H 'Authorization: API_KEY_HERE' \ 45 | -F 'image=@/full/path/to/image.jpg' \ 46 | -o '/full/path/to/image-nobg.jpg' \ 47 | https://api.hotpot.ai/remove-background 48 | ``` 49 | 50 | ### Node 51 | 52 | Install the [form-data](https://www.npmjs.com/package/form-data) library first: 53 | 54 | ```bash 55 | yarn add form-data 56 | ``` 57 | 58 | 59 | ```javascript 60 | 'use strict'; 61 | 62 | const fs = require('fs'); 63 | const https = require('https'); 64 | const FormData = require('form-data'); 65 | 66 | const form = new FormData(); 67 | // change to a full file path of the image you want to transform 68 | form.append('image', fs.createReadStream('/full/path/to/image.jpg')); 69 | 70 | const customHeaders = { 71 | 'Authorization': 'API_KEY_HERE' 72 | } 73 | 74 | // setting a correct MIME type and (multipart/form-data) a boundary for the payload 75 | const headers = {...form.getHeaders(), ...customHeaders} 76 | 77 | const options = { 78 | method: 'POST', 79 | hostname: 'api.hotpot.ai', 80 | port: 443, 81 | path: '/remove-background', 82 | headers: headers, 83 | encoding: null, 84 | }; 85 | 86 | const request = https.request(options, response => { 87 | const body = []; 88 | 89 | response.on('data', chunk => { 90 | body.push(Buffer.from(chunk)); 91 | }) 92 | 93 | response.on('end', () => { 94 | // change to a full file path where you want to save the resulting image 95 | fs.writeFileSync('/full/path/to/image-nobg.jpg', Buffer.concat(body), 'binary'); 96 | request.end(); 97 | }) 98 | }); 99 | 100 | request.on('error', error => { 101 | console.error(error); 102 | }); 103 | 104 | form.pipe(request); 105 | ``` 106 | 107 | ### Python 108 | 109 | Install the [requests](https://requests.readthedocs.io/en/master/) library first: 110 | 111 | ```bash 112 | pip3 install requests 113 | ``` 114 | 115 | ```python 116 | import requests 117 | 118 | headers = { 119 | 'Authorization': 'API_KEY_HERE', 120 | } 121 | 122 | # change to a full file path of the image you want to transform 123 | body = { 124 | 'image': open('/full/path/to/image.jpg', 'rb'), 125 | } 126 | 127 | response = requests.post('https://api.hotpot.ai/remove-background', headers=headers, files=body) 128 | 129 | # change to a full file path where you want to save the resulting image 130 | with open('/full/path/to/image-nobg.jpg', 'wb') as file: 131 | file.write(response.content) 132 | 133 | ``` 134 | 135 | ### PHP 136 | 137 | ```php 138 | new CurlFile('/full/path/to/image.jpg') 145 | ]; 146 | 147 | curl_setopt($ch, CURLOPT_URL, 'https://api.hotpot.ai/remove-background'); 148 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 149 | curl_setopt($ch, CURLOPT_POST, 1); 150 | curl_setopt($ch, CURLOPT_POSTFIELDS, $body); 151 | 152 | $headers = array('Authorization: API_KEY_HERE'); 153 | curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 154 | 155 | $response = curl_exec($ch); 156 | 157 | curl_close($ch); 158 | 159 | // change to a full file path where you want to save the resulting image 160 | file_put_contents('/full/path/to/image-nobg.jpg', $response); 161 | 162 | ``` 163 | 164 | ### C# Flurl.Http 165 | 166 | Install the [Flurl.Http](https://flurl.dev/) library first: 167 | 168 | ```bash 169 | dotnet add package Flurl.Http 170 | ``` 171 | 172 | ```csharp 173 | using var memoryStream = new MemoryStream(data); 174 | 175 | var request = await "https://api.hotpot.ai/remove-background" 176 | .WithHeader("Authorization", "API_KEY_HERE") 177 | // change to a full file path of the image you want to transform 178 | .PostMultipartAsync(builder => builder.AddFile("image", "/full/path/to/image.jpg")); 179 | 180 | var response = await request.GetBytesAsync(); 181 | 182 | // change to a full file path where you want to save the resulting image 183 | await File.WriteAllBytesAsync("/full/path/to/image-nobg.jpg", response); 184 | ``` 185 | 186 | ### C# System.Net.Http 187 | 188 | Note: the `Add` function requires three parameters. Otherwise the binary data will be incorrectly sent as a string. 189 | 190 | ```csharp 191 | using System; 192 | using System.IO; 193 | using System.Net.Http; 194 | using System.Threading.Tasks; 195 | 196 | class Program { 197 | public static async Task Main (string[] args) { 198 | var client = new HttpClient(); 199 | client.DefaultRequestHeaders.Add("Authorization", "API KEY HERE"); 200 | 201 | var form = new MultipartFormDataContent(); 202 | var image = new ByteArrayContent(File.ReadAllBytes("bg.jpg")); 203 | form.Add(image, "image", "bg.jpg"); 204 | 205 | var response = await client.PostAsync("https://api.hotpot.ai/remove-background", form); 206 | var result = await response.Content.ReadAsByteArrayAsync(); 207 | 208 | System.IO.File.WriteAllBytes("nobg.jpg", result); 209 | } 210 | } 211 | ``` 212 | --------------------------------------------------------------------------------