Spaces:
Runtime error
Runtime error
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 | |
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) | |