Spaces:
Running
Running
Johnny
updated config and ultils to include sentence_transformer for score, re-added gemma for summarization
4f034fb
import os | |
from dotenv import load_dotenv | |
from supabase import create_client | |
import requests | |
import time | |
from sentence_transformers import SentenceTransformer # Import the transformer model | |
# 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) | |
# Load Sentence Transformer Model (scoring) | |
embedding_model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2") | |
HF_MODELS = { | |
"gemma": "https://router.huggingface.co/hf-inference/models/Falconsai/text_summarization" | |
} | |
HF_API_TOKEN = os.getenv("HF_API_TOKEN") | |
if not HF_API_TOKEN: | |
raise ValueError("Missing Hugging Face API key. Check your .env file.") | |
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' for summarization). | |
: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' for summarization.") | |
api_url = HF_MODELS[model] | |
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 | |
if response.status_code == 402: | |
print(f"Error querying Hugging Face model '{model}': 402 Payment Required. Free tier may not support this model.") | |
return None | |
if response.status_code == 500: | |
print(f"Server error (500) on attempt {attempt + 1}. Retrying in {delay} seconds...") | |
time.sleep(delay) | |
continue | |
response.raise_for_status() | |
return response.json() | |
except requests.exceptions.RequestException as e: | |
print(f"Error querying Hugging Face model '{model}': {e}") | |
time.sleep(delay) | |
print("All retry attempts failed.") | |
return None |