File size: 4,416 Bytes
892f06a
f3f0a69
892f06a
 
79b5c9c
f3f0a69
56325dc
892f06a
 
f3f0a69
892f06a
f3f0a69
 
8def571
 
 
 
 
 
892f06a
56325dc
 
 
 
 
 
892f06a
4f034fb
 
79b5c9c
56325dc
edfcf73
 
79b5c9c
 
8f771eb
 
892f06a
79b5c9c
 
 
 
 
 
 
 
 
 
 
892f06a
 
79b5c9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
892f06a
79b5c9c
 
edfcf73
79b5c9c
edfcf73
79b5c9c
 
 
 
edfcf73
79b5c9c
 
edfcf73
79b5c9c
 
 
 
 
 
 
 
 
4f034fb
79b5c9c
 
 
 
 
 
 
 
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# === Imports ===
import os
import time
import requests
import socket
from dotenv import load_dotenv
from supabase import create_client
from sentence_transformers import SentenceTransformer
from openai import OpenAI

# === Load Environment Variables ===
load_dotenv()

# === OpenAI Configuration ===
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
if not OPENAI_API_KEY:
    raise ValueError("OPENAI_API_KEY is not set in the environment variables.")
openai_client = OpenAI(api_key=OPENAI_API_KEY)

# === Supabase Configuration ===
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)

# === Embedding Model for Scoring ===
embedding_model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")

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

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

# === Hugging Face Model Endpoints ===
HF_ENDPOINTS = {
    "bart-large-cnn-ovt": {
        "url": "https://hedemwou4oqkk65c.us-east-1.aws.endpoints.huggingface.cloud",
        "task": "summarization",
        "model_id": "facebook/bart-large-cnn"
    },
    "vzwjawyxvu030jsw": {  # Updated endpoint name to match URL
        "url": "https://vzwjawyxvu030jsw.us-east-1.aws.endpoints.huggingface.cloud",
        "task": "text-generation",
        "model_id": "google/gemma-7b"
    }
}

def check_endpoint_status(endpoint_name: str) -> dict:
    """
    Check the status of a private Hugging Face endpoint using DNS resolution
    """
    if endpoint_name not in HF_ENDPOINTS:
        return {
            "status": "error",
            "error": f"Unknown endpoint: {endpoint_name}"
        }

    try:
        endpoint_info = HF_ENDPOINTS[endpoint_name]
        hostname = endpoint_info['url'].replace('https://', '').split('/')[0]
        
        # Try DNS resolution
        try:
            socket.gethostbyname(hostname)
            # If DNS resolves, endpoint exists but may be stopped
            return {
                "status": "stopped",
                "scaled": True,
                "pending": 0,
                "error": None
            }
        except socket.gaierror:
            # If DNS fails, endpoint doesn't exist
            return {
                "status": "error",
                "error": "Endpoint not found"
            }
    except Exception as e:
        return {
            "status": "error",
            "error": str(e)
        }

def toggle_endpoint(endpoint_name: str, action: str) -> dict:
    """
    Start or stop a private Hugging Face endpoint
    """
    try:
        # For private endpoints, use the Endpoints API
        api_base = "https://api.endpoints.huggingface.cloud"
        action_url = f"{api_base}/v2/endpoint/{endpoint_name}/{action}"
        
        response = requests.post(
            action_url,
            headers=HF_HEADERS,
            timeout=10
        )
        
        if response.status_code in [200, 202]:
            return {
                "success": True,
                "message": f"Successfully {action}ed endpoint"
            }
        else:
            return {
                "error": f"Failed to {action} endpoint: {response.text}"
            }
    except Exception as e:
        return {
            "error": f"Failed to {action} endpoint: {str(e)}"
        }

# === Query Helper ===
def query(payload: dict, endpoint_name: str) -> dict:
    """
    Send a query to a Hugging Face endpoint
    """
    if endpoint_name not in HF_ENDPOINTS:
        return {
            "error": f"Unknown endpoint: {endpoint_name}"
        }

    endpoint_info = HF_ENDPOINTS[endpoint_name]
    url = endpoint_info['url']

    try:
        response = requests.post(
            url,
            headers=HF_HEADERS,
            json=payload,
            timeout=30
        )

        if response.status_code == 200:
            return response.json()
        else:
            return {
                "error": f"Query failed with status {response.status_code}: {response.text}"
            }
    except Exception as e:
        return {
            "error": str(e)
        }