DEV Community

qing
qing

Posted on

Hugging Face Inference API: Run 200,000+ AI Models for Free with Python

Hugging Face Inference API: Run 200,000+ AI Models for Free with Python

Hugging Face hosts 200,000+ models. Here's how to use them with Python.

Get Your Free API Key

  1. Create account at huggingface.co
  2. Go to Settings → Access Tokens
  3. Create a token (free)

Install

pip install httpx
Enter fullscreen mode Exit fullscreen mode

Text Generation

import httpx
import os

HF_TOKEN = os.getenv("HF_TOKEN")  # Get free at huggingface.co/settings/tokens
API_URL = "https://api-inference.huggingface.co/models"

def generate_text(prompt: str, model: str = "mistralai/Mistral-7B-Instruct-v0.1") -> str:
    with httpx.Client() as client:
        r = client.post(
            f"{API_URL}/{model}",
            headers={"Authorization": f"Bearer {HF_TOKEN}"},
            json={"inputs": prompt, "parameters": {"max_new_tokens": 200}},
            timeout=60,
        )
        return r.json()[0]["generated_text"]

print(generate_text("Write a Python function that sorts a list"))
Enter fullscreen mode Exit fullscreen mode

Image Classification

import httpx
import base64

def classify_image(image_path: str, model: str = "google/vit-base-patch16-224") -> list:
    with open(image_path, "rb") as f:
        image_data = base64.b64encode(f.read()).decode()

    with httpx.Client() as client:
        r = client.post(
            f"{API_URL}/{model}",
            headers={"Authorization": f"Bearer {HF_TOKEN}"},
            json={"inputs": image_data},
            timeout=30,
        )
    return r.json()  # Returns list of {label, score}
Enter fullscreen mode Exit fullscreen mode

Sentiment Analysis

def analyze_sentiment(text: str) -> dict:
    with httpx.Client() as client:
        r = client.post(
            f"{API_URL}/distilbert-base-uncased-finetuned-sst-2-english",
            headers={"Authorization": f"Bearer {HF_TOKEN}"},
            json={"inputs": text},
            timeout=30,
        )
    return r.json()[0]

result = analyze_sentiment("Python is amazing!")
print(result)  # [{"label": "POSITIVE", "score": 0.9998}]
Enter fullscreen mode Exit fullscreen mode

Best Free Models

Task Model Quality
Text Gen mistralai/Mistral-7B-Instruct-v0.1 ⭐⭐⭐⭐
Code bigcode/starcoder2-3b ⭐⭐⭐⭐
Sentiment distilbert-base-uncased-finetuned-sst-2 ⭐⭐⭐⭐⭐
Translation Helsinki-NLP/opus-mt-en-zh ⭐⭐⭐⭐

Follow me for more AI Python tutorials! 🐍

Follow for more Python content!


💡 Related: **Content Creator Ultimate Bundle (Save 33%)* — $29.99*

Top comments (0)