-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
129 lines (98 loc) · 3.88 KB
/
server.py
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
from datetime import datetime
import os
from flask import Flask, send_from_directory, send_file, request
from mmdx.search import VectorDB
from mmdx.model import ClipModel
import numpy as np
from mmdx.settings import DATA_PATH, DB_PATH, DB_DELETE_EXISTING, DB_BATCH_LOAD
app = Flask(__name__)
# Path for our main Svelte app. All routes in the app must be added
# here to allow refreshing to work correctly.
@app.route("/")
@app.route("/search/random")
@app.route("/search/image")
@app.route("/labels")
@app.route("/bootstrap")
def base():
return send_from_directory("client/dist/", "index.html")
# Path for all the static files (compiled JS/CSS, etc.)
@app.route("/<path:path>")
def assets(path):
return send_from_directory("client/dist/", path)
@app.route("/images/<path:path>")
def images(path):
return send_from_directory(DATA_PATH, path)
@app.route("/api/v1/random")
def random_search():
limit: int = request.args.get("limit", 12, type=int)
hits = db.random_search(limit=limit)
return {"total": len(hits.index), "hits": hits.to_dict("records")}
@app.route("/api/v1/keyword_search")
def keyword_search():
query: str = request.args.get("q")
exclude_labeled: bool = request.args.get("exclude_labeled", "false") == "true"
limit: int = request.args.get("limit", 12, type=int)
hits = db.search_by_text(query_string=query, limit=limit, exclude_labeled=exclude_labeled)
return {"total": len(hits.index), "hits": hits.to_dict("records")}
@app.route("/api/v1/image_search")
def image_search():
query: str = request.args.get("q")
exclude_labeled: bool = request.args.get("exclude_labeled", "false") == "true"
limit: int = request.args.get("limit", 12, type=int)
hits = db.search_by_image_path(image_path=query, limit=limit, exclude_labeled=exclude_labeled)
return {"total": len(hits.index), "hits": hits.to_dict("records")}
@app.route("/api/v1/add_label")
def add_label():
image_path: str = request.args.get("image_path", None, type=str)
label: str = request.args.get("label", None, type=str)
db.add_label(image_path=image_path, label=label)
return {"success": True}
@app.route("/api/v1/remove_label")
def remove_label():
image_path: str = request.args.get("image_path", None, type=str)
label: str = request.args.get("label", None, type=str)
db.remove_label(image_path=image_path, label=label)
return {"success": True}
@app.route("/api/v1/labels")
def labels():
labels = db.get_labels()
return {"labels": labels}
@app.route("/api/v1/label_counts")
def label_counts():
counts = db.get_label_counts()
print(counts)
return {"counts": counts}
@app.route("/api/v1/download/binary_labeled_data")
def download_binary_labeled_data():
current_time = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
filename = f"binary_labeled_data_{current_time}.zip"
output_zip_file = db.create_zip_labeled_binary_data(
output_dir=os.path.join(DB_PATH, "downloads"), filename=filename
)
print("Created zip file: ", output_zip_file)
return send_file(output_zip_file, as_attachment=True)
def create_db_for_data_path():
data_path = DATA_PATH
db_path = DB_PATH
print("Loading embedding model...")
model = ClipModel()
print(f"Loading vector database:")
print(f" - DB path: {os.path.abspath(db_path)}")
print(f" - Raw data path: {os.path.abspath(data_path)}")
print(f" - Delete existing?: {DB_DELETE_EXISTING}")
print(f" - Batch load?: {DB_BATCH_LOAD}")
vectordb = VectorDB.from_data_path(
data_path,
db_path,
model,
delete_existing=DB_DELETE_EXISTING,
batch_load=DB_BATCH_LOAD,
)
print("Finished DB initialization.")
return vectordb
db: VectorDB = create_db_for_data_path()
if __name__ == "__main__":
if os.environ.get("ENV") == "prod":
app.run(debug=False, host="0.0.0.0")
else:
app.run(debug=True, host="127.0.0.1")