Public Access
59 lines
2.2 KiB
Python
59 lines
2.2 KiB
Python
import os
|
|
import json
|
|
import requests
|
|
from modules.logger import log_event
|
|
|
|
CATALOGUE_FILE = "/DATA/AppData/MULTI-IA-AETHAS38/catalogue_dynamique.json"
|
|
|
|
def interroger_huggingface(prompt: str, modele_cible=None):
|
|
"""
|
|
Connecteur pour l'API Serverless de Hugging Face.
|
|
Sélectionne dynamiquement le modèle le plus populaire scanné par l'Updater.
|
|
"""
|
|
api_key = os.getenv("HUGGINGFACE_API_KEY")
|
|
if not api_key:
|
|
raise Exception("Clé API HuggingFace manquante dans le .env")
|
|
|
|
# Extraction dynamique du meilleur modèle Hugging Face gratuit disponible
|
|
if not modele_cible:
|
|
try:
|
|
if os.path.exists(CATALOGUE_FILE):
|
|
with open(CATALOGUE_FILE, "r", encoding="utf-8") as f:
|
|
data = json.load(f)
|
|
hf_models = [m.replace("huggingface/", "") for m in data.get("free_models", []) if m.startswith("huggingface/")]
|
|
if hf_models:
|
|
modele_cible = hf_models[0] # Modèle le plus téléchargé selon l'Updater
|
|
except Exception:
|
|
pass
|
|
|
|
if not modele_cible:
|
|
modele_cible = "mistralai/Mistral-7B-Instruct-v0.3"
|
|
|
|
url = f"https://router.huggingface.co/hf-inference/models/{modele_cible}"
|
|
headers = {
|
|
"Authorization": f"Bearer {api_key}",
|
|
"Content-Type": "application/json"
|
|
}
|
|
|
|
# Paramètres optimisés pour les LLMs d'instruction
|
|
payload = {
|
|
"inputs": prompt,
|
|
"parameters": {
|
|
"max_new_tokens": 2048,
|
|
"return_full_text": False,
|
|
"temperature": 0.7
|
|
}
|
|
}
|
|
|
|
try:
|
|
resp = requests.post(url, headers=headers, json=payload, timeout=60)
|
|
if resp.status_code == 200:
|
|
data = resp.json()
|
|
if isinstance(data, list) and len(data) > 0 and "generated_text" in data[0]:
|
|
texte = data[0]["generated_text"].strip()
|
|
return texte, "#ffb000", "HuggingFace", modele_cible
|
|
|
|
raise Exception(f"Erreur {resp.status_code} : {resp.text}")
|
|
except Exception as e:
|
|
log_event("WARNING", "HUGGINGFACE", f"Échec de la requête pour {modele_cible} : {e}")
|
|
raise e |