Spaces:
Sleeping
Sleeping
from flask import Blueprint, jsonify, request | |
import data_processor | |
import os | |
# Create a Blueprint for API routes | |
bp = Blueprint("api", __name__, url_prefix="/api") | |
def get_leaderboard(): | |
""" | |
API endpoint to get leaderboard data with optional filtering and searching | |
Query parameters: | |
- benchmark: Which benchmark to use (image, audio) | |
- filter_columns: Columns to filter by | |
- search_query: Text to search for | |
- core_columns: Core columns that should always be included | |
""" | |
# Get parameters from query string | |
benchmark = request.args.get("benchmark", "image") | |
filter_columns = ( | |
request.args.get("filter_columns", "").split(",") | |
if request.args.get("filter_columns") | |
else [] | |
) | |
search_query = request.args.get("search_query", "") | |
core_columns = ( | |
request.args.get("core_columns", "").split(",") | |
if request.args.get("core_columns") | |
else [] | |
) | |
# Determine file path based on benchmark | |
file_path = f"./data/{benchmark}_benchmark.csv" | |
# Load data | |
try: | |
data = data_processor.load_data(file_path) | |
# Apply filters if provided | |
if filter_columns: | |
data = data_processor.filter_data(data, filter_columns) | |
# Apply search if provided | |
if search_query: | |
data = data_processor.search_data(data, search_query) | |
return jsonify({"success": True, "data": data}) | |
except Exception as e: | |
return jsonify({"success": False, "error": str(e)}), 500 | |
def get_columns(): | |
""" | |
API endpoint to get all available columns for a benchmark | |
Query parameters: | |
- benchmark: Which benchmark to use (image, audio) | |
""" | |
benchmark = request.args.get("benchmark", "image") | |
file_path = f"./data/{benchmark}_benchmark.csv" | |
try: | |
columns = data_processor.get_columns(file_path) | |
return jsonify({"success": True, "columns": columns}) | |
except Exception as e: | |
return jsonify({"success": False, "error": str(e)}), 500 | |
def get_benchmarks(): | |
"""API endpoint to get available benchmarks""" | |
try: | |
benchmarks = data_processor.get_available_benchmarks() | |
return jsonify({"success": True, "benchmarks": benchmarks}) | |
except Exception as e: | |
return jsonify({"success": False, "error": str(e)}), 500 | |
def get_examples(): | |
""" | |
API endpoint to get examples for a specific benchmark and model | |
Query parameters: | |
- benchmark: Which benchmark to use (image, audio) | |
- model: Which model to get examples for | |
- attack: Which attack to get examples for (optional) | |
""" | |
benchmark = request.args.get("benchmark", "image") | |
model = request.args.get("model", "") | |
attack = request.args.get("attack", "") | |
if not model: | |
return jsonify({"success": False, "error": "Model parameter is required"}), 400 | |
# Construct path to examples | |
base_path = f"./examples/{benchmark}/{model}" | |
if attack: | |
base_path = os.path.join(base_path, attack) | |
try: | |
# Check if directory exists | |
if not os.path.exists(base_path): | |
return ( | |
jsonify({"success": False, "error": f"No examples found for {model}"}), | |
404, | |
) | |
# Get list of examples | |
examples = [] | |
for root, _, files in os.walk(base_path): | |
for file in files: | |
if file.endswith((".png", ".jpg", ".jpeg", ".wav", ".mp3")): | |
rel_path = os.path.relpath(os.path.join(root, file), "./examples") | |
examples.append( | |
{ | |
"path": f"/examples/{rel_path}", | |
"name": file, | |
"attack": attack or os.path.basename(root), | |
} | |
) | |
return jsonify({"success": True, "examples": examples}) | |
except Exception as e: | |
return jsonify({"success": False, "error": str(e)}), 500 | |
def get_attacks(): | |
""" | |
API endpoint to get available attacks for a benchmark | |
Query parameters: | |
- benchmark: Which benchmark to use (image, audio) | |
""" | |
benchmark = request.args.get("benchmark", "image") | |
file_path = f"./data/{benchmark}_attacks_variations.csv" | |
try: | |
if os.path.exists(file_path): | |
data = data_processor.load_data(file_path) | |
return jsonify({"success": True, "attacks": data}) | |
else: | |
return ( | |
jsonify( | |
{"success": False, "error": f"No attack data found for {benchmark}"} | |
), | |
404, | |
) | |
except Exception as e: | |
return jsonify({"success": False, "error": str(e)}), 500 | |