zhendery
commited on
Commit
·
6c0eeeb
1
Parent(s):
18ca1ba
feat: wav包含子文件夹 feat:新接口-强行重下wav
Browse files
api.py
CHANGED
|
@@ -40,8 +40,18 @@ def download_voices(bForce=False):
|
|
| 40 |
if not os.path.exists(voices_dir):
|
| 41 |
os.makedirs(voices_dir)
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
# 如果没有 .pmt 文件,尝试从远程下载
|
| 46 |
voice_download_url = os.getenv("VOICE_DOWNLOAD_URL")
|
| 47 |
|
|
@@ -195,17 +205,27 @@ def delete_voice(name: str, token: str = Depends(verify_token)):
|
|
| 195 |
@app.get("/voices")
|
| 196 |
def get_voices(token: str = Depends(verify_token)):
|
| 197 |
download_voices()
|
| 198 |
-
# 获取所有 .pmt
|
| 199 |
-
pmt_files = [
|
| 200 |
-
|
| 201 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
# 确保对应的 .wav 文件也存在
|
| 203 |
valid_voices = []
|
| 204 |
-
for voice in
|
| 205 |
if os.path.exists(f"./voices/{voice}.wav"):
|
| 206 |
valid_voices.append(voice)
|
| 207 |
return {"voices": valid_voices}
|
| 208 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
|
| 210 |
# ↓↓↓↓↓↓↓↓↓无需验证↓↓↓↓↓↓↓↓
|
| 211 |
@app.get("/")
|
|
|
|
| 40 |
if not os.path.exists(voices_dir):
|
| 41 |
os.makedirs(voices_dir)
|
| 42 |
|
| 43 |
+
# 改进:递归查找所有子目录中的.pmt文件,找到一个就返回
|
| 44 |
+
pmt_file_found = None
|
| 45 |
+
for root, dirs, files in os.walk(voices_dir):
|
| 46 |
+
for file in files:
|
| 47 |
+
if file.endswith(".pmt"):
|
| 48 |
+
pmt_file_found = file
|
| 49 |
+
break
|
| 50 |
+
if pmt_file_found:
|
| 51 |
+
break
|
| 52 |
+
|
| 53 |
+
# 如果没有找到.pmt文件且需要强制下载
|
| 54 |
+
if bForce or pmt_file_found is None:
|
| 55 |
# 如果没有 .pmt 文件,尝试从远程下载
|
| 56 |
voice_download_url = os.getenv("VOICE_DOWNLOAD_URL")
|
| 57 |
|
|
|
|
| 205 |
@app.get("/voices")
|
| 206 |
def get_voices(token: str = Depends(verify_token)):
|
| 207 |
download_voices()
|
| 208 |
+
# 获取所有 .pmt 文件(递归搜索子目录)
|
| 209 |
+
pmt_files = []
|
| 210 |
+
for root, dirs, files in os.walk("./voices"):
|
| 211 |
+
for file in files:
|
| 212 |
+
if file.endswith(".pmt"):
|
| 213 |
+
# 获取文件相对于当前工作目录的完整相对路径
|
| 214 |
+
full_path = os.path.join(root, file)
|
| 215 |
+
relative_path = os.path.relpath(full_path, "./voices")
|
| 216 |
+
pmt_files.append(relative_path[:-4])
|
| 217 |
+
|
| 218 |
# 确保对应的 .wav 文件也存在
|
| 219 |
valid_voices = []
|
| 220 |
+
for voice in pmt_files:
|
| 221 |
if os.path.exists(f"./voices/{voice}.wav"):
|
| 222 |
valid_voices.append(voice)
|
| 223 |
return {"voices": valid_voices}
|
| 224 |
|
| 225 |
+
@app.post("/re_download_voices")
|
| 226 |
+
def download_voices_file(token: str = Depends(verify_token)):
|
| 227 |
+
download_voices(True)
|
| 228 |
+
|
| 229 |
|
| 230 |
# ↓↓↓↓↓↓↓↓↓无需验证↓↓↓↓↓↓↓↓
|
| 231 |
@app.get("/")
|