import ai.onnxruntime.*;
import java.util.*;
public class MultiModelApp {
private final OrtEnvironment env;
private final Map<String, OrtSession> sessions;
public MultiModelApp() throws OrtException {
// Create shared environment
env = OrtEnvironment.getEnvironment(
OrtLoggingLevel.ORT_LOGGING_LEVEL_INFO,
"MultiModel"
);
sessions = new HashMap<>();
// Create multiple sessions
OrtSession.SessionOptions opts = new OrtSession.SessionOptions();
opts.setOptimizationLevel(
OrtSession.SessionOptions.OptLevel.ALL_OPT
);
sessions.put("model1", env.createSession("model1.onnx", opts));
sessions.put("model2", env.createSession("model2.onnx", opts));
sessions.put("model3", env.createSession("model3.onnx", opts));
}
public OrtSession.Result runModel(String modelName,
Map<String, OnnxTensor> inputs)
throws OrtException {
OrtSession session = sessions.get(modelName);
if (session == null) {
throw new IllegalArgumentException("Unknown model: " + modelName);
}
return session.run(inputs);
}
public void close() {
for (OrtSession session : sessions.values()) {
session.close();
}
sessions.clear();
}
}