-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
executable file
·424 lines (340 loc) · 14.6 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
import json
import tempfile
import librosa
import numpy as np
from flask import request, jsonify, Flask
import os
from flask_cors import CORS
from pydub import AudioSegment
from mclbn256 import Fr
import api_key
import requests
import time
import logging
import traceback
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
app = Flask(__name__)
app.config["TEMPLATES_AUTO_RELOAD"] = True
CORS(app)
# mfcc extraction from augmented data
#extraction mel spectrogram
def extract_mel_spec(filename):
x,sr=librosa.load(filename,duration=3,offset=0.5)
# trim silence
xt, _ = librosa.effects.trim(x, top_db=60, frame_length=1024, hop_length=128)
# convert trimmed audio to melspectrogram
X = librosa.feature.melspectrogram(y=xt, sr=sr)
Xdb = librosa.power_to_db(X, ref=np.max)
Xdb = Xdb.reshape(1,128,-1)
return Xdb
def extract_bytes_addr(addr):
addr_int = int(addr, 0)
rep = Fr(addr_int)
ser = rep.serialize()
first_byte = int.from_bytes(ser[0:8], "little")
second_byte = int.from_bytes(ser[8:16], "little")
third_byte = int.from_bytes(ser[16:24], "little")
fourth_byte = int.from_bytes(ser[24:32], "little")
return [first_byte, second_byte, third_byte, fourth_byte]
def u64_to_fr(array):
reconstructed_bytes = array[0].to_bytes(8, byteorder='little') \
+ array[1].to_bytes(8, byteorder='little') \
+ array[2].to_bytes(8, byteorder='little') \
+ array[3].to_bytes(8, byteorder='little')
return Fr(reconstructed_bytes)
@app.route('/prove/', methods=["POST"])
def prove_task():
try:
audio_file = request.files['audio'].read()
with tempfile.NamedTemporaryFile(mode="wb+") as input_json_buffer:
with tempfile.NamedTemporaryFile(mode="wb+") as audio_input_buffer:
audio_input_buffer.write(audio_file)
audio_input_buffer.flush()
val = extract_mel_spec(audio_input_buffer.name)
# 0 pad 2nd dim to max size
if val.shape[2] < 130:
val = np.pad(
val, ((0, 0), (0, 0), (0, 130-val.shape[2])))
# truncate to max size
else:
val = val[:, :, :130]
# setup input.json
inp = {
"input_data": [val.flatten().tolist()],
}
inp_json_str = json.dumps(inp)
input_json_buffer.write(inp_json_str.encode('utf-8'))
# seek buffer to 0 before sending
input_json_buffer.seek(0)
print("updating artifacts with new input.json")
res = requests.put(
url=f"{api_key.ARCHON_URL}/artifact/idol-3?deployment=prod-1",
headers={"X-API-KEY": api_key.API_KEY},
files={
"data": input_json_buffer
}
)
res.raise_for_status()
data = json.loads(res.content.decode('utf-8'))
latest_uuid = data["latest_uuid"]
print("latest_uuid: ", latest_uuid)
# gen-witness and prove
try:
res = requests.post(
url=f"{api_key.ARCHON_URL}/recipe",
headers={
"X-API-KEY": api_key.API_KEY,
"Content-Type": "application/json",
},
json={
"commands": [
{
"artifact": "idol-3",
"binary": "ezkl",
"deployment": "prod-1",
"command": [
"gen-witness",
f"--data input_{latest_uuid}.json",
f"--compiled-circuit model.compiled",
f"--output witness_{latest_uuid}.json"
],
},
{
"artifact": "idol-3",
"deployment": "prod-1",
"binary": "ezkl",
"command": [
"prove",
f"--witness witness_{latest_uuid}.json",
f"--compiled-circuit model.compiled" ,
"--pk-path pk.key",
f"--proof-path proof_{latest_uuid}.json",
],
"output_path": [f"proof_{latest_uuid}.json"]
},
],
"data": [],
"response_settings": {
"callback": {
"url": api_key.CALLBACK_URL,
},
},
}
)
if res.status_code >= 400:
print(f"Error: HTTP {res.status_code}")
error_message = res.json().get('message', 'No error message provided')
print(f"Error message: {error_message}")
else:
print("Request successful")
print(res.json())
except Exception as e:
print(f"Error parsing JSON response: {str(e)}")
res.raise_for_status()
data = json.loads(res.content.decode('utf-8'))
print("full data: ", data)
print("id: ", str(data["id"]))
return jsonify({
"status": "ok",
"id": str(data["id"])
})
except EOFError as e:
logger.exception("EOFError occurred during audio file processing")
logger.error(f"Error message: {str(e)}")
logger.error(f"Error traceback: {traceback.format_exc()}")
return "Incomplete file sent, you may want to try resubmitting again.", 500
except Exception as e:
logger.exception("An error occurred during prove task")
logger.error(f"Error message: {str(e)}")
logger.error(f"Error traceback: {traceback.format_exc()}")
return repr(e), 500
@app.route('/callback/', methods=["POST"])
def callback():
try:
data = request.data
data_output = json.loads(data)
print(data_output)
if data_output[1]["status"] == "Errored":
with open(os.path.join("proof_data", str(data_output[0]['recipe_id'])) + ".json", "w") as f:
to_save = {
"status": "error",
"error": data_output[1]["logs"]
}
json.dump(to_save, f)
if 'prove' in data_output[1]['command']['command']:
# Extract the proof_path
proof_path = next(arg for arg in data_output[1]['command']['command'] if arg.startswith('--proof-path'))
proof_file = proof_path.split('--proof-path ')[1]
print(f"proof file in callback: {proof_file}")
else:
print("No proof file found in the data.")
with open(os.path.join("proof_data", str(data_output[0]['recipe_id'])) + ".json", "w") as f:
to_save = {
"status": "error",
"error": "No proof file found"
}
json.dump(to_save, f)
return jsonify({"status": "error", "message": "No proof file found in response"}), 400
res = requests.get(
url=f"{api_key.ARCHON_URL}/artifact/idol-3/file/{proof_file}?deployment=prod-1",
headers={ "X-API-KEY": api_key.API_KEY},
)
proof_data = res.json()
to_save = {
"status": "success",
"score_hex": proof_data["pretty_public_inputs"]["outputs"][0][0],
"score": proof_data["pretty_public_inputs"]["rescaled_outputs"][0][0],
"proof": proof_data["hex_proof"]
}
with open(os.path.join("proof_data", str(data_output[0]['recipe_id'])) + ".json", "w") as f:
json.dump(to_save, f)
return jsonify({
"status": "ok"
})
except Exception as e:
with open(os.path.join("proof_data", str(data_output[0]['recipe_id'])) + ".json", "w") as f:
to_save = {
"status": "error"
}
json.dump(to_save, f)
print(repr(e))
return repr(e), 500
@app.route('/recipe/<id>/')
def recipe(id):
saved_file = os.path.join("proof_data", str(id) + ".json")
if not os.path.exists(saved_file):
return "Not Found", 400
else:
with open(saved_file, "r") as f:
f = json.load(f)
return jsonify(f)
@app.route('/', methods=['GET'])
def index():
return jsonify({'status': 'ok', 'res': "CryptoIdol Pre-processing Server"})
if __name__ == '__main__':
with open(os.path.join("test_files", "angry.wav"), "rb") as audio_file:
with tempfile.NamedTemporaryFile(mode="wb+") as input_json_buffer:
val = extract_mel_spec(audio_file)
# 0 pad 2nd dim to max size
if val.shape[2] < 130:
val = np.pad(
val, ((0, 0), (0, 0), (0, 130-val.shape[2])))
# truncate to max size
else:
val = val[:, :, :130]
# setup input.json
inp = {
"input_data": [val.flatten().tolist()],
}
inp_json_str = json.dumps(inp)
input_json_buffer.write(inp_json_str.encode('utf-8'))
# seek buffer to 0 before sending
input_json_buffer.seek(0)
print("updating artifacts with new input.json")
headers = {
'X-API-KEY': api_key.API_KEY,
"Content-Type": "multipart/form-data"
}
inp_json_str = json.dumps(inp)
input_json_buffer.write(inp_json_str.encode('utf-8'))
# seek buffer to 0 before sending
input_json_buffer.seek(0)
headers = {
'X-API-KEY': api_key.API_KEY,
"Content-Type": "multipart/form-data"
}
res = requests.put(
url=f"{api_key.ARCHON_URL}/artifact/idol-3?deployment=prod-1",
headers={"X-API-KEY": api_key.API_KEY},
files={
"data": input_json_buffer,
}
)
res.raise_for_status()
data = json.loads(res.content.decode('utf-8'))
print(data)
latest_uuid = data["latest_uuid"]
print("latest_uuid: ", latest_uuid)
# gen-witness and prove
try:
res = requests.post(
url=f"{api_key.ARCHON_URL}/recipe",
headers={
"X-API-KEY": api_key.API_KEY,
"Content-Type": "application/json",
},
json={
"commands": [
{
"artifact": "idol-3",
"binary": "ezkl",
"deployment": "prod-1",
"command": [
"gen-witness",
f"--data input_{latest_uuid}.json",
f"--compiled-circuit model.compiled",
f"--output witness_{latest_uuid}.json"
],
},
{
"artifact": "idol-3",
"deployment": "prod-1",
"binary": "ezkl",
"command": [
"prove",
f"--witness witness_{latest_uuid}.json",
f"--compiled-circuit model.compiled" ,
"--pk-path pk.key",
f"--proof-path proof_{latest_uuid}.json",
],
"output_path": [f"proof_{latest_uuid}.json"]
},
],
"data": []
}
)
if res.status_code >= 400:
print(f"Error: HTTP {res.status_code}")
print(f"Error message: {res.content}")
else:
print("Request successful")
print(res.json())
except Exception as e:
print(f"Error parsing response: {str(e)}")
data = json.loads(res.content.decode('utf-8'))
print("full data: ", data)
print("id: ", data["id"])
cluster_id = data["id"]
query_count = 0
proof_data = None
while query_count < 60:
time.sleep(20)
# get job status
# pass id to client so client polls
res = requests.get(
url=f"{api_key.ARCHON_URL}/recipe/{str(cluster_id)}",
headers={
"X-API-KEY": api_key.API_KEY,
}
)
res.raise_for_status()
data = json.loads(res.content.decode('utf-8'))
print("witness data: ", data[0])
print("prove data: ", data[1])
print("prove status: ", data[1]['status'])
status = data[1]['status']
if status == "Complete":
print(data)
json_data = json.loads(data[1]['output'][0]['utf8_string'])
res.raise_for_status()
proof_data = res.json()
print("hex_proof: ", json_data["hex_proof"])
print("instances: ", json_data["pretty_public_inputs"]["outputs"])
break
if status == "Errored":
print("ERRORED")
print(data)
break
query_count += 1