-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
209 lines (165 loc) · 7.16 KB
/
app.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import json
import sys
from functools import wraps
import numpy as np
from flask import Flask, request, jsonify, Blueprint, send_file
import os
from pojo.dataset import get_modal_type
from pojo.embedding import Embedding
from pojo.index import get_index, set_index
from pojo.response_data import ResponseData
from pojo.search import get_search
from vector_weight_learning import fvecs_converter
blueprint = Blueprint('blueprint', __name__, url_prefix='/m1/4132394-0-default')
# a decorator to simplify response
def jsonify_response_data(f):
@wraps(f)
def decorated_function(*args, **kwargs):
response_data = f(*args, **kwargs)
if isinstance(response_data, ResponseData):
response_dict = response_data.to_dict()
return jsonify(response_dict)
return response_data
return decorated_function
@blueprint.route('/embedding', methods=['POST'])
@jsonify_response_data
def post_embedding():
try:
body = request.json
learning = body['learning']
data = {
'id': 0,
'modalities': body['modalities'],
'deleted': False
}
weight = []
for modality in data['modalities']:
modality['weight'] = 1.0 / len(data['modalities'])
weight.append(1.0 / len(data['modalities']))
embedding = Embedding(data=data)
embedding.create_embedding(data=data)
if learning:
weight = embedding.learning()
return ResponseData(data=weight)
return ResponseData(data={})
except Exception as e:
return ResponseData(message=str(e), data={})
@blueprint.route('/index', methods=['POST'])
@jsonify_response_data
def post_index():
body = request.json
algorithm = body['algorithm']
neighbor = body['neighbor']
candidate = body['candidate']
index_weight = body['index_weight']
# normalize indexes' weight
total = sum(index_weight)
index_weight = [item / total for item in index_weight]
set_index(algorithm=algorithm, neighbor=neighbor, candidate=candidate, index_weight=index_weight)
return ResponseData(data={})
@blueprint.route('/search', methods=['POST'])
@jsonify_response_data
def post_search():
try:
print(request.form)
llm = request.form.get('llm')
text = request.form.get('text')
temperature = float(request.form.get('temperature'))
retrieval_number = int(request.form.get('resultNumber'))
retrieval_framework = request.form.get('retrievalFramework')
use_knowledge = bool(request.form.get('useKnowledge'))
selected_target = int(request.form.get('selectedTarget'))
retrieval_weight = [float(item) for item in json.loads(request.form.get('retrievalWeight'))]
for filename in os.listdir(search_path):
if filename != 'result.txt':
filepath = os.path.join(search_path, filename)
os.remove(filepath)
# save file to /search/xxx.tmp
text_path = ''
embedding = Embedding.get()
for i, modality in enumerate(embedding.modalities):
for j, modal in enumerate(modality.modals):
t = get_modal_type(modal)
with open(os.path.join(search_path, f'{modal}.tmp'), 'w') as file:
if t == 'text':
text_path = os.path.join(search_path, f'{modal}.tmp')
file.write(text)
elif t in request.files:
tmp = request.files[t]
tmp.save(os.path.join(upload_path, tmp.filename))
file.write(os.path.join(upload_path, tmp.filename))
# fix weight
index_method, index_path = get_index()
if len(retrieval_weight) != 0:
total = sum(retrieval_weight)
retrieval_weight = [item / total for item in retrieval_weight]
# print(use_knowledge)
search = None if not use_knowledge else get_search(retrieval_framework=retrieval_framework,
selected_target=selected_target,
retrieval_number=retrieval_number,
retrieval_weight=retrieval_weight,
index_path=index_path,
index_method=index_method)
# get LLM
from pojo.llm import get_llm
llm_model = get_llm(model=llm, temperature=temperature, history=history, search=search, text_path=text_path)
data = llm_model.generate_answer(content=text)
return ResponseData(data=data)
except Exception as e:
return ResponseData(message=str(e), data={})
@blueprint.route('/image', methods=['GET'])
@jsonify_response_data
def get_image():
param = request.args
meta = param.get('meta')
id = int(param.get('id'))
with open(os.path.join(root, 'dataset', 'meta', f'{meta}.txt'), 'r') as file:
for line_no, line in enumerate(file):
if line_no == id:
return send_file(line.strip(), mimetype='image/jpeg')
app = Flask(__name__)
app.register_blueprint(blueprint)
# set CORS headers after request
@app.after_request
def after_request(response):
response.headers.add('Access-Control-Allow-Origin', '*')
response.headers.add('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS')
response.headers.add('Access-Control-Allow-Headers', 'Content-Type, Authorization')
return response
@app.errorhandler(KeyError)
@jsonify_response_data
def handle_key_error(e):
return ResponseData(message=str(e), data={})
if __name__ == '__main__':
root = os.getcwd()
if sys.platform.startswith('win'):
root = '\\\\?\\' + root
dataset_path = os.path.join(root, 'dataset')
if not os.path.exists(dataset_path):
os.mkdir(dataset_path)
base_path = os.path.join(dataset_path, 'base')
if not os.path.exists(base_path):
os.mkdir(base_path)
# index_path = os.path.join(dataset_path, 'index')
# if not os.path.exists(index_path):
# os.mkdir(index_path)
meta_path = os.path.join(dataset_path, 'meta')
if not os.path.exists(meta_path):
os.mkdir(meta_path)
meta_path = os.path.join(dataset_path, 'query')
if not os.path.exists(meta_path):
os.mkdir(meta_path)
search_path = os.path.join(dataset_path, 'search')
if not os.path.exists(search_path):
os.mkdir(search_path)
upload_path = os.path.join(root, 'uploads')
if not os.path.exists(upload_path):
os.mkdir(upload_path)
embedding_config = os.path.join(dataset_path, 'config.json')
if not os.path.exists(embedding_config):
with open(embedding_config, 'w'):
pass
delete_id_path = os.path.join(dataset_path, 'delete.ivecs')
fvecs_converter.to_fvecs(delete_id_path, [[]])
history = []
app.run(host='127.0.0.1', port=4523, debug=True)