def audio_path(audio_path): # Get the audio file try: LOGGER.info(("Successfully load audio: {}".format(audio_path))) return FileResponse(audio_path) except Exception as e: LOGGER.error("upload audio error: {}".format(e)) return {'status': False, 'msg': e}, 400
def mols_img(mols_path): # Get the molecular image file try: LOGGER.info(("Successfully load molecular image: {}".format(mols_path))) return FileResponse(UPLOAD_PATH + '/' + mols_path + '.png') except Exception as e: LOGGER.error("upload image error: {}".format(e)) return {'status': False, 'msg': e}, 400
async def drop_tables(table_name: str = None): # Delete the collection of Milvus and MySQL try: status = do_drop(table_name, MILVUS_CLI, MYSQL_CLI) LOGGER.info("Successfully drop tables in Milvus and MySQL!") return status except Exception as e: LOGGER.error(e) return {'status': False, 'msg': e}, 400
async def count_audio(table_name: str = None): # Returns the total number of vectors in the system try: num = do_count(table_name, MILVUS_CLI) LOGGER.info("Successfully count the number of data!") return num except Exception as e: LOGGER.error(e) return {'status': False, 'msg': e}, 400
async def load_data(item: Item): # Insert all the data under the file path to Milvus/MySQL try: total_num = do_load(item.Table, item.File, MODEL, MILVUS_CLI, MYSQL_CLI) LOGGER.info("Successfully loaded data, total count: {}".format(total_num)) return {'status': True, 'msg': "Successfully loaded data!"} except Exception as e: LOGGER.error(e) return {'status': False, 'msg': e}, 400
async def search_data(Table: str = None, Mol: str = None): # Search the upload image in Milvus/MySQL try: # Save the upload data to server. ids, paths, distances = do_search(Table, Mol, MODEL, MILVUS_CLI, MYSQL_CLI) res = dict(zip(paths, zip(ids, distances))) res = sorted(res.items(), key=lambda item: item[1]) LOGGER.info("Successfully searched similar data!") return res except Exception as e: LOGGER.error(e) return {'status': False, 'msg': e}, 400
async def search_data(request: Request, item: Item_search): # Search the upload image in Milvus/MySQL try: # Save the upload data to server. ids, paths, distances = do_search(item.Table, item.Mol, item.Num, MODEL, MILVUS_CLI, MYSQL_CLI) host = request.headers['host'] for i in range(len(ids)): tmp = "http://" + str(host) + "/data?mols_path=" + str(ids[i]) ids[i] = tmp res = dict(zip(paths, zip(ids, distances))) res = sorted(res.items(), key=lambda item: item[1][1]) LOGGER.info("Successfully searched similar data!") return res except Exception as e: LOGGER.error(e) return {'status': False, 'msg': e}, 400
async def search_audio(request: Request, Table: str = None, audio: UploadFile = File(...)): # Search the uploaded audio in Milvus/MySQL try: # Save the upload data to server. content = await audio.read() audio_path = os.path.join(UPLOAD_PATH, audio.filename) with open(audio_path, "wb+") as f: f.write(content) host = request.headers['host'] ids, paths, distances = do_search(host, Table, audio_path, MODEL, MILVUS_CLI, MYSQL_CLI) names = [] for i in paths: names.append(os.path.basename(i)) res = dict(zip(paths, zip(names, distances))) # Sort results by distance metric, closest distances first res = sorted(res.items(), key=lambda item: item[1][1]) LOGGER.info("Successfully searched similar audio!") return res except Exception as e: LOGGER.error(e) return {'status': False, 'msg': e}, 400
app = FastAPI() app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"]) MODEL = None MILVUS_CLI = MilvusHelper() MYSQL_CLI = MySQLHelper() # Mkdir 'tmp/audio-data' if not os.path.exists(UPLOAD_PATH): os.makedirs(UPLOAD_PATH) LOGGER.info("mkdir the path:{} ".format(UPLOAD_PATH)) @app.get('/data') def audio_path(audio_path): # Get the audio file try: LOGGER.info(("Successfully load audio: {}".format(audio_path))) return FileResponse(audio_path) except Exception as e: LOGGER.error("upload audio error: {}".format(e)) return {'status': False, 'msg': e}, 400 @app.get('/progress') def get_progress():