Spaces:
Runtime error
Runtime error
Commit ·
836577c
1
Parent(s): 468dbac
add model files and app.py
Browse files- app.py +60 -0
- model/config.json +107 -0
- model/generation_config.json +7 -0
- model/preprocessor_config.json +28 -0
- model/pytorch_model.bin +3 -0
- model/special_tokens_map.json +7 -0
- model/tokenizer.json +0 -0
- model/tokenizer_config.json +18 -0
- model/vocab.txt +0 -0
app.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""deploy_GIT_with_Gradio.ipynb
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colaboratory.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1s3Aa-QBjUtT2sW6HuRHoVaWvpJAaQIn5
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
!pip install -q gradio
|
| 11 |
+
!pip install -q transformers
|
| 12 |
+
|
| 13 |
+
import transformers
|
| 14 |
+
from transformers import BlipProcessor, BlipForImageTextRetrieval,BlipForConditionalGeneration, AutoProcessor
|
| 15 |
+
from transformers import AutoModelForCausalLM
|
| 16 |
+
import torch
|
| 17 |
+
import gradio as gr
|
| 18 |
+
from PIL import Image
|
| 19 |
+
|
| 20 |
+
# Load model from Huggingface Transformer library
|
| 21 |
+
# processor = AutoProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 22 |
+
# model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 23 |
+
|
| 24 |
+
model_path = "model"
|
| 25 |
+
processor = AutoProcessor.from_pretrained(model_path)
|
| 26 |
+
model = AutoModelForCausalLM.from_pretrained(model_path)
|
| 27 |
+
|
| 28 |
+
# def predict_text_classification(image):
|
| 29 |
+
# inputs = processor(images=image, return_tensors="pt").to(device)
|
| 30 |
+
# pixel_values = inputs.pixel_values
|
| 31 |
+
# generated_ids = model.generate(pixel_values=pixel_values, max_length=50)
|
| 32 |
+
# generated_caption = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 33 |
+
|
| 34 |
+
# return generated_caption
|
| 35 |
+
|
| 36 |
+
# Define the prediction function
|
| 37 |
+
def generate_caption(image):
|
| 38 |
+
# Process the image
|
| 39 |
+
image = Image.fromarray(image)
|
| 40 |
+
#inputs = tokenizer(image, return_tensors="pt")
|
| 41 |
+
inputs = processor(images=image, return_tensors="pt")#.to(device)
|
| 42 |
+
pixel_values = inputs.pixel_values
|
| 43 |
+
|
| 44 |
+
# Generate caption
|
| 45 |
+
generated_ids = model.generate(pixel_values=pixel_values, max_length=50)
|
| 46 |
+
generated_caption = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 47 |
+
|
| 48 |
+
return generated_caption
|
| 49 |
+
|
| 50 |
+
# Define the Gradio interface
|
| 51 |
+
interface = gr.Interface(
|
| 52 |
+
fn=generate_caption,
|
| 53 |
+
inputs=gr.Image(),
|
| 54 |
+
outputs=gr.Textbox(),
|
| 55 |
+
live=True,
|
| 56 |
+
#capture_session=True # Required for handling PIL Image in the prediction function
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
# Launch the Gradio interface
|
| 60 |
+
interface.launch(share=True)
|
model/config.json
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_commit_hash": "1f7fe8444292beb4a259e3a5b6eba440cd5999d4",
|
| 3 |
+
"_name_or_path": "microsoft/git-base",
|
| 4 |
+
"architectures": [
|
| 5 |
+
"GitForCausalLM"
|
| 6 |
+
],
|
| 7 |
+
"attention_probs_dropout_prob": 0.1,
|
| 8 |
+
"bos_token_id": 101,
|
| 9 |
+
"classifier_dropout": null,
|
| 10 |
+
"eos_token_id": 102,
|
| 11 |
+
"hidden_act": "gelu",
|
| 12 |
+
"hidden_dropout_prob": 0.1,
|
| 13 |
+
"hidden_size": 768,
|
| 14 |
+
"initializer_range": 0.02,
|
| 15 |
+
"intermediate_size": 3072,
|
| 16 |
+
"layer_norm_eps": 1e-12,
|
| 17 |
+
"max_position_embeddings": 1024,
|
| 18 |
+
"model_type": "git",
|
| 19 |
+
"num_attention_heads": 12,
|
| 20 |
+
"num_hidden_layers": 6,
|
| 21 |
+
"num_image_with_embedding": null,
|
| 22 |
+
"pad_token_id": 0,
|
| 23 |
+
"position_embedding_type": "absolute",
|
| 24 |
+
"tie_word_embeddings": false,
|
| 25 |
+
"torch_dtype": "float32",
|
| 26 |
+
"transformers_version": null,
|
| 27 |
+
"use_cache": true,
|
| 28 |
+
"vision_config": {
|
| 29 |
+
"_name_or_path": "",
|
| 30 |
+
"add_cross_attention": false,
|
| 31 |
+
"architectures": null,
|
| 32 |
+
"attention_dropout": 0.0,
|
| 33 |
+
"bad_words_ids": null,
|
| 34 |
+
"begin_suppress_tokens": null,
|
| 35 |
+
"bos_token_id": null,
|
| 36 |
+
"chunk_size_feed_forward": 0,
|
| 37 |
+
"cross_attention_hidden_size": null,
|
| 38 |
+
"decoder_start_token_id": null,
|
| 39 |
+
"diversity_penalty": 0.0,
|
| 40 |
+
"do_sample": false,
|
| 41 |
+
"dropout": 0.0,
|
| 42 |
+
"early_stopping": false,
|
| 43 |
+
"encoder_no_repeat_ngram_size": 0,
|
| 44 |
+
"eos_token_id": null,
|
| 45 |
+
"exponential_decay_length_penalty": null,
|
| 46 |
+
"finetuning_task": null,
|
| 47 |
+
"forced_bos_token_id": null,
|
| 48 |
+
"forced_eos_token_id": null,
|
| 49 |
+
"hidden_act": "quick_gelu",
|
| 50 |
+
"hidden_size": 768,
|
| 51 |
+
"id2label": {
|
| 52 |
+
"0": "LABEL_0",
|
| 53 |
+
"1": "LABEL_1"
|
| 54 |
+
},
|
| 55 |
+
"image_size": 224,
|
| 56 |
+
"initializer_factor": 1.0,
|
| 57 |
+
"initializer_range": 0.02,
|
| 58 |
+
"intermediate_size": 3072,
|
| 59 |
+
"is_decoder": false,
|
| 60 |
+
"is_encoder_decoder": false,
|
| 61 |
+
"label2id": {
|
| 62 |
+
"LABEL_0": 0,
|
| 63 |
+
"LABEL_1": 1
|
| 64 |
+
},
|
| 65 |
+
"layer_norm_eps": 1e-05,
|
| 66 |
+
"length_penalty": 1.0,
|
| 67 |
+
"max_length": 20,
|
| 68 |
+
"min_length": 0,
|
| 69 |
+
"model_type": "git_vision_model",
|
| 70 |
+
"no_repeat_ngram_size": 0,
|
| 71 |
+
"num_attention_heads": 12,
|
| 72 |
+
"num_beam_groups": 1,
|
| 73 |
+
"num_beams": 1,
|
| 74 |
+
"num_channels": 3,
|
| 75 |
+
"num_hidden_layers": 12,
|
| 76 |
+
"num_return_sequences": 1,
|
| 77 |
+
"output_attentions": false,
|
| 78 |
+
"output_hidden_states": false,
|
| 79 |
+
"output_scores": false,
|
| 80 |
+
"pad_token_id": null,
|
| 81 |
+
"patch_size": 16,
|
| 82 |
+
"prefix": null,
|
| 83 |
+
"problem_type": null,
|
| 84 |
+
"projection_dim": 512,
|
| 85 |
+
"pruned_heads": {},
|
| 86 |
+
"remove_invalid_values": false,
|
| 87 |
+
"repetition_penalty": 1.0,
|
| 88 |
+
"return_dict": true,
|
| 89 |
+
"return_dict_in_generate": false,
|
| 90 |
+
"sep_token_id": null,
|
| 91 |
+
"suppress_tokens": null,
|
| 92 |
+
"task_specific_params": null,
|
| 93 |
+
"temperature": 1.0,
|
| 94 |
+
"tf_legacy_loss": false,
|
| 95 |
+
"tie_encoder_decoder": false,
|
| 96 |
+
"tie_word_embeddings": true,
|
| 97 |
+
"tokenizer_class": null,
|
| 98 |
+
"top_k": 50,
|
| 99 |
+
"top_p": 1.0,
|
| 100 |
+
"torch_dtype": null,
|
| 101 |
+
"torchscript": false,
|
| 102 |
+
"transformers_version": "4.30.2",
|
| 103 |
+
"typical_p": 1.0,
|
| 104 |
+
"use_bfloat16": false
|
| 105 |
+
},
|
| 106 |
+
"vocab_size": 30522
|
| 107 |
+
}
|
model/generation_config.json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_from_model_config": true,
|
| 3 |
+
"bos_token_id": 101,
|
| 4 |
+
"eos_token_id": 102,
|
| 5 |
+
"pad_token_id": 0,
|
| 6 |
+
"transformers_version": "4.30.2"
|
| 7 |
+
}
|
model/preprocessor_config.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"crop_size": {
|
| 3 |
+
"height": 224,
|
| 4 |
+
"width": 224
|
| 5 |
+
},
|
| 6 |
+
"do_center_crop": true,
|
| 7 |
+
"do_convert_rgb": true,
|
| 8 |
+
"do_normalize": true,
|
| 9 |
+
"do_rescale": true,
|
| 10 |
+
"do_resize": true,
|
| 11 |
+
"image_mean": [
|
| 12 |
+
0.48145466,
|
| 13 |
+
0.4578275,
|
| 14 |
+
0.40821073
|
| 15 |
+
],
|
| 16 |
+
"image_processor_type": "CLIPImageProcessor",
|
| 17 |
+
"image_std": [
|
| 18 |
+
0.26862954,
|
| 19 |
+
0.26130258,
|
| 20 |
+
0.27577711
|
| 21 |
+
],
|
| 22 |
+
"processor_class": "GitProcessor",
|
| 23 |
+
"resample": 3,
|
| 24 |
+
"rescale_factor": 0.00392156862745098,
|
| 25 |
+
"size": {
|
| 26 |
+
"shortest_edge": 224
|
| 27 |
+
}
|
| 28 |
+
}
|
model/pytorch_model.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:5cbe4226c595094a82da0b880bf2c89c9f35874453d293887f42e7c4d6ea1d30
|
| 3 |
+
size 706594713
|
model/special_tokens_map.json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cls_token": "[CLS]",
|
| 3 |
+
"mask_token": "[MASK]",
|
| 4 |
+
"pad_token": "[PAD]",
|
| 5 |
+
"sep_token": "[SEP]",
|
| 6 |
+
"unk_token": "[UNK]"
|
| 7 |
+
}
|
model/tokenizer.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
model/tokenizer_config.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"clean_up_tokenization_spaces": true,
|
| 3 |
+
"cls_token": "[CLS]",
|
| 4 |
+
"do_lower_case": true,
|
| 5 |
+
"mask_token": "[MASK]",
|
| 6 |
+
"model_input_names": [
|
| 7 |
+
"input_ids",
|
| 8 |
+
"attention_mask"
|
| 9 |
+
],
|
| 10 |
+
"model_max_length": 512,
|
| 11 |
+
"pad_token": "[PAD]",
|
| 12 |
+
"processor_class": "GitProcessor",
|
| 13 |
+
"sep_token": "[SEP]",
|
| 14 |
+
"strip_accents": null,
|
| 15 |
+
"tokenize_chinese_chars": true,
|
| 16 |
+
"tokenizer_class": "BertTokenizer",
|
| 17 |
+
"unk_token": "[UNK]"
|
| 18 |
+
}
|
model/vocab.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|