File size: 956 Bytes
987c803
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import requests
import os
from dotenv import load_dotenv

load_dotenv()

# Define the endpoint and your API key
api_url = "https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3-8B-Instruct"
api_key = os.getenv('HFSecret')

# Define the headers for authorization
headers = {
    "Authorization": f"Bearer {api_key}"
}

# Function to call the HuggingFace API
def call_huggingface_api(prompt):
    data = {
        "inputs": prompt,
        "parameters": {"max_length": 500, "temperature": 0.5}
    }
    
    response = requests.post(api_url, headers=headers, json=data)
    
    if response.status_code != 200:
        raise Exception(f"Error: {response.status_code}, {response.text}")
    
    return response.json()

# Example: Sentiment analysis
prompt = "Perform sentiment analysis on the following text: I love programming!"
try:
    result = call_huggingface_api(prompt)
    print(result)
except Exception as e:
    print(f"Error: {e}")