1- import type { ModelDataMinimal } from "./types.js" ;
1+ import type { InferenceSnippet , ModelDataMinimal } from "./types.js" ;
22import { describe , expect , it } from "vitest" ;
3- import { snippetConversational } from "./python.js" ;
3+ import { getPythonInferenceSnippet } from "./python.js" ;
44
55describe ( "inference API snippets" , ( ) => {
66 it ( "conversational llm" , async ( ) => {
@@ -10,7 +10,7 @@ describe("inference API snippets", () => {
1010 tags : [ "conversational" ] ,
1111 inference : "" ,
1212 } ;
13- const snippet = snippetConversational ( model , "api_token" ) ;
13+ const snippet = getPythonInferenceSnippet ( model , "api_token" ) as InferenceSnippet [ ] ;
1414
1515 expect ( snippet [ 0 ] . content ) . toEqual ( `from huggingface_hub import InferenceClient
1616
@@ -34,14 +34,43 @@ for chunk in stream:
3434 print(chunk.choices[0].delta.content, end="")` ) ;
3535 } ) ;
3636
37+ it ( "conversational llm non-streaming" , async ( ) => {
38+ const model : ModelDataMinimal = {
39+ id : "meta-llama/Llama-3.1-8B-Instruct" ,
40+ pipeline_tag : "text-generation" ,
41+ tags : [ "conversational" ] ,
42+ inference : "" ,
43+ } ;
44+ const snippet = getPythonInferenceSnippet ( model , "api_token" , { streaming : false } ) as InferenceSnippet [ ] ;
45+
46+ expect ( snippet [ 0 ] . content ) . toEqual ( `from huggingface_hub import InferenceClient
47+
48+ client = InferenceClient(api_key="api_token")
49+
50+ messages = [
51+ {
52+ "role": "user",
53+ "content": "What is the capital of France?"
54+ }
55+ ]
56+
57+ completion = client.chat.completions.create(
58+ model="meta-llama/Llama-3.1-8B-Instruct",
59+ messages=messages,
60+ max_tokens=500
61+ )
62+
63+ print(completion.choices[0].message)` ) ;
64+ } ) ;
65+
3766 it ( "conversational vlm" , async ( ) => {
3867 const model : ModelDataMinimal = {
3968 id : "meta-llama/Llama-3.2-11B-Vision-Instruct" ,
4069 pipeline_tag : "image-text-to-text" ,
4170 tags : [ "conversational" ] ,
4271 inference : "" ,
4372 } ;
44- const snippet = snippetConversational ( model , "api_token" ) ;
73+ const snippet = getPythonInferenceSnippet ( model , "api_token" ) as InferenceSnippet [ ] ;
4574
4675 expect ( snippet [ 0 ] . content ) . toEqual ( `from huggingface_hub import InferenceClient
4776
@@ -75,4 +104,41 @@ stream = client.chat.completions.create(
75104for chunk in stream:
76105 print(chunk.choices[0].delta.content, end="")` ) ;
77106 } ) ;
107+
108+ it ( "text-to-image" , async ( ) => {
109+ const model : ModelDataMinimal = {
110+ id : "black-forest-labs/FLUX.1-schnell" ,
111+ pipeline_tag : "text-to-image" ,
112+ tags : [ ] ,
113+ inference : "" ,
114+ } ;
115+ const snippets = getPythonInferenceSnippet ( model , "api_token" ) as InferenceSnippet [ ] ;
116+
117+ expect ( snippets . length ) . toEqual ( 2 ) ;
118+
119+ expect ( snippets [ 0 ] . client ) . toEqual ( "huggingface_hub" ) ;
120+ expect ( snippets [ 0 ] . content ) . toEqual ( `from huggingface_hub import InferenceClient
121+ client = InferenceClient("black-forest-labs/FLUX.1-schnell", token="api_token")
122+
123+ # output is a PIL.Image object
124+ image = client.text_to_image("Astronaut riding a horse")` ) ;
125+
126+ expect ( snippets [ 1 ] . client ) . toEqual ( "requests" ) ;
127+ expect ( snippets [ 1 ] . content ) . toEqual ( `import requests
128+
129+ API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-schnell"
130+ headers = {"Authorization": "Bearer api_token"}
131+
132+ def query(payload):
133+ response = requests.post(API_URL, headers=headers, json=payload)
134+ return response.content
135+ image_bytes = query({
136+ "inputs": "Astronaut riding a horse",
137+ })
138+
139+ # You can access the image with PIL.Image for example
140+ import io
141+ from PIL import Image
142+ image = Image.open(io.BytesIO(image_bytes))` ) ;
143+ } ) ;
78144} ) ;
0 commit comments