Skip to content

Commit 3ca3c5e

Browse files
style: format code with autopep8
Format code with autopep8 This commit fixes the style issues introduced in a9a88e7 according to the output from Autopep8. Details: https://app.deepsource.com/gh/avinashkranjan/Amazing-Python-Scripts/transform/5e3a3df7-dec0-48e8-9d6a-9e405e8908bb/
1 parent 225b071 commit 3ca3c5e

File tree

8 files changed

+73
-62
lines changed

8 files changed

+73
-62
lines changed

audio_detect/webserver/audio/encoder/encode.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ def get_audio_embedding(path):
1919
except Exception as e:
2020
print("error with embedding:", path)
2121
write_log(e, 1)
22-
return path, None
22+
return path, None

audio_detect/webserver/audio/indexer/index.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def create_table_milvus(client, table_name, dimension):
1818
collection_param = {
1919
'collection_name': table_name,
2020
'dimension': dimension,
21-
'index_file_size':2048,
21+
'index_file_size': 2048,
2222
'metric_type': METRIC_TYPE
2323
}
2424
status = client.create_collection(collection_param)
@@ -61,7 +61,8 @@ def delete_collection(client, table_name):
6161
def search_vectors(client, table_name, vectors, metric, top_k):
6262
search_param = {'nprobe': 32}
6363
try:
64-
status, res = client.search(collection_name=table_name, query_records=vectors, top_k=top_k, params=search_param)
64+
status, res = client.search(
65+
collection_name=table_name, query_records=vectors, top_k=top_k, params=search_param)
6566
print(status)
6667
return res
6768
except Exception as e:
@@ -84,4 +85,4 @@ def count_collection(client, table_name):
8485
return num
8586
except Exception as e:
8687
print("Milvus ERROR:", e)
87-
logging.error(e)
88+
logging.error(e)

audio_detect/webserver/audio/indexer/logs.py

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,34 @@
99
except ImportError:
1010
codecs = None
1111

12+
1213
class MultiprocessHandler(logging.FileHandler):
13-
def __init__(self,filename,when='D',backupCount=0,encoding=None,delay=False):
14+
def __init__(self, filename, when='D', backupCount=0, encoding=None, delay=False):
1415

1516
self.prefix = filename
1617
self.backupCount = backupCount
1718
self.when = when.upper()
1819
self.extMath = r"^\d{4}-\d{2}-\d{2}"
1920

2021
self.when_dict = {
21-
'S':"%Y-%m-%d-%H-%M-%S",
22-
'M':"%Y-%m-%d-%H-%M",
23-
'H':"%Y-%m-%d-%H",
24-
'D':"%Y-%m-%d"
22+
'S': "%Y-%m-%d-%H-%M-%S",
23+
'M': "%Y-%m-%d-%H-%M",
24+
'H': "%Y-%m-%d-%H",
25+
'D': "%Y-%m-%d"
2526
}
2627

2728
self.suffix = self.when_dict.get(when)
2829
if not self.suffix:
2930
raise ValueError(u"指定的日期间隔单位无效: %s" % self.when)
30-
31-
self.filefmt = os.path.join("logs","%s.%s" % (self.prefix,self.suffix))
32-
31+
32+
self.filefmt = os.path.join("logs", "%s.%s" %
33+
(self.prefix, self.suffix))
34+
3335
self.filePath = datetime.datetime.now().strftime(self.filefmt)
34-
36+
3537
_dir = os.path.dirname(self.filefmt)
3638
try:
37-
39+
3840
if not os.path.exists(_dir):
3941
os.makedirs(_dir)
4042
except Exception:
@@ -45,29 +47,30 @@ def __init__(self,filename,when='D',backupCount=0,encoding=None,delay=False):
4547
if codecs is None:
4648
encoding = None
4749

48-
logging.FileHandler.__init__(self,self.filePath,'a+',encoding,delay)
50+
logging.FileHandler.__init__(
51+
self, self.filePath, 'a+', encoding, delay)
4952

5053
def shouldChangeFileToWrite(self):
51-
54+
5255
_filePath = datetime.datetime.now().strftime(self.filefmt)
53-
56+
5457
if _filePath != self.filePath:
5558
self.filePath = _filePath
5659
return True
5760
return False
5861

