Commit
·
3bd45f6
1
Parent(s):
3649225
added files
Browse files- README.md +3 -0
- handler.py +53 -0
- requirements.txt +5 -0
- sharded/config.json +256 -0
- sharded/pytorch_model-00001-of-00004.bin +3 -0
- sharded/pytorch_model-00002-of-00004.bin +3 -0
- sharded/pytorch_model-00003-of-00004.bin +3 -0
- sharded/pytorch_model-00004-of-00004.bin +3 -0
- sharded/pytorch_model.bin.index.json +0 -0
README.md
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
---
|
handler.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# handler.py
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import pipeline, AutoProcessor, Blip2ForConditionalGeneration
|
| 4 |
+
import os
|
| 5 |
+
"""import base64
|
| 6 |
+
from io import BytesIO
|
| 7 |
+
from PIL import Image"""
|
| 8 |
+
|
| 9 |
+
# check for GPU
|
| 10 |
+
device = 0 if torch.cuda.is_available() else -1
|
| 11 |
+
|
| 12 |
+
class EndpointHandler():
|
| 13 |
+
def __init__(self, path=""):
|
| 14 |
+
blip2_proc = AutoProcessor.from_pretrained("Salesforce/blip2-opt-2.7b")
|
| 15 |
+
#blip2 = Blip2ForConditionalGeneration.from_pretrained("sharded", device_map="auto", load_in_8bit=True)
|
| 16 |
+
blip2 = Blip2ForConditionalGeneration.from_pretrained(os.path.join(path, "sharded"), device_map="auto", load_in_8bit=True)
|
| 17 |
+
#translator = pipeline("translation",model="Helsinki-NLP/opus-mt-en-de",device=device)
|
| 18 |
+
|
| 19 |
+
def __call__(self, data):
|
| 20 |
+
# deserialize incomin request
|
| 21 |
+
"""b64_img = data.pop("b64", data)
|
| 22 |
+
lang = data.pop("lang", None)
|
| 23 |
+
decode = data.pop("decode", None)
|
| 24 |
+
|
| 25 |
+
#prepare image
|
| 26 |
+
im_bytes = base64.b64decode(b64_img) # im_bytes is a binary image
|
| 27 |
+
im_file = BytesIO(im_bytes) # convert image to file-like object
|
| 28 |
+
image = Image.open(im_file).convert("RGB")
|
| 29 |
+
output = {}
|
| 30 |
+
inputs = self.blip2_proc(image, return_tensors="pt").to(device, torch.float16)
|
| 31 |
+
#nucleus vs beam sampling
|
| 32 |
+
if decode == None or decode == "beam":
|
| 33 |
+
generated_ids = self.blip2.generate(**inputs, max_new_tokens=20)
|
| 34 |
+
prediction = self.blip2_proc.batch_decode(generated_ids, skip_special_tokens=True)[0].strip()
|
| 35 |
+
#english vs german caption
|
| 36 |
+
if lang != None or lang == "de":
|
| 37 |
+
translation = self.translator(prediction)
|
| 38 |
+
output["beam"] = translation[0]
|
| 39 |
+
else:
|
| 40 |
+
output["beam"] = prediction
|
| 41 |
+
if decode != None or decode == "nucleus":
|
| 42 |
+
generated_ids = self.blip2.generate(**inputs, max_new_tokens=20)
|
| 43 |
+
prediction = self.blip2_proc.batch_decode(generated_ids, skip_special_tokens=True,do_sample=True)[0].strip()
|
| 44 |
+
#english vs german caption
|
| 45 |
+
if lang != None or lang == "de":
|
| 46 |
+
translation = self.translator(prediction)
|
| 47 |
+
output["nucleus"] = translation[0]
|
| 48 |
+
else:
|
| 49 |
+
output["nucleus"] = prediction
|
| 50 |
+
|
| 51 |
+
# postprocess the prediction
|
| 52 |
+
return output"""
|
| 53 |
+
return 73
|
requirements.txt
ADDED
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_commit_hash": "ca9e6c21b0ae56818ab19c8c873eb1bb5cfae2f8",
|
| 3 |
+
"_name_or_path": "Salesforce/blip2-opt-2.7b",
|
| 4 |
+
"architectures": [
|
| 5 |
+
"Blip2ForConditionalGeneration"
|
| 6 |
+
],
|
| 7 |
+
"initializer_factor": 1.0,
|
| 8 |
+
"initializer_range": 0.02,
|
| 9 |
+
"model_type": "blip-2",
|
| 10 |
+
"num_query_tokens": 32,
|
| 11 |
+
"qformer_config": {
|
| 12 |
+
"_name_or_path": "",
|
| 13 |
+
"add_cross_attention": false,
|
| 14 |
+
"architectures": null,
|
| 15 |
+
"attention_probs_dropout_prob": 0.1,
|
| 16 |
+
"bad_words_ids": null,
|
| 17 |
+
"begin_suppress_tokens": null,
|
| 18 |
+
"bos_token_id": null,
|
| 19 |
+
"chunk_size_feed_forward": 0,
|
| 20 |
+
"classifier_dropout": null,
|
| 21 |
+
"cross_attention_frequency": 2,
|
| 22 |
+
"cross_attention_hidden_size": null,
|
| 23 |
+
"decoder_start_token_id": null,
|
| 24 |
+
"diversity_penalty": 0.0,
|
| 25 |
+
"do_sample": false,
|
| 26 |
+
"early_stopping": false,
|
| 27 |
+
"encoder_hidden_size": 1408,
|
| 28 |
+
"encoder_no_repeat_ngram_size": 0,
|
| 29 |
+
"eos_token_id": null,
|
| 30 |
+
"exponential_decay_length_penalty": null,
|
| 31 |
+
"finetuning_task": null,
|
| 32 |
+
"forced_bos_token_id": null,
|
| 33 |
+
"forced_eos_token_id": null,
|
| 34 |
+
"hidden_act": "gelu",
|
| 35 |
+
"hidden_dropout_prob": 0.1,
|
| 36 |
+
"hidden_size": 768,
|
| 37 |
+
"id2label": {
|
| 38 |
+
"0": "LABEL_0",
|
| 39 |
+
"1": "LABEL_1"
|
| 40 |
+
},
|
| 41 |
+
"initializer_range": 0.02,
|
| 42 |
+
"intermediate_size": 3072,
|
| 43 |
+
"is_decoder": false,
|
| 44 |
+
"is_encoder_decoder": false,
|
| 45 |
+
"label2id": {
|
| 46 |
+
"LABEL_0": 0,
|
| 47 |
+
"LABEL_1": 1
|
| 48 |
+
},
|
| 49 |
+
"layer_norm_eps": 1e-12,
|
| 50 |
+
"length_penalty": 1.0,
|
| 51 |
+
"max_length": 20,
|
| 52 |
+
"max_position_embeddings": 512,
|
| 53 |
+
"min_length": 0,
|
| 54 |
+
"model_type": "blip_2_qformer",
|
| 55 |
+
"no_repeat_ngram_size": 0,
|
| 56 |
+
"num_attention_heads": 12,
|
| 57 |
+
"num_beam_groups": 1,
|
| 58 |
+
"num_beams": 1,
|
| 59 |
+
"num_hidden_layers": 12,
|
| 60 |
+
"num_return_sequences": 1,
|
| 61 |
+
"output_attentions": false,
|
| 62 |
+
"output_hidden_states": false,
|
| 63 |
+
"output_scores": false,
|
| 64 |
+
"pad_token_id": 0,
|
| 65 |
+
"position_embedding_type": "absolute",
|
| 66 |
+
"prefix": null,
|
| 67 |
+
"problem_type": null,
|
| 68 |
+
"pruned_heads": {},
|
| 69 |
+
"remove_invalid_values": false,
|
| 70 |
+
"repetition_penalty": 1.0,
|
| 71 |
+
"return_dict": true,
|
| 72 |
+
"return_dict_in_generate": false,
|
| 73 |
+
"sep_token_id": null,
|
| 74 |
+
"suppress_tokens": null,
|
| 75 |
+
"task_specific_params": null,
|
| 76 |
+
"temperature": 1.0,
|
| 77 |
+
"tf_legacy_loss": false,
|
| 78 |
+
"tie_encoder_decoder": false,
|
| 79 |
+
"tie_word_embeddings": true,
|
| 80 |
+
"tokenizer_class": null,
|
| 81 |
+
"top_k": 50,
|
| 82 |
+
"top_p": 1.0,
|
| 83 |
+
"torch_dtype": null,
|
| 84 |
+
"torchscript": false,
|
| 85 |
+
"transformers_version": "4.27.0.dev0",
|
| 86 |
+
"typical_p": 1.0,
|
| 87 |
+
"use_bfloat16": false,
|
| 88 |
+
"vocab_size": 30522
|
| 89 |
+
},
|
| 90 |
+
"text_config": {
|
| 91 |
+
"_name_or_path": "facebook/opt-2.7b",
|
| 92 |
+
"_remove_final_layer_norm": false,
|
| 93 |
+
"activation_dropout": 0.0,
|
| 94 |
+
"activation_function": "relu",
|
| 95 |
+
"add_cross_attention": false,
|
| 96 |
+
"architectures": [
|
| 97 |
+
"OPTForCausalLM"
|
| 98 |
+
],
|
| 99 |
+
"attention_dropout": 0.0,
|
| 100 |
+
"bad_words_ids": null,
|
| 101 |
+
"begin_suppress_tokens": null,
|
| 102 |
+
"bos_token_id": 2,
|
| 103 |
+
"chunk_size_feed_forward": 0,
|
| 104 |
+
"cross_attention_hidden_size": null,
|
| 105 |
+
"decoder_start_token_id": null,
|
| 106 |
+
"diversity_penalty": 0.0,
|
| 107 |
+
"do_layer_norm_before": true,
|
| 108 |
+
"do_sample": false,
|
| 109 |
+
"dropout": 0.1,
|
| 110 |
+
"early_stopping": false,
|
| 111 |
+
"enable_bias": true,
|
| 112 |
+
"encoder_no_repeat_ngram_size": 0,
|
| 113 |
+
"eos_token_id": 50118,
|
| 114 |
+
"exponential_decay_length_penalty": null,
|
| 115 |
+
"ffn_dim": 10240,
|
| 116 |
+
"finetuning_task": null,
|
| 117 |
+
"forced_bos_token_id": null,
|
| 118 |
+
"forced_eos_token_id": null,
|
| 119 |
+
"hidden_size": 2560,
|
| 120 |
+
"id2label": {
|
| 121 |
+
"0": "LABEL_0",
|
| 122 |
+
"1": "LABEL_1"
|
| 123 |
+
},
|
| 124 |
+
"init_std": 0.02,
|
| 125 |
+
"is_decoder": false,
|
| 126 |
+
"is_encoder_decoder": false,
|
| 127 |
+
"label2id": {
|
| 128 |
+
"LABEL_0": 0,
|
| 129 |
+
"LABEL_1": 1
|
| 130 |
+
},
|
| 131 |
+
"layer_norm_elementwise_affine": true,
|
| 132 |
+
"layerdrop": 0.0,
|
| 133 |
+
"length_penalty": 1.0,
|
| 134 |
+
"max_length": 20,
|
| 135 |
+
"max_position_embeddings": 2048,
|
| 136 |
+
"min_length": 0,
|
| 137 |
+
"model_type": "opt",
|
| 138 |
+
"no_repeat_ngram_size": 0,
|
| 139 |
+
"num_attention_heads": 32,
|
| 140 |
+
"num_beam_groups": 1,
|
| 141 |
+
"num_beams": 1,
|
| 142 |
+
"num_hidden_layers": 32,
|
| 143 |
+
"num_return_sequences": 1,
|
| 144 |
+
"output_attentions": false,
|
| 145 |
+
"output_hidden_states": false,
|
| 146 |
+
"output_scores": false,
|
| 147 |
+
"pad_token_id": 1,
|
| 148 |
+
"prefix": "</s>",
|
| 149 |
+
"problem_type": null,
|
| 150 |
+
"pruned_heads": {},
|
| 151 |
+
"remove_invalid_values": false,
|
| 152 |
+
"repetition_penalty": 1.0,
|
| 153 |
+
"return_dict": true,
|
| 154 |
+
"return_dict_in_generate": false,
|
| 155 |
+
"sep_token_id": null,
|
| 156 |
+
"suppress_tokens": null,
|
| 157 |
+
"task_specific_params": null,
|
| 158 |
+
"temperature": 1.0,
|
| 159 |
+
"tf_legacy_loss": false,
|
| 160 |
+
"tie_encoder_decoder": false,
|
| 161 |
+
"tie_word_embeddings": true,
|
| 162 |
+
"tokenizer_class": null,
|
| 163 |
+
"top_k": 50,
|
| 164 |
+
"top_p": 1.0,
|
| 165 |
+
"torch_dtype": "float16",
|
| 166 |
+
"torchscript": false,
|
| 167 |
+
"transformers_version": "4.27.0.dev0",
|
| 168 |
+
"typical_p": 1.0,
|
| 169 |
+
"use_bfloat16": false,
|
| 170 |
+
"use_cache": true,
|
| 171 |
+
"vocab_size": 50272,
|
| 172 |
+
"word_embed_proj_dim": 2560
|
| 173 |
+
},
|
| 174 |
+
"torch_dtype": "float16",
|
| 175 |
+
"transformers_version": null,
|
| 176 |
+
"use_decoder_only_language_model": true,
|
| 177 |
+
"vision_config": {
|
| 178 |
+
"_name_or_path": "",
|
| 179 |
+
"add_cross_attention": false,
|
| 180 |
+
"architectures": null,
|
| 181 |
+
"attention_dropout": 0.0,
|
| 182 |
+
"bad_words_ids": null,
|
| 183 |
+
"begin_suppress_tokens": null,
|
| 184 |
+
"bos_token_id": null,
|
| 185 |
+
"chunk_size_feed_forward": 0,
|
| 186 |
+
"cross_attention_hidden_size": null,
|
| 187 |
+
"decoder_start_token_id": null,
|
| 188 |
+
"diversity_penalty": 0.0,
|
| 189 |
+
"do_sample": false,
|
| 190 |
+
"dropout": 0.0,
|
| 191 |
+
"early_stopping": false,
|
| 192 |
+
"encoder_no_repeat_ngram_size": 0,
|
| 193 |
+
"eos_token_id": null,
|
| 194 |
+
"exponential_decay_length_penalty": null,
|
| 195 |
+
"finetuning_task": null,
|
| 196 |
+
"forced_bos_token_id": null,
|
| 197 |
+
"forced_eos_token_id": null,
|
| 198 |
+
"hidden_act": "gelu",
|
| 199 |
+
"hidden_size": 1408,
|
| 200 |
+
"id2label": {
|
| 201 |
+
"0": "LABEL_0",
|
| 202 |
+
"1": "LABEL_1"
|
| 203 |
+
},
|
| 204 |
+
"image_size": 224,
|
| 205 |
+
"initializer_factor": 1.0,
|
| 206 |
+
"initializer_range": 1e-10,
|
| 207 |
+
"intermediate_size": 6144,
|
| 208 |
+
"is_decoder": false,
|
| 209 |
+
"is_encoder_decoder": false,
|
| 210 |
+
"label2id": {
|
| 211 |
+
"LABEL_0": 0,
|
| 212 |
+
"LABEL_1": 1
|
| 213 |
+
},
|
| 214 |
+
"layer_norm_eps": 1e-05,
|
| 215 |
+
"length_penalty": 1.0,
|
| 216 |
+
"max_length": 20,
|
| 217 |
+
"min_length": 0,
|
| 218 |
+
"model_type": "blip_2_vision_model",
|
| 219 |
+
"no_repeat_ngram_size": 0,
|
| 220 |
+
"num_attention_heads": 16,
|
| 221 |
+
"num_beam_groups": 1,
|
| 222 |
+
"num_beams": 1,
|
| 223 |
+
"num_channels": 3,
|
| 224 |
+
"num_hidden_layers": 39,
|
| 225 |
+
"num_return_sequences": 1,
|
| 226 |
+
"output_attentions": false,
|
| 227 |
+
"output_hidden_states": false,
|
| 228 |
+
"output_scores": false,
|
| 229 |
+
"pad_token_id": null,
|
| 230 |
+
"patch_size": 14,
|
| 231 |
+
"prefix": null,
|
| 232 |
+
"problem_type": null,
|
| 233 |
+
"projection_dim": 512,
|
| 234 |
+
"pruned_heads": {},
|
| 235 |
+
"qkv_bias": true,
|
| 236 |
+
"remove_invalid_values": false,
|
| 237 |
+
"repetition_penalty": 1.0,
|
| 238 |
+
"return_dict": true,
|
| 239 |
+
"return_dict_in_generate": false,
|
| 240 |
+
"sep_token_id": null,
|
| 241 |
+
"suppress_tokens": null,
|
| 242 |
+
"task_specific_params": null,
|
| 243 |
+
"temperature": 1.0,
|
| 244 |
+
"tf_legacy_loss": false,
|
| 245 |
+
"tie_encoder_decoder": false,
|
| 246 |
+
"tie_word_embeddings": true,
|
| 247 |
+
"tokenizer_class": null,
|
| 248 |
+
"top_k": 50,
|
| 249 |
+
"top_p": 1.0,
|
| 250 |
+
"torch_dtype": null,
|
| 251 |
+
"torchscript": false,
|
| 252 |
+
"transformers_version": "4.27.0.dev0",
|
| 253 |
+
"typical_p": 1.0,
|
| 254 |
+
"use_bfloat16": false
|
| 255 |
+
}
|
| 256 |
+
}
|
sharded/pytorch_model-00001-of-00004.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:624383716d5a5748ef9e83488f778341f8e5f8978915ade8003aa305cc2c786c
|
| 3 |
+
size 1997731454
|
sharded/pytorch_model-00002-of-00004.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:0594c5c7e27886b250723325ea0d7f4e0a683181ecab6140e936f61dffece57c
|
| 3 |
+
size 1977729785
|
sharded/pytorch_model-00003-of-00004.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:9bec0dabfa98e0426308e7c94f2b304aabf1753f366df4c9e14f5a1f24c3e811
|
| 3 |
+
size 1993212341
|
sharded/pytorch_model-00004-of-00004.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:531b9bab50eac9f310b27a663e56f989d3c087f4f8e7a3c4022227487a30a5b9
|
| 3 |
+
size 1778516128
|
sharded/pytorch_model.bin.index.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|