在C#中通过IronPython调用OpenAI Whisper遇replicate模块缺失错误求助
解决IronPython调用Python代码时找不到'replicate'模块的问题
问题背景
我是C#新手,想开发一款语音助手,用OpenAI Whisper实现语音转文本功能。因为没法直接在C#里用Whisper,所以选择用IronPython调用Python代码的方式实现。但运行时出现ImportException错误,提示找不到'replicate'模块。
我的Python代码
import replicate import os os.environ['REPLICATE_API_TOKEN'] = '73xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' model = replicate.models.get("openai/whisper") version = model.versions.get("30414ee7c4fffc37e260fcab7842b5be470b9b840f2b608f5baa9bbef9a259ed") inputs = { 'audio': open(r"C:\Users\JAHAN TEEGH\Documents\Sound recordings\Recording.mp3", "rb"), 'model': "large", 'temperature': 0, 'suppress_tokens': "-1", 'condition_on_previous_text': True, 'temperature_increment_on_fallback': 0.2, 'compression_ratio_threshold': 2.4, 'logprob_threshold': -1, 'no_speech_threshold': 0.6, } result = version.predict(**inputs) print(result['segments'][0]['text'])
我的C#代码
using IronPython.Hosting; using Microsoft.Scripting.Hosting; using System; ScriptEngine engine = Python.CreateEngine(); ScriptSource source = engine.CreateScriptSourceFromFile(@"D:\code\Dastak\stt\py.py"); ScriptScope scope = engine.CreateScope(); source.Execute(scope); dynamic result = scope.GetVariable("v"); Console.WriteLine(result);
错误信息
Unhandled exception. IronPython.Runtime.Exceptions.ImportException: No module named 'replicate' at Microsoft.Scripting.Runtime.LightExceptions.ThrowException(LightException lightEx) at Microsoft.Scripting.Runtime.LightExceptions.CheckAndThrow(Object value) at Microsoft.Scripting.Interpreter.FuncCallInstruction`2.Run(InterpretedFrame frame) at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame) at Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet](T0 arg0, T1 arg1) at IronPython.Compiler.PythonScriptCode.RunWorker(CodeContext ctx) at IronPython.Compiler.PythonScriptCode.Run(Scope scope) at IronPython.Compiler.RuntimeScriptCode.InvokeTarget(Scope scope) at IronPython.Compiler.RuntimeScriptCode.Run(Scope scope) at Microsoft.Scripting.SourceUnit.Execute(Scope scope, ErrorSink errorSink) at Microsoft.Scripting.SourceUnit.Execute(Scope scope) at Microsoft.Scripting.Hosting.ScriptSource.Execute(ScriptScope scope) at Program.<Main>$(String[] args) in D:\code\Dastak\stt\myApp\Program.cs:line 34
解决方法
1. 给IronPython环境安装replicate模块
IronPython有独立的Python环境,和系统默认Python不共享已安装的模块。你需要用IronPython自带的pip来安装replicate:
- 打开命令提示符,找到IronPython的安装目录(比如
C:\Program Files\IronPython 3.4) - 执行以下命令安装模块:
ipy -m pip install replicate
如果系统找不到ipy命令,直接用完整路径执行:
"C:\Program Files\IronPython 3.4\ipy.exe" -m pip install replicate
2. 让IronPython加载系统Python的第三方库(可选)
如果不想单独在IronPython环境安装模块,可以配置IronPython加载系统Python的site-packages目录。修改C#代码如下:
using IronPython.Hosting; using Microsoft.Scripting.Hosting; using System; ScriptEngine engine = Python.CreateEngine(); // 替换为你系统Python的site-packages路径,根据Python版本调整 string sitePackagesPath = @"C:\Users\JAHAN TEEGH\AppData\Local\Programs\Python\Python311\Lib\site-packages"; var searchPaths = engine.GetSearchPaths(); searchPaths.Add(sitePackagesPath); engine.SetSearchPaths(searchPaths); ScriptSource source = engine.CreateScriptSourceFromFile(@"D:\code\Dastak\stt\py.py"); ScriptScope scope = engine.CreateScope(); source.Execute(scope); dynamic result = scope.GetVariable("v"); Console.WriteLine(result);
同时修改Python代码,将结果赋值给变量v(否则C#代码会找不到这个变量):
import replicate import os os.environ['REPLICATE_API_TOKEN'] = '73xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' model = replicate.models.get("openai/whisper") version = model.versions.get("30414ee7c4fffc37e260fcab7842b5be470b9b840f2b608f5baa9bbef9a259ed") inputs = { 'audio': open(r"C:\Users\JAHAN TEEGH\Documents\Sound recordings\Recording.mp3", "rb"), 'model': "large", 'temperature': 0, 'suppress_tokens': "-1", 'condition_on_previous_text': True, 'temperature_increment_on_fallback': 0.2, 'compression_ratio_threshold': 2.4, 'logprob_threshold': -1, 'no_speech_threshold': 0.6, } result = version.predict(**inputs) v = result['segments'][0]['text'] # 添加这行赋值语句 print(v)
3. 额外注意事项
- 确保IronPython版本和replicate模块兼容,推荐使用3.4及以上版本的IronPython。
- 检查你的REPLICATE_API_TOKEN是否有效,避免后续调用模型时出现权限错误。
- 原C#代码中尝试获取变量
v,但原Python代码未定义该变量,必须修改Python代码添加赋值语句,否则会出现新的变量未找到错误。
内容的提问来源于stack exchange,提问作者yasin gahan tigh
相关产品推荐
相关产品推荐

