This guide will walk you through integrating Hugging Face with APIpie, enabling you to use thousands of open-source AI models for various tasks through a unified interface.
Hugging Face is a leading platform for the AI community, providing:
By connecting Hugging Face with APIpie, you can access a wide range of models through a consistent API, while benefiting from APIpie's additional features like model routing and failover.
For Python:
pip install huggingface_hub openai
For JavaScript/TypeScript:
npm install @huggingface/inference openai
# or
yarn add @huggingface/inference openai
# or
pnpm add @huggingface/inference openai
There are two main ways to integrate Hugging Face with APIpie:
from huggingface_hub import InferenceClient
# Initialize the client with APIpie as the provider
client = InferenceClient(
provider="openai",
api_key="your-apipie-api-key",
base_url="https://apipie.ai/v1",
)
import os
import openai
# Configure OpenAI to use APIpie
os.environ["OPENAI_API_KEY"] = "your-apipie-api-key"
os.environ["OPENAI_API_BASE"] = "https://apipie.ai/v1"
# Create a client with APIpie configuration
client = openai.OpenAI()
| Application Type | What Hugging Face Helps You Build |
|---|---|
| Natural Language Processing | Text generation, translation, summarization, and more |
| Computer Vision | Image classification, object detection, image segmentation |
| Audio Processing | Speech recognition, audio classification, text-to-speech |
| Multimodal Applications | Vision-language tasks, document understanding |
| Specialized Models | Domain-specific models like biomedical or financial NLP |
from huggingface_hub import InferenceClient
# Initialize client with APIpie
client = InferenceClient(
provider="openai",
api_key="your-apipie-api-key",
base_url="https://apipie.ai/v1"
)
# Use chat completion API with Hugging Face models through APIpie
response = client.chat.completions.create(
model="meta-llama/Meta-Llama-3-8B-Instruct", # Hugging Face model
messages=[
{"role": "user", "content": "What is the capital of France?"}
],
max_tokens=100
)
print(response.choices[0].message.content)
import { HfInference } from '@huggingface/inference';
// Initialize with APIpie configuration
const hf = new HfInference({
apiKey: 'your-apipie-api-key',
baseURL: 'https://apipie.ai/v1',
});
// Chat completion with Hugging Face model
const chatCompletion = await hf.chatCompletion({
model: 'meta-llama/Meta-Llama-3-8B-Instruct',
messages: [
{
role: 'user',
content: 'What is the capital of France?',
},
],
max_tokens: 100,
});
console.log(chatCompletion.choices[0].message.content);
import openai
import os
from PIL import Image
import io
import base64
# Configure OpenAI client to use APIpie
client = openai.OpenAI(
api_key="your-apipie-api-key",
base_url="https://apipie.ai/v1"
)
# Generate an image using a Hugging Face model through APIpie
response = client.images.generate(
model="stabilityai/stable-diffusion-2-1", # Hugging Face model
prompt="A serene landscape with mountains and a lake at sunset",
n=1,
size="1024x1024"
)
# Process and display the image
image_url = response.data[0].url
# Or if you get a base64 encoded image
# image_data = base64.b64decode(response.data[0].b64_json)
# image = Image.open(io.BytesIO(image_data))
# image.save("generated_image.png")
import openai
import os
# Configure client to use APIpie
client = openai.OpenAI(
api_key="your-apipie-api-key",
base_url="https://apipie.ai/v1"
)
# Transcribe audio using a Hugging Face model
with open("audio_sample.mp3", "rb") as audio_file:
response = client.audio.transcriptions.create(
model="facebook/wav2vec2-large-960h-lv60-self", # Hugging Face model
file=audio_file,
response_format="text"
)
print(response)
from huggingface_hub import InferenceClient
# Initialize client with APIpie
client = InferenceClient(
provider="openai",
api_key="your-apipie-api-key",
base_url="https://apipie.ai/v1"
)
# Generate embeddings
response = client.embeddings.create(
model="sentence-transformers/all-MiniLM-L6-v2", # Hugging Face model
input="The food was delicious and the service was excellent."
)
print(response.data[0].embedding)
import openai
import os
import base64
from PIL import Image
import io
# Configure client to use APIpie
client = openai.OpenAI(
api_key="your-apipie-api-key",
base_url="https://apipie.ai/v1"
)
# Read image file
with open("image.jpg", "rb") as image_file:
encoded_image = base64.b64encode(image_file.read()).decode('utf-8')
# Perform visual question answering using a Hugging Face model
response = client.chat.completions.create(
model="llava-hf/llava-1.5-7b-hf", # Hugging Face model
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "What is shown in this image?"},
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{encoded_image}"}}
]
}
],
max_tokens=300
)
print(response.choices[0].message.content)
For more information, see the Python Inference client Documentation or the Java Script Inference client Documentation.
If you encounter any issues during the integration process, please reach out on APIpie Discord for assistance.
Google Agent Development Kit (ADK) Integration Guide
Learn how to integrate Google Agent Development Kit (ADK) with APIpie for building flexible and powerful AI agents with code-first development.
Agno Integration Guide
Learn how to integrate Agno with APIpie for building powerful multimodal agents with advanced reasoning capabilities.