File size: 9,212 Bytes
4aed899
2dc30e3
4aed899
 
2dc30e3
 
 
4aed899
 
 
2dc30e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a54a361
 
2dc30e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a54a361
 
2dc30e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4aed899
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<!doctype html>
<html lang="en">
	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1" />
		<title>Replicate Inference Provider Examples</title>
		<meta name="description" content="Copy-paste examples for running image, video, speech, and audio models on Replicate through Hugging Face Inference Providers." />
		<link rel="stylesheet" href="style.css" />
	</head>
	<body>
		<main>
			<header>
				<h1>Run models on Replicate from Hugging Face</h1>
				<p class="lede">
					Use Replicate as an Inference Provider with the standard Hugging Face clients. Just your
					<code>HF_TOKEN</code> and <code>provider="replicate"</code>, no separate integration needed.
				</p>
				<nav class="links">
					<a href="https://huggingface.co/docs/inference-providers/providers/replicate" target="_blank" rel="noopener">Provider docs</a>
					<a href="https://huggingface.co/collections/replicate/run-with-replicate-6a04d0792d027edbf66c7155" target="_blank" rel="noopener">Featured models</a>
					<a href="https://huggingface.co/models?inference_provider=replicate&amp;sort=trending" target="_blank" rel="noopener">All Replicate-powered models</a>
					<a href="https://replicate.com" target="_blank" rel="noopener">replicate.com</a>
				</nav>
			</header>

			<section>
				<h2>Setup</h2>
				<div class="tabs" data-group="setup">
					<button class="tab active" data-lang="python">Python</button>
					<button class="tab" data-lang="js">JavaScript</button>
				</div>
				<pre data-group="setup" data-lang="python"><code>pip install -U huggingface_hub pillow
export HF_TOKEN=hf_...</code></pre>
				<pre data-group="setup" data-lang="js" hidden><code>npm install @huggingface/inference
export HF_TOKEN=hf_...</code></pre>
				<p class="note">
					Create a token at <a href="https://huggingface.co/settings/tokens" target="_blank" rel="noopener">huggingface.co/settings/tokens</a>
					with the "Make calls to Inference Providers" permission.
				</p>
			</section>

			<section>
				<h2>Text to image</h2>
				<p class="model">Model: <a href="https://huggingface.co/Tongyi-MAI/Z-Image-Turbo" target="_blank" rel="noopener">Tongyi-MAI/Z-Image-Turbo</a>. Also try <code>black-forest-labs/FLUX.1-dev</code> or <code>Qwen/Qwen-Image</code>.</p>
				<div class="tabs" data-group="t2i">
					<button class="tab active" data-lang="python">Python</button>
					<button class="tab" data-lang="js">JavaScript</button>
				</div>
				<pre data-group="t2i" data-lang="python"><code>import os
from huggingface_hub import InferenceClient

client = InferenceClient(provider="replicate", api_key=os.environ["HF_TOKEN"])

image = client.text_to_image(
    "A cinematic photo of an astronaut riding a horse",
    model="Tongyi-MAI/Z-Image-Turbo",
)
image.save("astronaut.png")</code></pre>
				<pre data-group="t2i" data-lang="js" hidden><code>import { InferenceClient } from "@huggingface/inference";
import { writeFile } from "node:fs/promises";

const client = new InferenceClient(process.env.HF_TOKEN);

const image = await client.textToImage({
  provider: "replicate",
  model: "Tongyi-MAI/Z-Image-Turbo",
  inputs: "A cinematic photo of an astronaut riding a horse",
});
const ext = image.type.split("/")[1];
await writeFile(`astronaut.${ext}`, Buffer.from(await image.arrayBuffer()));</code></pre>
			</section>

			<section>
				<h2>Image editing (image to image)</h2>
				<p class="model">Model: <a href="https://huggingface.co/black-forest-labs/FLUX.1-Kontext-dev" target="_blank" rel="noopener">black-forest-labs/FLUX.1-Kontext-dev</a>. Also try <code>Qwen/Qwen-Image-Edit</code> or <code>black-forest-labs/FLUX.2-dev</code>.</p>
				<div class="tabs" data-group="i2i">
					<button class="tab active" data-lang="python">Python</button>
					<button class="tab" data-lang="js">JavaScript</button>
				</div>
				<pre data-group="i2i" data-lang="python"><code>import os
from huggingface_hub import InferenceClient

client = InferenceClient(provider="replicate", api_key=os.environ["HF_TOKEN"])

image = client.image_to_image(
    "cat.png",
    prompt="Turn the cat into a tiger",
    model="black-forest-labs/FLUX.1-Kontext-dev",
)
image.save("tiger.png")</code></pre>
				<pre data-group="i2i" data-lang="js" hidden><code>import { InferenceClient } from "@huggingface/inference";
import { readFile, writeFile } from "node:fs/promises";

const client = new InferenceClient(process.env.HF_TOKEN);

