Spaces:
Running
Running
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}") | |