Python HF Not Working

I created an account on Hugging Face and made a token. I have saved that as a variable in my environment. I still can’t seem to get a basic connection to interact with a script. Any help will be very much appreciated

import requests
import os

api_key = os.getenv(‘HF_TOKEN’) or os.getenv(‘HUGGINGFACE_API_KEY’)
print(f"Using API key: {api_key[:10]}…" if api_key else “No API key found”)

url = “https://api-inference.huggingface.co/models/distilgpt2
headers = {“Authorization”: f"Bearer {api_key}"}

Simple message

payload = {
“inputs”: “Hello, how are you today?”,
“options”: {“wait_for_model”: True}
}

print(“Sending request…”)
response = requests.post(url, headers=headers, json=payload)

print(f"Status: {response.status_code}“)
print(f"Response: {response.text}”)

if response.status_code == 200:
data = response.json()
print(f"Generated text: {data}“)
else:
print(f"Error: {response.status_code} - {response.text}”)

OUTPUT

Sending request…
Status: 404
Response: Not Found
Error: 404 - Not Found

1 Like

It cannot be used via API unless it is a deployed model, and deployed models are occasionally changed. That model does not appear to be deployed.

These are the models currently deployed with Hugging Face.