forked from VIDA-NYU/mmdx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
194 lines (158 loc) · 5.51 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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
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,
ACCESS_KEY,
ENDPOINT_URL,
SECRET_KEY,
DATA_SOURCE,
LOAD_DATA,
)
from mmdx.s3_client import S3Client
from io import BytesIO
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):
if DATA_SOURCE.upper() == "S3":
image_data = S3_Client.get_obj(DATA_PATH, path)
image_buffer = BytesIO(image_data.read())
return send_file(
image_buffer,
as_attachment=False,
download_name=path,
)
else:
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, S3_Client=S3_Client, 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)
table: str = request.args.get("table", None, type=str)
db.add_label(image_path=image_path, label=label, table=table)
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)
table: str = request.args.get("table", None, type=str)
db.remove_label(image_path=image_path, label=label, table=table)
return {"success": True}
@app.route("/api/v1/labels")
def labels():
table: str = request.args.get("table", None, type=str)
labels = db.get_labels(table)
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"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)
@app.route("/api/v1/load/csv_data", methods=['POST'])
def create_database():
# try:
if 'file' not in request.files:
return {'error': 'No file part'}
file = request.files['file']
print(file)
if file.filename == '':
return {'error': 'No selected file'}
filename = file.filename
filepath = os.path.join(filename)
file.save(filepath)
os.environ['CSV_PATH'] = filepath
global db
db = create_db_for_data_path(S3_Client)
return {'message': 'CSV data received and processed successfully'}
def create_db_for_data_path(S3_Client):
data_path = DATA_PATH
db_path = DB_PATH
if DATA_SOURCE.upper() == "S3":
data_path_msg = f" - Data Bucket: {data_path}"
else:
S3_Client = None
data_path_msg = f" - Raw data path: {os.path.abspath(data_path)}"
print("Loading embedding model...")
model = ClipModel()
print(f"Loading vector database:")
print(f" - DB path: {os.path.abspath(db_path)}")
print(data_path_msg)
print(f" - Delete existing?: {DB_DELETE_EXISTING}")
print(f" - Batch load?: {DB_BATCH_LOAD}")
vectordb = VectorDB.from_data_path(
data_path,
db_path,
S3_Client,
model,
delete_existing=DB_DELETE_EXISTING,
batch_load=DB_BATCH_LOAD,
)
print("Finished DB initialization.")
return vectordb
if DATA_SOURCE.upper() == "S3":
S3_Client = S3Client(
access_key=ACCESS_KEY,
secret_key=SECRET_KEY,
endpoint_url=ENDPOINT_URL,
)
else:
S3_Client=None
if LOAD_DATA:
db: VectorDB = None
else:
db: VectorDB = create_db_for_data_path(S3_Client)
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")