Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added example of how to run Hugging Face models through Ollama using TC #8771

Merged

Conversation

ilopezluna
Copy link
Contributor

@ilopezluna ilopezluna commented Jun 11, 2024

Added example of how to use OllamaContainer to run Hugging Face models

Co-authored-by: @kiview

@ilopezluna ilopezluna marked this pull request as ready for review June 11, 2024 15:18
@ilopezluna ilopezluna requested a review from a team as a code owner June 11, 2024 15:18
Copy link
Member

@kiview kiview left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For adding reuse support, see suggestions (try if they compile like this 😅 ).

}

@Override
protected void containerIsStarted(InspectContainerResponse containerInfo) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
protected void containerIsStarted(InspectContainerResponse containerInfo) {
protected void containerIsStarted(InspectContainerResponse containerInfo, boolean reused) {

Support re-use.


@Override
protected void containerIsStarted(InspectContainerResponse containerInfo) {
super.containerIsStarted(containerInfo);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
super.containerIsStarted(containerInfo);
super.containerIsStarted(containerInfo, reused);
if (reused) {
return;
}

Don't download the model on re-use.

@ilopezluna ilopezluna requested a review from kiview June 13, 2024 06:22
@kiview kiview mentioned this pull request Jun 25, 2024
@eddumelendez eddumelendez added this to the next milestone Jun 26, 2024
Copy link
Member

@eddumelendez eddumelendez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @ilopezluna! Left some comments to improve the example.

Comment on lines 27 to 38
if (huggingFaceModel != null) {
this.setImage(
new ImageFromDockerfile()
.withDockerfileFromBuilder(builder -> {
builder
.from(this.getDockerImageName())
.run("apt-get update && apt-get upgrade -y && apt-get install -y python3-pip")
.run("pip install huggingface-hub")
.build();
})
);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can move those commands to containerIsStarted and use execInContainer directly.

Comment on lines 21 to 40
try (
OllamaContainer ollama = new OllamaContainer(
DockerImageName.parse(imageName).asCompatibleSubstituteFor("ollama/ollama:0.1.42")
)
) {
try {
ollama.start();
} catch (ContainerFetchException ex) {
// Create the image
try (
OllamaHuggingFaceContainer huggingFaceContainer = new OllamaHuggingFaceContainer(
imageName,
new OllamaHuggingFaceContainer.HuggingFaceModel(repository, model)
)
) {
huggingFaceContainer.start();
huggingFaceContainer.stop();
}
ollama.start();
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's use the docker client API to check if the image exists, the code will be more readable.

- use docker client to check if image already exist
- remove the gguf file after creating the ollama model
@eddumelendez eddumelendez merged commit 62a383b into testcontainers:main Jun 30, 2024
100 checks passed
@eddumelendez
Copy link
Member

Thanks @ilopezluna !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants