File size: 4,960 Bytes
9a03fcf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
148
149
150
151
152
153
154
155
156
from flask import Blueprint, jsonify, request
import data_processor
import os

# Create a Blueprint for API routes
bp = Blueprint("api", __name__, url_prefix="/api")


@bp.route("/leaderboard", methods=["GET"])
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


@bp.route("/columns", methods=["GET"])
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


@bp.route("/benchmarks", methods=["GET"])
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


@bp.route("/examples", methods=["GET"])
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


@bp.route("/attacks", methods=["GET"])
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