Skip to content

Commit

Permalink
HuggingFaceTokenizer 改成离线加载,解决在线被墙的问题。
Browse files Browse the repository at this point in the history
  • Loading branch information
mymagicpower committed Nov 14, 2024
1 parent a415dae commit d34a3cb
Show file tree
Hide file tree
Showing 35 changed files with 341 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public StableDiffusionControlNetPipeline(String root, String model, Device devic
.optModelPath(Paths.get(root + "text_encoder.pt"))
.optEngine("PyTorch")
.optProgress(new ProgressBar())
.optTranslator(new TextEncoder())
.optTranslator(new TextEncoder(root))
.optDevice(device)
.build();
this.textEncoderModel = textEncoderCriteria.loadModel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import ai.djl.translate.NoBatchifyTranslator;
import ai.djl.translate.TranslatorContext;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Arrays;

public class TextEncoder implements NoBatchifyTranslator<String, NDList> {
Expand All @@ -18,20 +20,36 @@ public class TextEncoder implements NoBatchifyTranslator<String, NDList> {

HuggingFaceTokenizer tokenizer;

String rootPath;

public TextEncoder(String rootPath) {
this.rootPath = rootPath;
}

@Override
public void prepare(TranslatorContext ctx) throws IOException {
// sentence-transformers/msmarco-distilbert-dot-v5
// openai/clip-vit-large-patch14
// openai/clip-vit-base-patch32
// https://huggingface.co/sentence-transformers/msmarco-distilbert-dot-v5
// https://huggingface.co/runwayml/stable-diffusion-v1-5/blob/main/tokenizer/tokenizer_config.json
// tokenizer =
// HuggingFaceTokenizer.builder()
// .optPadding(true)
// .optPadToMaxLength()
// .optMaxLength(MAX_LENGTH)
// .optTruncation(true)
// .optTokenizerName("openai/clip-vit-large-patch14")
// .build();

String modelPath = rootPath + "clip-vit-large-patch14";
tokenizer =
HuggingFaceTokenizer.builder()
.optPadding(true)
.optPadToMaxLength()
.optMaxLength(MAX_LENGTH)
.optTokenizerPath(Paths.get(modelPath))
.optTruncation(true)
.optTokenizerName("openai/clip-vit-large-patch14")
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public StableDiffusionControlNetPipeline(String root, String model, Device devic
.optModelPath(Paths.get(root + "text_encoder.pt"))
.optEngine("PyTorch")
.optProgress(new ProgressBar())
.optTranslator(new TextEncoder())
.optTranslator(new TextEncoder(root))
.optDevice(device)
.build();
this.textEncoderModel = textEncoderCriteria.loadModel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import ai.djl.translate.NoBatchifyTranslator;
import ai.djl.translate.TranslatorContext;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Arrays;

public class TextEncoder implements NoBatchifyTranslator<String, NDList> {
Expand All @@ -18,20 +20,36 @@ public class TextEncoder implements NoBatchifyTranslator<String, NDList> {

HuggingFaceTokenizer tokenizer;

String rootPath;

public TextEncoder(String rootPath) {
this.rootPath = rootPath;
}

@Override
public void prepare(TranslatorContext ctx) throws IOException {
// sentence-transformers/msmarco-distilbert-dot-v5
// openai/clip-vit-large-patch14
// openai/clip-vit-base-patch32
// https://huggingface.co/sentence-transformers/msmarco-distilbert-dot-v5
// https://huggingface.co/runwayml/stable-diffusion-v1-5/blob/main/tokenizer/tokenizer_config.json
// tokenizer =
// HuggingFaceTokenizer.builder()
// .optPadding(true)
// .optPadToMaxLength()
// .optMaxLength(MAX_LENGTH)
// .optTruncation(true)
// .optTokenizerName("openai/clip-vit-large-patch14")
// .build();

String modelPath = rootPath + "clip-vit-large-patch14";
tokenizer =
HuggingFaceTokenizer.builder()
.optPadding(true)
.optPadToMaxLength()
.optMaxLength(MAX_LENGTH)
.optTokenizerPath(Paths.get(modelPath))
.optTruncation(true)
.optTokenizerName("openai/clip-vit-large-patch14")
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public StableDiffusionControlNetPipeline(String root, String model, Device devic
.optModelPath(Paths.get(root + "text_encoder.pt"))
.optEngine("PyTorch")
.optProgress(new ProgressBar())
.optTranslator(new TextEncoder())
.optTranslator(new TextEncoder(root))
.optDevice(device)
.build();
this.textEncoderModel = textEncoderCriteria.loadModel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import ai.djl.translate.NoBatchifyTranslator;
import ai.djl.translate.TranslatorContext;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Arrays;

public class TextEncoder implements NoBatchifyTranslator<String, NDList> {
Expand All @@ -18,20 +20,36 @@ public class TextEncoder implements NoBatchifyTranslator<String, NDList> {

HuggingFaceTokenizer tokenizer;

String rootPath;

public TextEncoder(String rootPath) {
this.rootPath = rootPath;
}

@Override
public void prepare(TranslatorContext ctx) throws IOException {
// sentence-transformers/msmarco-distilbert-dot-v5
// openai/clip-vit-large-patch14
// openai/clip-vit-base-patch32
// https://huggingface.co/sentence-transformers/msmarco-distilbert-dot-v5
// https://huggingface.co/runwayml/stable-diffusion-v1-5/blob/main/tokenizer/tokenizer_config.json
// tokenizer =
// HuggingFaceTokenizer.builder()
// .optPadding(true)
// .optPadToMaxLength()
// .optMaxLength(MAX_LENGTH)
// .optTruncation(true)
// .optTokenizerName("openai/clip-vit-large-patch14")
// .build();

String modelPath = rootPath + "clip-vit-large-patch14";
tokenizer =
HuggingFaceTokenizer.builder()
.optPadding(true)
.optPadToMaxLength()
.optMaxLength(MAX_LENGTH)
.optTokenizerPath(Paths.get(modelPath))
.optTruncation(true)
.optTokenizerName("openai/clip-vit-large-patch14")
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public StableDiffusionControlNetPipeline(String root, String model, Device devic
.optModelPath(Paths.get(root + "text_encoder.pt"))
.optEngine("PyTorch")
.optProgress(new ProgressBar())
.optTranslator(new TextEncoder())
.optTranslator(new TextEncoder(root))
.optDevice(device)
.build();
this.textEncoderModel = textEncoderCriteria.loadModel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import ai.djl.translate.NoBatchifyTranslator;
import ai.djl.translate.TranslatorContext;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Arrays;

public class TextEncoder implements NoBatchifyTranslator<String, NDList> {
Expand All @@ -18,20 +20,36 @@ public class TextEncoder implements NoBatchifyTranslator<String, NDList> {

HuggingFaceTokenizer tokenizer;

String rootPath;

public TextEncoder(String rootPath) {
this.rootPath = rootPath;
}

@Override
public void prepare(TranslatorContext ctx) throws IOException {
// sentence-transformers/msmarco-distilbert-dot-v5
// openai/clip-vit-large-patch14
// openai/clip-vit-base-patch32
// https://huggingface.co/sentence-transformers/msmarco-distilbert-dot-v5
// https://huggingface.co/runwayml/stable-diffusion-v1-5/blob/main/tokenizer/tokenizer_config.json
// tokenizer =
// HuggingFaceTokenizer.builder()
// .optPadding(true)
// .optPadToMaxLength()
// .optMaxLength(MAX_LENGTH)
// .optTruncation(true)
// .optTokenizerName("openai/clip-vit-large-patch14")
// .build();

String modelPath = rootPath + "clip-vit-large-patch14";
tokenizer =
HuggingFaceTokenizer.builder()
.optPadding(true)
.optPadToMaxLength()
.optMaxLength(MAX_LENGTH)
.optTokenizerPath(Paths.get(modelPath))
.optTruncation(true)
.optTokenizerName("openai/clip-vit-large-patch14")
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public StableDiffusionControlNetPipeline(String root, String model, Device devic
.optModelPath(Paths.get(root + "text_encoder.pt"))
.optEngine("PyTorch")
.optProgress(new ProgressBar())
.optTranslator(new TextEncoder())
.optTranslator(new TextEncoder(root))
.optDevice(device)
.build();
this.textEncoderModel = textEncoderCriteria.loadModel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import ai.djl.translate.NoBatchifyTranslator;
import ai.djl.translate.TranslatorContext;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Arrays;

public class TextEncoder implements NoBatchifyTranslator<String, NDList> {
Expand All @@ -18,20 +20,36 @@ public class TextEncoder implements NoBatchifyTranslator<String, NDList> {

HuggingFaceTokenizer tokenizer;

String rootPath;

public TextEncoder(String rootPath) {
this.rootPath = rootPath;
}

@Override
public void prepare(TranslatorContext ctx) throws IOException {
// sentence-transformers/msmarco-distilbert-dot-v5
// openai/clip-vit-large-patch14
// openai/clip-vit-base-patch32
// https://huggingface.co/sentence-transformers/msmarco-distilbert-dot-v5
// https://huggingface.co/runwayml/stable-diffusion-v1-5/blob/main/tokenizer/tokenizer_config.json
// tokenizer =
// HuggingFaceTokenizer.builder()
// .optPadding(true)
// .optPadToMaxLength()
// .optMaxLength(MAX_LENGTH)
// .optTruncation(true)
// .optTokenizerName("openai/clip-vit-large-patch14")
// .build();

String modelPath = rootPath + "clip-vit-large-patch14";
tokenizer =
HuggingFaceTokenizer.builder()
.optPadding(true)
.optPadToMaxLength()
.optMaxLength(MAX_LENGTH)
.optTokenizerPath(Paths.get(modelPath))
.optTruncation(true)
.optTokenizerName("openai/clip-vit-large-patch14")
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public StableDiffusionControlNetPipeline(String root, String model, Device devic
.optModelPath(Paths.get(root + "text_encoder.pt"))
.optEngine("PyTorch")
.optProgress(new ProgressBar())
.optTranslator(new TextEncoder())
.optTranslator(new TextEncoder(root))
.optDevice(device)
.build();
this.textEncoderModel = textEncoderCriteria.loadModel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import ai.djl.translate.NoBatchifyTranslator;
import ai.djl.translate.TranslatorContext;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Arrays;

public class TextEncoder implements NoBatchifyTranslator<String, NDList> {
Expand All @@ -18,20 +20,36 @@ public class TextEncoder implements NoBatchifyTranslator<String, NDList> {

HuggingFaceTokenizer tokenizer;

String rootPath;

public TextEncoder(String rootPath) {
this.rootPath = rootPath;
}

@Override
public void prepare(TranslatorContext ctx) throws IOException {
// sentence-transformers/msmarco-distilbert-dot-v5
// openai/clip-vit-large-patch14
// openai/clip-vit-base-patch32
// https://huggingface.co/sentence-transformers/msmarco-distilbert-dot-v5
// https://huggingface.co/runwayml/stable-diffusion-v1-5/blob/main/tokenizer/tokenizer_config.json
// tokenizer =
// HuggingFaceTokenizer.builder()
// .optPadding(true)
// .optPadToMaxLength()
// .optMaxLength(MAX_LENGTH)
// .optTruncation(true)
// .optTokenizerName("openai/clip-vit-large-patch14")
// .build();

String modelPath = rootPath + "clip-vit-large-patch14";
tokenizer =
HuggingFaceTokenizer.builder()
.optPadding(true)
.optPadToMaxLength()
.optMaxLength(MAX_LENGTH)
.optTokenizerPath(Paths.get(modelPath))
.optTruncation(true)
.optTokenizerName("openai/clip-vit-large-patch14")
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public StableDiffusionControlNetPipeline(String root, String model, Device devic
.optModelPath(Paths.get(root + "text_encoder.pt"))
.optEngine("PyTorch")
.optProgress(new ProgressBar())
.optTranslator(new TextEncoder())
.optTranslator(new TextEncoder(root))
.optDevice(device)
.build();
this.textEncoderModel = textEncoderCriteria.loadModel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import ai.djl.translate.NoBatchifyTranslator;
import ai.djl.translate.TranslatorContext;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Arrays;

public class TextEncoder implements NoBatchifyTranslator<String, NDList> {
Expand All @@ -18,20 +20,36 @@ public class TextEncoder implements NoBatchifyTranslator<String, NDList> {

HuggingFaceTokenizer tokenizer;

String rootPath;

public TextEncoder(String rootPath) {
this.rootPath = rootPath;
}

@Override
public void prepare(TranslatorContext ctx) throws IOException {
// sentence-transformers/msmarco-distilbert-dot-v5
// openai/clip-vit-large-patch14
// openai/clip-vit-base-patch32
// https://huggingface.co/sentence-transformers/msmarco-distilbert-dot-v5
// https://huggingface.co/runwayml/stable-diffusion-v1-5/blob/main/tokenizer/tokenizer_config.json
// tokenizer =
// HuggingFaceTokenizer.builder()
// .optPadding(true)
// .optPadToMaxLength()
// .optMaxLength(MAX_LENGTH)
// .optTruncation(true)
// .optTokenizerName("openai/clip-vit-large-patch14")
// .build();

String modelPath = rootPath + "clip-vit-large-patch14";
tokenizer =
HuggingFaceTokenizer.builder()
.optPadding(true)
.optPadToMaxLength()
.optMaxLength(MAX_LENGTH)
.optTokenizerPath(Paths.get(modelPath))
.optTruncation(true)
.optTokenizerName("openai/clip-vit-large-patch14")
.build();
}

Expand Down
Loading

0 comments on commit d34a3cb

Please sign in to comment.