const image = await client.imageToImage({
  provider: "replicate",
  model: "black-forest-labs/FLUX.1-Kontext-dev",
  inputs: new Blob([await readFile("cat.png")]),
  parameters: { prompt: "Turn the cat into a tiger" },
});
const ext = image.type.split("/")[1];
await writeFile(`tiger.${ext}`, Buffer.from(await image.arrayBuffer()));</code></pre>
			</section>

			<section>
				<h2>Text to video</h2>
				<p class="model">Model: <a href="https://huggingface.co/Wan-AI/Wan2.2-T2V-A14B-Diffusers" target="_blank" rel="noopener">Wan-AI/Wan2.2-T2V-A14B-Diffusers</a></p>
				<div class="tabs" data-group="t2v">
					<button class="tab active" data-lang="python">Python</button>
					<button class="tab" data-lang="js">JavaScript</button>
				</div>
				<pre data-group="t2v" data-lang="python"><code>import os
from huggingface_hub import InferenceClient

client = InferenceClient(provider="replicate", api_key=os.environ["HF_TOKEN"])

video = client.text_to_video(
    "A young man walking on the street at sunset",
    model="Wan-AI/Wan2.2-T2V-A14B-Diffusers",
)
with open("video.mp4", "wb") as f:
    f.write(video)</code></pre>
				<pre data-group="t2v" data-lang="js" hidden><code>import { InferenceClient } from "@huggingface/inference";
import { writeFile } from "node:fs/promises";

const client = new InferenceClient(process.env.HF_TOKEN);

const video = await client.textToVideo({
  provider: "replicate",
  model: "Wan-AI/Wan2.2-T2V-A14B-Diffusers",
  inputs: "A young man walking on the street at sunset",
});
await writeFile("video.mp4", Buffer.from(await video.arrayBuffer()));</code></pre>
			</section>

			<section>
				<h2>Speech recognition</h2>
				<p class="model">Model: <a href="https://huggingface.co/openai/whisper-large-v3" target="_blank" rel="noopener">openai/whisper-large-v3</a></p>
				<div class="tabs" data-group="asr">
					<button class="tab active" data-lang="python">Python</button>
					<button class="tab" data-lang="js">JavaScript</button>
				</div>
				<pre data-group="asr" data-lang="python"><code>import os
from huggingface_hub import InferenceClient

client = InferenceClient(provider="replicate", api_key=os.environ["HF_TOKEN"])

result = client.automatic_speech_recognition(
    "sample.flac",
    model="openai/whisper-large-v3",
)
print(result.text)</code></pre>
				<pre data-group="asr" data-lang="js" hidden><code>import { InferenceClient } from "@huggingface/inference";
import { readFile } from "node:fs/promises";

const client = new InferenceClient(process.env.HF_TOKEN);

const result = await client.automaticSpeechRecognition({
  provider: "replicate",
  model: "openai/whisper-large-v3",
  data: new Blob([await readFile("sample.flac")]),
});
console.log(result.text);</code></pre>
			</section>

			<section>
				<h2>Text to speech</h2>
				<p class="model">Model: <a href="https://huggingface.co/ResembleAI/chatterbox" target="_blank" rel="noopener">ResembleAI/chatterbox</a></p>
				<div class="tabs" data-group="tts">
					<button class="tab active" data-lang="python">Python</button>
					<button class="tab" data-lang="js">JavaScript</button>
				</div>
				<pre data-group="tts" data-lang="python"><code>import os
from huggingface_hub import InferenceClient

client = InferenceClient(provider="replicate", api_key=os.environ["HF_TOKEN"])

audio = client.text_to_speech(
    "Hello from Replicate on Hugging Face!",
    model="ResembleAI/chatterbox",
)
with open("speech.wav", "wb") as f:
    f.write(audio)</code></pre>
				<pre data-group="tts" data-lang="js" hidden><code>import { InferenceClient } from "@huggingface/inference";
import { writeFile } from "node:fs/promises";

const client = new InferenceClient(process.env.HF_TOKEN);

const audio = await client.textToSpeech({
  provider: "replicate",
  model: "ResembleAI/chatterbox",
  inputs: "Hello from Replicate on Hugging Face!",
});
await writeFile("speech.wav", Buffer.from(await audio.arrayBuffer()));</code></pre>
			</section>

			<footer>
				<p>
					Need cURL or more tasks? See the
					<a href="https://huggingface.co/docs/inference-providers/providers/replicate" target="_blank" rel="noopener">Replicate provider docs</a>.
					Want the full Replicate API? Visit <a href="https://replicate.com/docs" target="_blank" rel="noopener">replicate.com/docs</a>.
				</p>
			</footer>
		</main>

		<script>
			document.querySelectorAll(".tab").forEach(function (btn) {
				btn.addEventListener("click", function () {
					var lang = btn.dataset.lang;
					document.querySelectorAll(".tab").forEach(function (t) {
						t.classList.toggle("active", t.dataset.lang === lang);
					});
					document.querySelectorAll("pre[data-lang]").forEach(function (p) {
						p.hidden = p.dataset.lang !== lang;
					});
				});
			});
		</script>
	</body>
</html>