From 100998dec9ef9de1d8f29f4542a54752c77891ea Mon Sep 17 00:00:00 2001 From: qingen Date: Thu, 17 Mar 2022 17:57:59 +0800 Subject: [PATCH] [wip][vec] update client, test=doc #1543 --- demos/audio_searching/README.md | 9 ++++++--- demos/audio_searching/README_cn.md | 7 +++++-- demos/audio_searching/docker-compose.yaml | 2 +- demos/audio_searching/src/encode.py | 4 +++- demos/audio_searching/src/main.py | 15 ++++++++------- demos/audio_searching/src/operations/count.py | 2 +- demos/audio_searching/src/operations/drop.py | 2 +- demos/audio_searching/src/operations/load.py | 6 +++--- demos/audio_searching/src/operations/search.py | 3 ++- 9 files changed, 30 insertions(+), 20 deletions(-) diff --git a/demos/audio_searching/README.md b/demos/audio_searching/README.md index a2d86df1..676b1361 100644 --- a/demos/audio_searching/README.md +++ b/demos/audio_searching/README.md @@ -87,8 +87,8 @@ Then to start the system server, and it provides HTTP backend services. 2022-03-07 17:39:14,865 | INFO | on.py | startup | 45 | Waiting for application startup. INFO: Application startup complete. 2022-03-07 17:39:14,866 | INFO | on.py | startup | 59 | Application startup complete. - INFO: Uvicorn running on http://127.0.0.1:8002 (Press CTRL+C to quit) - 2022-03-07 17:39:14,867 | INFO | server.py | _log_started_message | 206 | Uvicorn running on http://127.0.0.1:8002 (Press CTRL+C to quit) + INFO: Uvicorn running on http://0.0.0.0:8002 (Press CTRL+C to quit) + 2022-03-07 17:39:14,867 | INFO | server.py | _log_started_message | 206 | Uvicorn running on http://0.0.0.0:8002 (Press CTRL+C to quit) ``` ### 3. Usage @@ -124,7 +124,10 @@ Then to start the system server, and it provides HTTP backend services. ``` - GUI test (optional) - Navigate to 127.0.0.1:8068 in your browser to access the front-end interface. + Navigate to 127.0.0.1:8068 in your browser to access the front-end interface + + Note: If the browser and the service are not on the same machine, then the IP needs to be changed to the IP of the machine where the service is located, and the corresponding API_URL in docker-comemater. yaml needs to be changed and the service can be restarted + - Insert data Download the data and decompress it to a path named /home/speech/data. Then enter /home/speech/data in the address bar of the upload page to upload the data diff --git a/demos/audio_searching/README_cn.md b/demos/audio_searching/README_cn.md index ba5cb31d..6ba559c1 100644 --- a/demos/audio_searching/README_cn.md +++ b/demos/audio_searching/README_cn.md @@ -89,8 +89,8 @@ ffce340b3790 minio/minio:RELEASE.2020-12-03T00-03-10Z "/usr/bin/docker-ent…" 2022-03-07 17:39:14,865 | INFO | on.py | startup | 45 | Waiting for application startup. INFO: Application startup complete. 2022-03-07 17:39:14,866 | INFO | on.py | startup | 59 | Application startup complete. - INFO: Uvicorn running on http://127.0.0.1:8002 (Press CTRL+C to quit) - 2022-03-07 17:39:14,867 | INFO | server.py | _log_started_message | 206 | Uvicorn running on http://127.0.0.1:8002 (Press CTRL+C to quit) + INFO: Uvicorn running on http://0.0.0.0:8002 (Press CTRL+C to quit) + 2022-03-07 17:39:14,867 | INFO | server.py | _log_started_message | 206 | Uvicorn running on http://0.0.0.0:8002 (Press CTRL+C to quit) ``` ### 3. 测试方法 @@ -127,6 +127,9 @@ ffce340b3790 minio/minio:RELEASE.2020-12-03T00-03-10Z "/usr/bin/docker-ent…" - 前端测试(可选) 在浏览器中输入 127.0.0.1:8068 访问前端页面 + + 注:如果浏览器和服务不在同一台机器上,那么 IP需要修改成服务所在的机器 IP,并且docker-compose.yaml 中相应的 API_URL 也要修改,并重新起服务即可 + - 上传音频 下载数据并解压到一文件夹,假设为 /home/speech/data,那么在上传页面地址栏输入 /home/speech/data 进行数据上传 diff --git a/demos/audio_searching/docker-compose.yaml b/demos/audio_searching/docker-compose.yaml index 6e3f94ef..7d23ef8a 100644 --- a/demos/audio_searching/docker-compose.yaml +++ b/demos/audio_searching/docker-compose.yaml @@ -64,7 +64,7 @@ services: webclient: container_name: audio-webclient - image: iregistry.baidu-int.com/paddlespeech/audio-search-client:1.0 + image: iregistry.baidu-int.com/paddlespeech/audio-search-client:2.3 networks: app_net: ipv4_address: 172.16.23.13 diff --git a/demos/audio_searching/src/encode.py b/demos/audio_searching/src/encode.py index 391822c7..eba5c48c 100644 --- a/demos/audio_searching/src/encode.py +++ b/demos/audio_searching/src/encode.py @@ -11,6 +11,8 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +import os + import librosa import numpy as np from logs import LOGGER @@ -27,7 +29,7 @@ def get_audio_embedding(path): # TODO add infer/python interface to get embedding, now fake it by rand # vpr = ECAPATDNN(checkpoint_path=None, device='cuda') # embedding = vpr.inference(audio) - + np.random.seed(hash(os.path.basename(path)) % 1000000) embedding = np.random.rand(1, 2048) embedding = embedding / np.linalg.norm(embedding) embedding = embedding.tolist()[0] diff --git a/demos/audio_searching/src/main.py b/demos/audio_searching/src/main.py index 082fc651..db091a39 100644 --- a/demos/audio_searching/src/main.py +++ b/demos/audio_searching/src/main.py @@ -104,11 +104,12 @@ async def search_audio(request: Request, _, paths, distances = do_search(host, table_name, query_audio_path, MILVUS_CLI, MYSQL_CLI) names = [] - for i in paths: - names.append(os.path.basename(i)) + for path, score in zip(paths, distances): + names.append(os.path.basename(path)) + LOGGER.info(f"search result {path}, score {score}") 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]) + res = sorted(res.items(), key=lambda item: item[1][1], reverse=True) LOGGER.info("Successfully searched similar audio!") return res except Exception as e: @@ -126,12 +127,12 @@ async def search_local_audio(request: Request, _, paths, distances = do_search(host, table_name, query_audio_path, MILVUS_CLI, MYSQL_CLI) names = [] - for path, dist in zip(paths, distances): + for path, score in zip(paths, distances): names.append(os.path.basename(path)) - LOGGER.info(f"search result {path}, distance {dist}") + LOGGER.info(f"search result {path}, score {score}") 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]) + res = sorted(res.items(), key=lambda item: item[1][1], reverse=True) LOGGER.info("Successfully searched similar audio!") return res except Exception as e: @@ -164,4 +165,4 @@ async def drop_tables(table_name: str=None): if __name__ == '__main__': - uvicorn.run(app=app, host='127.0.0.1', port=8002) + uvicorn.run(app=app, host='0.0.0.0', port=8002) diff --git a/demos/audio_searching/src/operations/count.py b/demos/audio_searching/src/operations/count.py index 9a1f4208..eb4ba89d 100644 --- a/demos/audio_searching/src/operations/count.py +++ b/demos/audio_searching/src/operations/count.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. import sys - +sys.path.append("..") from config import DEFAULT_TABLE from logs import LOGGER diff --git a/demos/audio_searching/src/operations/drop.py b/demos/audio_searching/src/operations/drop.py index f8278ddd..44a6ea0d 100644 --- a/demos/audio_searching/src/operations/drop.py +++ b/demos/audio_searching/src/operations/drop.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. import sys - +sys.path.append("..") from config import DEFAULT_TABLE from logs import LOGGER diff --git a/demos/audio_searching/src/operations/load.py b/demos/audio_searching/src/operations/load.py index 792434fb..5e70aa09 100644 --- a/demos/audio_searching/src/operations/load.py +++ b/demos/audio_searching/src/operations/load.py @@ -16,9 +16,9 @@ import sys from diskcache import Cache from encode import get_audio_embedding - -from ..config import DEFAULT_TABLE -from ..logs import LOGGER +sys.path.append("..") +from config import DEFAULT_TABLE +from logs import LOGGER def get_audios(path): diff --git a/demos/audio_searching/src/operations/search.py b/demos/audio_searching/src/operations/search.py index 861fee01..253ab05a 100644 --- a/demos/audio_searching/src/operations/search.py +++ b/demos/audio_searching/src/operations/search.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. import sys - +sys.path.append("..") from config import DEFAULT_TABLE from config import TOP_K from encode import get_audio_embedding @@ -34,6 +34,7 @@ def do_search(host, table_name, audio_path, milvus_cli, mysql_cli): for i in range(len(paths)): tmp = "http://" + str(host) + "/data?audio_path=" + str(paths[i]) paths[i] = tmp + distances[i] = (1 - distances[i]) * 100 return vids, paths, distances except Exception as e: LOGGER.error(f"Error with search: {e}") -- GitLab