Testing the Hugging Face Inference API (Free Tier)

2026-10-12来源 dev.to PythonEnglish

Testing the Hugging Face Inference API (Free Tier)

Learn how to use Hugging Face's free inference API for AI model testing.

Prerequisites

Quick Start

import requests import os # Get your free token from Hugging Face settings HF_TOKEN = os.getenv("HF_TOKEN") # Use a free model (no auth required for some) MODEL = "google/flan-t5-base" def query_hf(prompt: str, model: str = MODEL) -> str: """Query Hugging Face Inference API.""" url = f"https://api-inference.huggingface.co/models/{model}" headers = {"Authorization": f"Bearer {HF_TOKEN}"} if HF_TOKEN else {} response = requests.post( url, headers=headers, json={"inputs": prompt}, timeout=30 ) if response.status_code == 200: return response.json()[0]["generated_text"] else: raise Exception(f"Error: {response.status_code} - {response.text}") # Usage if __name__ == "__main__": prompt = "Translate to French: Hello, how are you?" try: result = query_hf(prompt) print(f"Prompt: {prompt}") print(f"Result: {result}") except Exception as e: print(f"Error: {e}") 

Testing Different Free Models

# Text generation models = [ "gpt2", "facebook/opt-350m", "google/flan-t5-base", "microsoft/DialoGPT-small" ] for model in models: try: result = query_hf("The future of AI is", model) print(f"{model}: {result[:100]}...") except Exception as e: print(f"{model}: Error - {e}") 

Error Handling & Rate Limits

import time def robust_query(prompt: str, model: str = MODEL, retries: int = 3) -> str: """Query with exponential backoff for rate limits.""" for attempt in range(retries): try: return query_hf(prompt, model) except Exception as e: if "429" in str(e) or "503" in str(e): wait = 2 ** attempt print(f"Rate limited. Waiting {wait}s...") time.sleep(wait) else: raise raise Exception("Max retries exceeded") 

Conclusion

Hugging Face's free inference API provides access to thousands of models without any cost. Perfect for experimentation, prototyping, and learning AI integration.

原文:https://www.a8q9.cn/en/article/testing-the-hugging-face-inference-api-free-tier-e720ae.html

相关文章