File size: 2,465 Bytes
f3f0a69
 
56325dc
edfcf73
8f771eb
f3f0a69
 
 
 
56325dc
 
 
 
 
 
 
8f771eb
edfcf73
 
 
 
 
56325dc
edfcf73
 
 
8f771eb
 
 
 
edfcf73
8f771eb
edfcf73
 
8f771eb
 
 
 
edfcf73
 
 
 
8f771eb
edfcf73
8f771eb
 
 
edfcf73
8f771eb
 
 
edfcf73
8f771eb
 
 
 
edfcf73
8f771eb
edfcf73
8f771eb
edfcf73
8f771eb
 
 
19ea0c5
8f771eb
 
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import os
from dotenv import load_dotenv
from supabase import create_client
import requests
import time

# Load environment variables from .env file
load_dotenv()

# Supabase API Config
SUPABASE_URL = "https://lmpazoxzucnlqqxjoihi.supabase.co"
SUPABASE_KEY = os.getenv("SUPABASE_API_KEY")
if not SUPABASE_KEY:
    raise ValueError("SUPABASE_KEY is not set in the environment variables.")
supabase = create_client(SUPABASE_URL, SUPABASE_KEY)

# Hugging Face API Config
HF_MODELS = {
    "gemma": "https://api-inference.huggingface.co/models/google/gemma-7b",
    "bart": "https://api-inference.huggingface.co/models/facebook/bart-large-cnn"
}

HF_API_TOKEN = os.getenv("HF_API_TOKEN")
if not HF_API_TOKEN:
    raise ValueError("Missing Hugging Face API key. Check your .env file.")

# Correct API Headers
HF_HEADERS = {"Authorization": f"Bearer {HF_API_TOKEN}"}

def query(payload, model="gemma", retries=3, delay=5):
    """
    Sends a request to the Hugging Face API with retries.

    :param payload: The input data for inference.
    :param model: The model name ('gemma' or 'bart').
    :param retries: Number of times to retry if the request fails.
    :param delay: Delay in seconds before retrying.
    :return: The model's response in JSON format, or None if all retries fail.
    """
    if model not in HF_MODELS:
        raise ValueError("Invalid model name. Choose 'gemma' or 'bart'.")

    api_url = HF_MODELS[model]  # Correct model URL

    for attempt in range(retries):
        try:
            response = requests.post(api_url, headers=HF_HEADERS, json=payload)

            if response.status_code == 401:
                print(f"Error querying Hugging Face model '{model}': 401 Unauthorized. Check API key.")
                return None  # API key issue

            if response.status_code == 500:
                print(f"Server error (500) on attempt {attempt + 1}. Retrying in {delay} seconds...")
                time.sleep(delay)  # Wait before retrying
                continue  # Retry the request

            response.raise_for_status()  # Raise an error for failed requests (except 500)

            return response.json()  # Return the parsed JSON response

        except requests.exceptions.RequestException as e:
            print(f"Error querying Hugging Face model '{model}': {e}")
            time.sleep(delay)  # Wait before retrying

    print("All retry attempts failed.")
    return None  # Return None if all retries fail