python - Calling huggingface APIs fails - Stack Overflow

admin2025-05-02  1

This is my first time to use huggingface.

I want to call its API to answer a prompt.

Here is what I have so far:

import requests

# Define API details
API_URL = ".3-70B-Instruct"
API_TOKEN = "hf_********************************"

# Define headers with authentication token
headers = {
    "Authorization": f"Bearer {API_TOKEN}"
}

# Define the input payload
payload = {
    "inputs": "What is the biggest animal?",  # Replace with the input text or data
}

def query_huggingface_model(api_url, headers, payload):
    try:
        response = requests.post(api_url, headers=headers, json=payload)
        response.raise_for_status()  # Raise an error for HTTP codes 4xx/5xx
        return response.json()
    except requests.exceptions.RequestException as e:
        print(f"An error occurred: {e}")
        return None

# Query the model
response = query_huggingface_model(API_URL, headers, payload)

# Process and print the response
if response:
    print("Model Response:", response)
else:
    print("Failed to get a response from the model.")

but when I try to run it I get this error:

An error occurred: 400 Client Error: Bad Request for url:
.3-70B-Instruct
Failed to get a response from the model.

I tried to change the API_url to .3-70B-Instruct but still getting the same error.

How to fix that; are there other errors in my code?

转载请注明原文地址:http://www.anycun.com/QandA/1746138014a92099.html