Files
mammouth-models-dashboard/update_models.py

141 lines
4.8 KiB
Python
Raw Normal View History

import os
import requests
import json
import time
from dotenv import load_dotenv
load_dotenv("../.env.global")
AIANALASYS_APIKEY = os.getenv("AIANALASYS_APIKEY")
def get_mammouth_models():
# URL correcte fournie par l'utilisateur
url = "https://api.mammouth.ai/public/models"
try:
response = requests.get(url, verify=False) # verify=False au cas où il y a des soucis de certifs
response.raise_for_status()
return response.json().get('data', [])
except Exception as e:
print(f"Error fetching Mammouth models: {e}")
return []
def get_aa_data():
url = "https://artificialanalysis.ai/api/v2/data/llms/models"
headers = {"x-api-key": AIANALASYS_APIKEY}
try:
response = requests.get(url, headers=headers)
response.raise_for_status()
return response.json().get('data', [])
except Exception as e:
print(f"Error fetching Artificial Analysis data: {e}")
return []
def generate_markdown(models_data):
categories = {}
for m in models_data:
cat = m.get('category', 'General')
if cat not in categories:
categories[cat] = []
categories[cat].append(m)
md = "# Table des Modèles Mammouth.ai\n\n"
md += "*Mise à jour automatique via Artificial Analysis & Mammouth Public API*\n\n"
md += f"Dernière mise à jour : {time.strftime('%Y-%m-%d %H:%M:%S')}\n\n"
order = ['Coding', 'Agents', 'General']
sorted_cats = sorted(categories.keys(), key=lambda x: order.index(x) if x in order else 99)
for cat in sorted_cats:
models = categories[cat]
# Tri : Score (desc), puis Prix (asc)
models.sort(key=lambda x: (x.get('score') or 0, -(x.get('price_in') or 999)), reverse=True)
md += f"## {cat}\n\n"
md += "| Modèle | Prix (In / Out / 1M) | Intelligence Index | Vitesse (TPS) |\n"
md += "| :--- | :--- | :--- | :--- |\n"
for m in models:
p_in = f"${m['price_in']:.2f}"
p_out = f"${m['price_out']:.2f}"
score = f"**{m['score']:.1f}**" if m['score'] else "N/A"
speed = f"{m['speed']:.1f}" if m['speed'] else "N/A"
md += f"| {m['name']} | {p_in} / {p_out} | {score} | {speed} |\n"
md += "\n"
return md
def main():
print("Fetching Mammouth public models...")
mammouth_models = get_mammouth_models()
print(f"Found {len(mammouth_models)} models from Mammouth.")
print("Fetching Artificial Analysis data...")
aa_raw = get_aa_data()
# Mapping AA
aa_map = {}
for aa_m in aa_raw:
m_id = aa_m.get('model_id', '').lower()
m_name = aa_m.get('model_name', '').lower()
if m_id: aa_map[m_id] = aa_m
if m_name: aa_map[m_name] = aa_m
enriched_models = []
for m in mammouth_models:
m_id = m.get('id', '')
info = m.get('model_info', {})
if not m_id: continue
# Mapping intelligent
m_id_low = m_id.lower()
aa_info = aa_map.get(m_id_low)
# Si pas de match exact, on cherche une correspondance partielle
if not aa_info:
for key in aa_map:
if key in m_id_low or m_id_low in key:
# On vérifie que ce n'est pas un faux positif (ex: gpt-4 vs gpt-4-turbo)
if abs(len(key) - len(m_id_low)) < 5:
aa_info = aa_map[key]
break
# Extraction des prix
try:
price_in = float(info.get('input_cost_per_token', 0)) * 1000000
price_out = float(info.get('output_cost_per_token', 0)) * 1000000
except:
price_in, price_out = 0, 0
score = None
speed = None
if aa_info:
evals = aa_info.get('evaluations', {})
score = evals.get('artificial_analysis_intelligence_index')
speed = aa_info.get('median_output_tokens_per_second')
# Catégorisation
category = "General"
if any(x in m_id_low for x in ['coding', 'code', 'starcoder', 'coder', 'codestral']):
category = "Coding"
elif any(x in m_id_low for x in ['agent', 'hermes', 'tool', 'function', 'sonar']):
category = "Agents"
enriched_models.append({
'name': m_id,
'price_in': price_in,
'price_out': price_out,
'score': score,
'speed': speed,
'category': category
})
# Filtrer les modèles (on garde tout ce qui a un prix ou un score)
final_list = [m for m in enriched_models if m['price_in'] > 0 or m['score'] is not None]
with open("README.md", "w", encoding="utf-8") as f:
f.write(generate_markdown(final_list))
print(f"Done! README.md updated with {len(final_list)} models.")
if __name__ == "__main__":
main()