uripper commited on
Commit
fbc35e5
·
1 Parent(s): 6eab1e7
Files changed (3) hide show
  1. Dockerfile +20 -0
  2. app/app.py +48 -0
  3. requirements.txt +4 -0
Dockerfile ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use an official Python runtime as a parent image
2
+ FROM python:3.11
3
+
4
+ # Set the working directory in the container
5
+ WORKDIR /app
6
+
7
+ # Copy the current directory contents into the container at /app
8
+ COPY . ./app
9
+
10
+ # Install any needed packages specified in requirements.txt
11
+ RUN pip install --no-cache-dir -r requirements.txt
12
+
13
+ # Make port 7860 available to the world outside this container
14
+ EXPOSE 7860
15
+
16
+ # Define environment variable
17
+ ENV NAME FlaskApp
18
+
19
+ # Run app.py when the container launches
20
+ CMD ["python", "app.py"]
app/app.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, jsonify
2
+ import pickle
3
+ import pandas as pd
4
+ from flask_cors import CORS
5
+
6
+ app = Flask(__name__)
7
+ CORS(app)
8
+
9
+ # Load the graph and data
10
+ with open("models/card_graph.pkl", "rb") as f:
11
+ G = pickle.load(f)
12
+ df = pd.read_pickle("data/oracle_cards_graph.pkl")
13
+
14
+
15
+ # Define the endpoint to get closest card matches
16
+ @app.route("/api/get_matches", methods=["POST"])
17
+ def get_closest_matches():
18
+ data = request.get_json()
19
+ card_name = data.get("card_name", "")
20
+ num_matches = data.get("num_matches", 5)
21
+
22
+ # Check if the card exists in the graph
23
+ if card_name not in G:
24
+ return jsonify({"error": f"Card '{card_name}' not found in the graph."}), 404
25
+
26
+ # Get neighbors (similar cards) and their corresponding weights
27
+ neighbors = G[card_name]
28
+ sorted_neighbors = sorted(
29
+ neighbors.items(), key=lambda x: x[1]["weight"], reverse=True
30
+ )
31
+ closest_matches = sorted_neighbors[:num_matches]
32
+
33
+ # Create a list of closest matches
34
+ matches_info = []
35
+ for match in closest_matches:
36
+ matched_card = df[df["name"] == match[0]].iloc[0]
37
+ match_info = {
38
+ "name": matched_card["name"],
39
+ "match_score": match[1]["weight"],
40
+ }
41
+ matches_info.append(match_info)
42
+
43
+ # Return the list of matches as JSON
44
+ return jsonify(matches_info)
45
+
46
+
47
+ if __name__ == "__main__":
48
+ app.run(debug=True, host="0.0.0.0", port=7860)
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ Flask
2
+ pandas
3
+ Flask-Cors
4
+ networkx