File size: 1,418 Bytes
fbc35e5
 
 
 
332c4c1
fbc35e5
 
 
 
 
2ebd9ac
fbc35e5
2ebd9ac
fbc35e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from flask import Flask, request, jsonify
import pickle
import pandas as pd
from flask_cors import CORS


app = Flask(__name__)
CORS(app)

# Load the graph and data
with open("models/card_graph.pkl", "rb") as f:
    G = pickle.load(f)
df = pd.read_pickle("data/oracle_cards_graph.pkl")


# Define the endpoint to get closest card matches
@app.route("/api/get_matches", methods=["POST"])
def get_closest_matches():
    data = request.get_json()
    card_name = data.get("card_name", "")
    num_matches = data.get("num_matches", 5)

    # Check if the card exists in the graph
    if card_name not in G:
        return jsonify({"error": f"Card '{card_name}' not found in the graph."}), 404

    # Get neighbors (similar cards) and their corresponding weights
    neighbors = G[card_name]
    sorted_neighbors = sorted(
        neighbors.items(), key=lambda x: x[1]["weight"], reverse=True
    )
    closest_matches = sorted_neighbors[:num_matches]

    # Create a list of closest matches
    matches_info = []
    for match in closest_matches:
        matched_card = df[df["name"] == match[0]].iloc[0]
        match_info = {
            "name": matched_card["name"],
            "match_score": match[1]["weight"],
        }
        matches_info.append(match_info)

    # Return the list of matches as JSON
    return jsonify(matches_info)


if __name__ == "__main__":
    app.run(debug=True, host="0.0.0.0", port=7860)