Python 学习整理2

python 报错 SyntaxError: 'return' outside function

PHPer 2025-08-26 16 0 0

{

"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 ...

立即注册
更新于:2025-08-26 10:37:20
    您需要登录后才可以评论。 立即注册
    相关内容

    python的selenium的用法整理

    selenium3定位元素(包括父子,兄弟)及常见操作

    使用Selenium自动验证滑块登录

    AI和Python 学习整理

    AudioSeparationGUI 对输入的音频,自动根据说话人进行分类 20250817

    SoloSpeech 项目安装和运行测试 20250817

    python 多项目部署 优先考虑用Anaconda

    espnet 声音分离

    ClearerVoice-Studio 安装测试遇到的问题,安装pysptk一直失败

    uvr5 bs roformer 爆显存以及关于huggingface的国内镜像hf-mirror.com

    用Kimi编程

    Studio One 调试插件Techivation AI-Rack 一键智能混音

    在hf-mirror.com模型镜像站用代码下载模型遇到报错 speaker-diarization模型不...

    用腾讯元宝编程

    Conda国内镜像源

    数字对象标识符 (DOI)

    在创建conda环境时,如果不指定Python版本,conda将不会默认安装Python。

    whisperx 的使用 音频处理

    Win10系统 Trae的使用 关于powershell报错: 因为在此系统上禁止运行脚本

    warp的使用

    AI编程工具比较

    FastApi

    python 报错整理2

    给出一份「Python 中常见 import 名称 与 pip install 名称不一致」的速查表

    Windows用Conda创建环境报错 CondaToSNonInteractiveError: Terms of Se...

    快速验证顺序是否被改

    Python 学习整理2

    fastapi-speaker-extractor whisperx 项目报错 ValueError: The chosen ...

    python报错 UnicodeEncodeError: 'gbk' codec can't encode character '\u...

    python whisperx 报错 in load_align_model raise ValueError(f'The ch...

    pyannote/embedding 模型是真难引入模型

    Trae 或者是我自己 莫名奇妙创建了个文件,影响了项目代码的运行。

    WhisperX 无法加载模型

    HUGGINGFACE_HUB_CACHE 设置错误导致的问题

    Trae的bug太多了,怪不得免费

    通义之旅

    通义之旅2

    目标说话人声音提取模型训练的思路

    python报错 can't convert cuda:0 device type tensor to numpy. Use Tenso...

    Expected all tensors to be on the same device, but found at least two ...

    腾讯元宝推荐的项目结构(音频处理项目)

    音频处理项目fse

    各种python 相关命令

    python 报错 SyntaxError: 'return' outside function

    python常用命令

    腾讯编程助手

    python一些扩展兼容安装的处理方案

    Python 常用命令整理

    魔塔社区 modelscope 常用命令

    推荐内容

    怎样使用V2Ray代理和SSTap玩如魔兽世界/绝地求生/LOL台服/战地3/黑色沙漠/彩...

    sstap游戏代理教程 从此玩如魔兽世界/绝地求生/LOL台服/战地3/黑色沙漠/彩虹六...

    BT磁力搜索网站汇总和找不到的资源

    什么是磁力链接,您如何使用?

    Z-Library:全球最大的数字图书馆/含打不开的解决方案/镜像

    使用V2Ray的mKCP协议加速游戏

    v2rayN已停止工作

    【车险课堂】什么是无赔款优待系数ncd,你“造”吗?