|-转 python 报错 SyntaxError: 'return' outside function
{
"1b38f5a6230ac636944ef3df2a7b18a9aec530f931fed681d942267186102368": {
"short_code": "1uqjLT",
"label": 0.0,
"filepath": "outputs\\preprocessed\\1b38f5a6230ac636944ef3df2a7b18a9aec530f931fed681d942267186102368.wav",
"created_at": "2025-08-26T10:23:35.373116+08:00"
},
"0386ab79e9163b22a2b5e25a84d3443d79b5cc08d7f26d87da3bc68de8340544": {
"short_code": "1uqcmD",
"label": 0.67,
"filepath": "outputs\\preprocessed\\0386ab79e9163b22a2b5e25a84d3443d79b5cc08d7f26d87da3bc68de8340544.wav",
"created_at": "2025-08-26T03:22:45.509993+08:00"
},
"856616252eccfa58e713998fdeaaf8a2acbd896d15977bf780884ca73635cc76": {
"short_code": "1uqiqv",
"label": 0.0,
"filepath": "outputs\\preprocessed\\processed_tmpo9v0me_w_1uqiqv.wav",
"created_at": "2025-08-26T09:52:01.721592+08:00"
}
}
2025-08-26 09:14:02,637 --- app.api.audio_router --- ERROR --- 处理音频时发生错误:
"D:\python\fastapi-speaker-extractor\app\services\audio_processor.py", line 75 return ^ SyntaxError: 'return' outside function 2025-08-26 09:14:02,631 --- app.api.audio_router --- WARNING --- 无法从主应用获取模型: WhisperX 模型未加载 2025-08-26 09:14:02,631 --- app.api.audio_router --- INFO --- 未提供配置参数,使用默认配置 2025-08-26 09:14:02,632 --- app.api.audio_router --- INFO --- 收到请求,文件=西游记多人16秒.wav,config={} 2025-08-26 09:14:02,637 --- app.api.audio_router --- ERROR --- 处理音频时发生错误: Traceback (most recent call last): File "D:\python\fastapi-speaker-extractor\app\api\audio_router.py", line 72, in extract_speaker from app.services.audio_processor import process_audio File "D:\python\fastapi-speaker-extractor\app\services\audio_processor.py", line 75 return ^ SyntaxError: 'return' outside function
Traceback (most recent call last):
File "D:\python\fastapi-speaker-extractor\app\api\audio_router.py", line 72, in extract_speaker
from app.services.audio_processor import process_audio
File "D:\python\fastapi-speaker-extractor\app\services\audio_processor.py", line 75
return
^
# 导入自定义模块 try: from logger import logger from app.utils.embedding_model_middleware import get_embedding_middleware, ModelType from app.utils.onnx_model_loader import ONNXPyannoteEmbedding from app.utils.error_handler import ErrorHandler except ImportError as e: print(f"导入工具模块失败: {e}") return
代码 return的这个写法不知道是错哪里了。
通义灵码:
到目前为止,我们已经解决了以下几个问题:
JSON解析错误 - 通过将 config_value 参数改为可选并改进解析逻辑
类型注解兼容性问题 - 通过使用 Union[Path, str] 替代 Path | str 以支持 Python 3.8+
模型加载问题 - 通过修改模型加载逻辑确保默认加载
目录创建问题 - 通过将字符串路径转换为 Path 对象
现在您的应用程序应该能够正常处理音频文件了。
20250826 0926
G:\ProgramData\miniconda3\envs\speaker-extractor\lib\site-packages\pydub\audio_segment.py:678: RuntimeWarning: coroutine 'UploadFile.seek' was never awaited file.seek(0) RuntimeWarning: Enable tracemalloc to get the object allocation traceback G:\ProgramData\miniconda3\envs\speaker-extractor\lib\site-packages\pydub\audio_segment.py:678: RuntimeWarning: coroutine 'UploadFile.read' was never awaited file.seek(0) RuntimeWarning: Enable tracemalloc to get the object allocation traceback G:\ProgramData\miniconda3\envs\speaker-extractor\lib\site-packages\pydub\utils.py:268: RuntimeWarning: coroutine 'UploadFile.seek' was never awaited file.seek(0) RuntimeWarning: Enable tracemalloc to get the object allocation traceback 2025-08-26 09:26:28,638 --- fastapi-speaker-extractor --- ERROR --- ❌ 处理音频时发生错误: a bytes-like object is required, not 'coroutine' Traceback (most recent call last): File "D:\python\fastapi-speaker-extractor\app\services\audio_processor.py", line 148, in process_audio
22222222
2025-08-26 09:29:05,415 --- fastapi-speaker-extractor --- INFO --- 📊 计算目标说话人占比 ✅ [缓存命中] 从缓存加载目标说话人 embedding: inputs/target_reference/target_reference_embedding\121e918b12035ef25024d8d9e2c8f39b.npy 🎧 正在分析音频,总长度: 3.00 秒,分段长度: 1.5 秒 2025-08-26 09:29:05,422 --- fastapi-speaker-extractor --- ERROR --- ❌ 处理音频时发生错误: 'float' object cannot be interpreted as an integer Traceback (most recent call last):
222222222222
2025-08-26 09:30:22,560 --- fastapi-speaker-extractor --- INFO --- 临时文件已清理: C:\Users\ADMINI~1\AppData\Local\Temp\tmpybyujt8p.wav 2025-08-26 09:30:22,561 --- app.api.audio_router --- ERROR --- 处理音频时发生错误: Traceback (most recent call last): File "D:\python\fastapi-speaker-extractor\app\services\audio_processor.py", line 188, in process_audio proportion = calculate_target_proportion( File "D:\python\fastapi-speaker-extractor\app\utils\audio_utils.py", line 118, in calculate_target_proportion for i in range(num_segments): TypeError: 'float' object cannot be interpreted as an integer
33333
raise RuntimeError(f"音频处理失败: {str(e)}") RuntimeError: 音频处理失败: 'WindowsPath' object has no attribute 'cpu' INFO: 127.0.0.1:14903 - "POST /api/audio/speaker/extract/ HTTP/1.1" 500 Internal Server Error ERROR: Exception in ASGI application Traceback (most recent call last): File "D:\python\fastapi-speaker-extractor\app\services\audio_processor.py", line 223, in process_audio sha256_hash=compute_audio_sha256(output_filepath), File "D:\python\fastapi-speaker-extractor\app\utils\common_utils.py", line 208, in compute_audio_sha256 audio_np = audio_tensor.cpu().numpy() # 转 CPU 并转为 NumPy AttributeError: 'WindowsPath' object has no attribute 'cpu'
5555555 ...