Integrating OpenAI Whisper API with C# application

I’m building a voice assistant app in C# and need to integrate speech recognition using OpenAI’s Whisper model. Since there’s no direct C# library for Whisper, I’m trying to use IronPython to run Python code from within my C# application.

Here’s my Python script that works fine when run standalone:

import replicate
import os

os.environ['REPLICATE_API_TOKEN'] = 'r8_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

whisper_model = replicate.models.get("openai/whisper")
model_version = whisper_model.versions.get("30414ee7c4fffc37e260fcab7842b5be470b9b840f2b608f5baa9bbef9a259ed")

config = {
    'audio': open(r"C:\Audio\test_recording.wav", "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,
}

output = model_version.predict(**config)
print(output['segments'][0]['text'])

And here’s my C# code trying to execute the Python script:

using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
using System;

ScriptEngine pythonEngine = Python.CreateEngine();
ScriptSource scriptFile = pythonEngine.CreateScriptSourceFromFile(@"C:\MyApp\whisper_script.py");
ScriptScope scriptScope = pythonEngine.CreateScope();
scriptFile.Execute(scriptScope);
dynamic output = scriptScope.GetVariable("transcription");
Console.WriteLine(output);

However, I’m getting this error when running the C# application:

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)

How can I resolve this module import issue and successfully use Whisper in my C# application?

IronPython has major limitations with modern Python packages, especially anything with C extensions like the replicate library. Don’t wrestle with IronPython - just use Process.Start to run your Python script as a separate process from C#. Way more reliable and you get the full Python ecosystem without compatibility headaches. Pass the audio file path as a command line argument and grab the output through StandardOutput. I’ve done this in several production apps where I needed Python ML libraries with .NET. Performance hit is minimal since speech recognition isn’t real-time anyway, and you’ll save yourself the headache of trying to make complex Python dependencies work in IronPython’s limited runtime.

Ditch IronPython - it’s basically dead and can’t handle modern packages. Set up a Flask API to wrap your Whisper code and call it from C# with HttpClient. Much cleaner separation and no more PythonNet headaches.

IronPython can’t see packages you’ve installed in regular Python - it runs on .NET and has its own isolated package system. Even if you pip installed replicate, IronPython won’t find it because it’s not in IronPython’s search path. You could try installing packages specifically for IronPython, but replicate’s dependencies won’t work there anyway. I’ve run into this before when building similar apps. Your best bet is creating a small Python web service that handles the Whisper calls, then call it from your C# app. You get full Python compatibility without breaking your main C# application.

I’ve hit this same issue before. Skip IronPython and go with Python.NET instead. It runs actual CPython from your C# app, so all your pip packages (like replicate) work normally. Just grab it from NuGet and make sure Python’s installed on the target machine. Different syntax than IronPython, but way more reliable for complex dependencies. Another option: use file-based communication. Have your C# app drop the audio file path in a watched folder, kick off your Python script, then read the transcription from a text file. Keeps everything isolated and works great for non-realtime speech recognition.

The replicate library won’t work with IronPython - it relies on native extensions and HTTP libraries that IronPython can’t handle. Skip Replicate and use OpenAI’s API directly instead. They’ve got Whisper available through their REST API now, which is way easier to integrate with C#. Just use HttpClient to POST your audio file to their transcriptions endpoint. You’ll ditch the Python dependency completely and get better error handling in your C# app. The API call is simple and you get the same Whisper models without dealing with Python runtime in .NET.