5962
def doChangeFile(self):
60-
63+
6164
self.baseFilename = os.path.abspath(self.filePath)
62-
65+
6366
if self.stream:
64-
67+
6568
self.stream.close()
66-
69+
6770
self.stream = None
6871

6972
if not self.delay:
70-
73+
7174
self.stream = self._open()
7275

7376
if self.backupCount > 0:
@@ -78,7 +81,7 @@ def doChangeFile(self):
7881

7982
def getFilesToDelete(self):
8083

81-
dirName,_ = os.path.split(self.baseFilename)
84+
dirName, _ = os.path.split(self.baseFilename)
8285
fileNames = os.listdir(dirName)
8386
result = []
8487

@@ -90,10 +93,9 @@ def getFilesToDelete(self):
9093
suffix = fileName[plen:]
9194

9295
if re.compile(self.extMath).match(suffix):
93-
result.append(os.path.join(dirName,fileName))
96+
result.append(os.path.join(dirName, fileName))
9497
result.sort()
9598

96-
9799
if len(result) < self.backupCount:
98100
result = []
99101
else:
@@ -105,8 +107,8 @@ def emit(self, record):
105107
try:
106108
if self.shouldChangeFileToWrite():
107109
self.doChangeFile()
108-
logging.FileHandler.emit(self,record)
109-
except (KeyboardInterrupt,SystemExit):
110+
logging.FileHandler.emit(self, record)
111+
except (KeyboardInterrupt, SystemExit):
110112
raise
111113
except:
112114
self.handleError(record)
@@ -121,18 +123,14 @@ def write_log(log_message, level=0):
121123
stream_handler.setLevel(logging.INFO)
122124
stream_handler.setFormatter(fmt)
123125

124-
125126
log_name = "app.log"
126127
file_handler = MultiprocessHandler(log_name, when='D', backupCount=7)
127128
file_handler.setLevel(logging.INFO)
128129
file_handler.setFormatter(fmt)
129130

130-
131131
logger.addHandler(stream_handler)
132132
logger.addHandler(file_handler)
133133
if level:
134134
logger.error(log_message)
135135
else:
136136
logger.info(log_message)
137-
138-

audio_detect/webserver/audio/indexer/tools.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,31 @@
88
def connect_mysql():
99
try:
1010
# conn = pymysql.connect(host="127.0.0.1",user="root",port=3306,password="123456",database="mysql", local_infile=True)
11-
conn = pymysql.connect(host=MYSQL_HOST,user=MYSQL_USER,port=MYSQL_PORT,password=MYSQL_PWD,database=MYSQL_DB, local_infile=True)
11+
conn = pymysql.connect(host=MYSQL_HOST, user=MYSQL_USER, port=MYSQL_PORT,
12+
password=MYSQL_PWD, database=MYSQL_DB, local_infile=True)
1213
return conn
1314
except Exception as e:
1415
print("MYSQL ERROR: connect failed", e)
15-
write_log(e,1)
16+
write_log(e, 1)
1617

1718

18-
def create_table_mysql(conn,cursor, table_name):
19-
sql = "create table if not exists " + table_name + "(milvus_id bigint, audio_id text, index ix_milvus (milvus_id));"
19+
def create_table_mysql(conn, cursor, table_name):
20+
sql = "create table if not exists " + table_name + \
21+
"(milvus_id bigint, audio_id text, index ix_milvus (milvus_id));"
2022
try:
2123
cursor.execute(sql)
2224
print("MYSQL create table.")
2325
except Exception as e:
2426
print("MYSQL ERROR:", sql, e)
25-
write_log(e,1)
27+
write_log(e, 1)
2628

2729

2830
def search_by_milvus_ids(conn, cursor, ids, table_name):
2931
str_ids = str(ids)
30-
str_ids = str(str_ids).replace('[','').replace(']','')
31-
sql = "select audio_id from " + table_name + " where milvus_id in (" + str_ids + ") order by field (milvus_id," + str_ids + ");"
32+
str_ids = str(str_ids).replace('[', '').replace(']', '')
33+
sql = "select audio_id from " + table_name + \
34+
" where milvus_id in (" + str_ids + \
35+
") order by field (milvus_id," + str_ids + ");"
3236
# print(sql)
3337
try:
3438
cursor.execute(sql)
@@ -38,24 +42,26 @@ def search_by_milvus_ids(conn, cursor, ids, table_name):
3842
return results
3943
except Exception as e:
4044
print("MYSQL ERROR:", sql, e)
41-
write_log(e,1)
45+
write_log(e, 1)
4246

