A 404 response when calling the Hugging Face Inference API endpoint with a valid token usually means the API endpoint model does not support inference or is not available via the Inference API.
Important points:
-
GPT-2 model (gpt2) is not supported by the Hugging Face hosted Inference API by default.
The Inference API supports specific models that have been enabled for inference hosting.
-
You can only send requests to models that support the Inference API (these models explicitly show an Inference API badge on their Hugging Face page).
For example, distilbert-base-uncased-finetuned-sst-2-english, facebook/bart-large-cnn, or some hosted text generation models.
How to check if a model supports the API:
- Open the model card page on huggingface.co.
- Pick a model.
- Look for "Hosted inference API" section or an "Use in Transformers" widget.
- Or try via the API Explorer at https://huggingface.co/inference-api
Example models supporting hosted API:
distilbert-base-uncased-finetuned-sst-2-english (sentiment)
facebook/bart-large-cnn (summarization)
bert-base-uncased (fill-mask)
What you can do
Example: Use GPT-2-like model via the text-generation API endpoint
Hugging Face provides a text-generation API where you can send requests to specific models that are text-generation enabled.
For example, use:
https://api-inference.huggingface.co/models/gpt2
But this works only if the model supports hosted inference.
If gpt2 returns 404, usually it means this model is not enabled for hosted inference API on your account.
Try a model that is guaranteed to work:
For example, try the sentiment-analysis model:
- Endpoint:
https://api-inference.huggingface.co/models/distilbert-base-uncased-finetuned-sst-2-english
Change your C# example API url accordingly:
private static readonly string apiUrl = "https://api-inference.huggingface.co/models/distilbert-base-uncased-finetuned-sst-2-english";
Then your payload would be something like:
{
"inputs": "I love using Hugging Face API!"
}
Output looks like:
[
{
"label": "POSITIVE",
"score": 0.9997
}
]
Alternative: Use the Hugging Face text-generation endpoint
Another approach: Use the official Hugging Face text-generation inference API that is designed for generation.
See this page:
Example: For text-generation you need:
{
"inputs": "Your prompt",
"parameters": {
"max_new_tokens": 50,
"do_sample": true
}
}
Recap / Troubleshooting Tips:
- 404 = model not found or not hosted on the inference API.
- Use a model with hosted inference enabled.
- Confirm your token works by testing on models guaranteed to exist.
- Use the right request payload format for the model.
- For GPT-2 style text generation, if the official model endpoint is not supported, try
gpt2-large or other GPT-2 family models that may be hosted.
- Check the model card: if it says Hosted API or an "Use in Inference API" button, it is enabled.
If you want to generate text with GPT-2 in your .NET app and hug face hosted endpoint for gpt2 is not available:
You have two options:
- Use a hosted GPT-2-like model that supports inference API, e.g.,
EleutherAI/gpt-neo-125M:
private static readonly string apiUrl = "https://api-inference.huggingface.co/models/EleutherAI/gpt-neo-125M";
- Host your own model or run locally using Transformers library (Python usually).
Quick test for you:
Try this minimal curl command:
curl -H "Authorization: Bearer YOUR_TOKEN" \
-d '{"inputs": "The quick brown fox"}' \
https://api-inference.huggingface.co/models/distilbert-base-uncased-finetuned-sst-2-english
You should get a JSON sentiment response instead of 404.
If you want, I can provide you with a full working .NET example targeting a known working model — just let me know!