4347

4448
def load_data_to_mysql(conn, cursor, table_name, file_name):
45-
sql = "load data local infile '" + file_name + "' into table " + table_name + " fields terminated by ',';"
49+
sql = "load data local infile '" + file_name + \
50+
"' into table " + table_name + " fields terminated by ',';"
4651
try:
4752
cursor.execute(sql)
4853
conn.commit()
4954
print("MYSQL load table.")
5055
except Exception as e:
5156
print("MYSQL ERROR:", sql, e)
52-
write_log(e,1)
57+
write_log(e, 1)
5358
finally:
5459
if os.path.exists(file_name):
5560
with open(file_name) as f:
5661
line = f.readlines()
5762
print("-----------MySQL insert info--------len:" + str(len(line)))
58-
write_log("-----------MySQL insert info--------len:" + str(len(line)))
63+
write_log(
64+
"-----------MySQL insert info--------len:" + str(len(line)))
5965
os.remove(file_name)
6066

6167

@@ -66,7 +72,7 @@ def delete_table(conn, cursor, table_name):
6672
print("MYSQL delete table.")
6773
except:
6874
print("MYSQL ERROR:", sql, e)
69-
write_log(e,1)
75+
write_log(e, 1)
7076

7177

7278
def delete_all_data(conn, cursor, table_name):
@@ -77,7 +83,7 @@ def delete_all_data(conn, cursor, table_name):
7783
print("MYSQL delete all data.")
7884
except:
7985
print("MYSQL ERROR:", sql, e)
80-
write_log(e,1)
86+
write_log(e, 1)
8187

8288

8389
def count_table(conn, cursor, table_name):
@@ -89,4 +95,4 @@ def count_table(conn, cursor, table_name):
8995
return results[0][0]
9096
except Exception as e:
9197
print("MYSQL ERROR:", sql, e)
92-
write_log(e,1)
98+
write_log(e, 1)

audio_detect/webserver/audio/service/count.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from audio.indexer.index import milvus_client, count_collection
66
from audio.indexer.tools import count_table
77

8+
89
def do_count_table(index_client, conn, cursor, table_name):
910
if not table_name:
1011
table_name = DEFAULT_TABLE

audio_detect/webserver/audio/service/insert.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def init_table(index_client, conn, cursor, table_name):
1919

2020

2121
def get_ids_file(ids_milvus, ids_audio, file_name):
22-
with open(file_name,'w') as f:
22+
with open(file_name, 'w') as f:
2323
for i in range(len(ids_audio)):
2424
line = str(ids_milvus[i]) + "," + ids_audio[i] + '\n'
2525
f.write(line)
@@ -48,7 +48,7 @@ def get_wav_info(audio_path, wav):
4848

4949

5050
def do_insert_audio(index_client, conn, cursor, table_name, audio_path):
51-
51+
5252
try:
5353
init_table(index_client, conn, cursor, table_name)
5454
wavs = os.listdir(audio_path)
@@ -58,14 +58,15 @@ def do_insert_audio(index_client, conn, cursor, table_name, audio_path):
5858
for wav in wavs:
5959
# print("---wav:", wav)
6060
if ".wav" in wav:
61-
ids_wav, vectors_wav = get_audio_embedding(audio_path + '/' + wav)
61+
ids_wav, vectors_wav = get_audio_embedding(
62+
audio_path + '/' + wav)
6263
if vectors_wav:
6364
get_spectorgram(audio_path, wav)
6465
embeddings.append(vectors_wav)
6566
ids_audio.append(ids_wav)
6667
# print("len of embeddings", len(embeddings))
6768
_, ids_milvus = insert_vectors(index_client, table_name, embeddings)
68-
69+
6970
file_name = str(uuid.uuid1()) + ".csv"
7071
get_ids_file(ids_milvus, ids_audio, file_name)
7172
print("load data to mysql:", file_name)

audio_detect/webserver/audio/service/search.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ def do_search_audio(index_client, conn, cursor, table_name, filename):
1313
_, vectors_audio = get_audio_embedding(filename)
1414
# print(vectors_audio)
1515

16-
results = search_vectors(index_client, table_name, [vectors_audio], METRIC_TYPE, TOP_K)
16+
results = search_vectors(index_client, table_name, [
17+
vectors_audio], METRIC_TYPE, TOP_K)
1718

1819
re_ids_img = []
1920
milvus_ids = [x.id for x in results[0]]

audio_detect/webserver/main.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,12 @@ def unzip_file(zip_src, dst_dir):
5454
async def audio_count_table_api(table_name: str = None):
5555
try:
5656
index_client, conn, cursor = audio_init_conn()
57-
rows_milvus, rows_mysql = audio_count_table(index_client, conn, cursor, table_name)
57+
rows_milvus, rows_mysql = audio_count_table(
58+
index_client, conn, cursor, table_name)
5859
return {'status': True, 'msg': {'rows_milvus': rows_milvus, 'rows_mysql': rows_mysql}}, 200
5960
except Exception as e:
6061
logging.error(e)
61-
return {'status': False, 'msg':e}, 400
62+
return {'status': False, 'msg': e}, 400
6263

6364

6465
@app.get('/getAudio')
@@ -68,7 +69,7 @@ async def audio_endpoint(audio: str):
6869
return FileResponse(audio)
6970
except Exception as e:
7071
logging.error(e)
71-
return {'status': False, 'msg':e}, 400
72+
return {'status': False, 'msg': e}, 400
7273

7374

7475
@app.get('/getSpectrogram')
@@ -78,7 +79,7 @@ async def spectrogram_endpoint(image: str):
7879
return FileResponse(image)
7980
except Exception as e:
8081
logging.error(e)
81-
return {'status': False, 'msg':e}, 400
82+
return {'status': False, 'msg': e}, 400
8283

8384

8485
@app.post('/audio/insert')
@@ -91,17 +92,19 @@ async def do_insert_audio_api(file: bytes = File(...), table_name: str = None):
9192

9293
os.makedirs(audio_UPLOAD_PATH + "/" + table_name)
9394
fname_path = audio_UPLOAD_PATH + "/" + table_name + "/" + "demo_audio.zip"
94-
with open(fname_path,'wb') as f:
95+
with open(fname_path, 'wb') as f:
9596
f.write(file)
9697

97-
audio_path = unzip_file(fname_path, audio_UPLOAD_PATH + "/" + table_name)
98+
audio_path = unzip_file(
99+
fname_path, audio_UPLOAD_PATH + "/" + table_name)
98100
os.remove(fname_path)
99101

100-
info = audio_insert_audio(index_client, conn, cursor, table_name, audio_UPLOAD_PATH + "/" + table_name)
102+
info = audio_insert_audio(
103+
index_client, conn, cursor, table_name, audio_UPLOAD_PATH + "/" + table_name)
101104
return {'status': True, 'msg': info}, 200
102105
except Exception as e:
103106
logging.error(e)
104-
return {'status': False, 'msg':e}, 400
107+
return {'status': False, 'msg': e}, 400
105108

106109

107110
@app.post('/audio/search')
@@ -114,8 +117,9 @@ async def do_search_audio_api(request: Request, audio: UploadFile = File(...), t
114117

115118
index_client, conn, cursor = audio_init_conn()
116119
host = request.headers['host']
117-
milvus_ids, milvus_distance, audio_ids = audio_search_audio(index_client, conn, cursor, table_name, filename)
118-
120+
milvus_ids, milvus_distance, audio_ids = audio_search_audio(
121+
index_client, conn, cursor, table_name, filename)
122+
119123
results = []
120124
for i in range(len(milvus_ids)):
121125
re = {
@@ -128,8 +132,7 @@ async def do_search_audio_api(request: Request, audio: UploadFile = File(...), t
128132
return {'status': True, 'msg': results}, 200
129133
except Exception as e:
130134
logging.error(e)
131-
return {'status': False, 'msg':e}, 400
132-
135+
return {'status': False, 'msg': e}, 400
133136

134137

135138
if __name__ == '__main__':

0 commit comments

Comments
 (0)