Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,105 @@
|
|
| 1 |
-
π¦ RADIOCAP13 β HuggingFace Space
|
| 2 |
|
| 3 |
-
Below is a complete multi-file project layout for deploying your image-captioning model as a HuggingFace Space.
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# π¦ RADIOCAP13 β HuggingFace Space
|
| 2 |
|
| 3 |
+
Below is a complete multi-file project layout for deploying your image-captioning model as a HuggingFace Space.
|
| 4 |
+
You can copy/paste these into your repository.
|
| 5 |
|
| 6 |
+
---
|
| 7 |
|
| 8 |
+
## **app.py**
|
| 9 |
+
```python
|
| 10 |
+
import gradio as gr
|
| 11 |
+
import torch
|
| 12 |
+
from transformers import ViTModel
|
| 13 |
+
from PIL import Image
|
| 14 |
+
from torchvision import transforms
|
| 15 |
+
import json
|
| 16 |
|
| 17 |
+
IMG_SIZE = 224
|
| 18 |
+
SEQ_LEN = 32
|
| 19 |
+
VOCAB_SIZE = 75460
|
| 20 |
|
| 21 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 22 |
|
| 23 |
+
transform = transforms.Compose([
|
| 24 |
+
transforms.Resize((IMG_SIZE, IMG_SIZE)),
|
| 25 |
+
transforms.ToTensor(),
|
| 26 |
+
])
|
| 27 |
|
| 28 |
+
def preprocess_image(img):
|
| 29 |
+
if img is None:
|
| 30 |
+
raise ValueError("Image is None")
|
| 31 |
+
if not isinstance(img, Image.Image):
|
| 32 |
+
img = Image.fromarray(img)
|
| 33 |
+
if img.mode != "RGB":
|
| 34 |
+
img = img.convert("RGB")
|
| 35 |
+
return transform(img)
|
| 36 |
|
| 37 |
+
class SimpleTokenizer:
|
| 38 |
+
def __init__(self, word2idx=None):
|
| 39 |
+
self.word2idx = word2idx or {}
|
| 40 |
+
self.idx2word = {v: k for k, v in self.word2idx.items()}
|
| 41 |
|
| 42 |
+
@classmethod
|
| 43 |
+
def load(cls, path):
|
| 44 |
+
with open(f"{path}/vocab.json", "r") as f:
|
| 45 |
+
word2idx = json.load(f)
|
| 46 |
+
return cls(word2idx)
|
| 47 |
|
| 48 |
+
class BiasDecoder(torch.nn.Module):
|
| 49 |
+
def __init__(self, feature_dim=768, vocab_size=VOCAB_SIZE):
|
| 50 |
+
super().__init__()
|
| 51 |
+
self.token_emb = torch.nn.Embedding(vocab_size, feature_dim)
|
| 52 |
+
self.pos_emb = torch.nn.Embedding(SEQ_LEN-1, feature_dim)
|
| 53 |
+
self.final_layer = torch.nn.Linear(feature_dim, vocab_size)
|
| 54 |
|
| 55 |
+
def forward(self, img_feat, target_seq):
|
| 56 |
+
x = self.token_emb(target_seq)
|
| 57 |
+
pos = torch.arange(x.size(1), device=x.device).clamp(max=self.pos_emb.num_embeddings - 1)
|
| 58 |
+
x = x + self.pos_emb(pos)
|
| 59 |
+
x = x + img_feat.unsqueeze(1)
|
| 60 |
+
return self.final_layer(x)
|
| 61 |
|
| 62 |
+
# Load models
|
| 63 |
+
decoder = BiasDecoder().to(device)
|
| 64 |
+
decoder.load_state_dict(torch.load("pytorch_model.bin", map_location=device))
|
| 65 |
+
decoder.eval()
|
| 66 |
|
| 67 |
+
vit = ViTModel.from_pretrained("google/vit-base-patch16-224-in21k").to(device)
|
| 68 |
+
vit.eval()
|
| 69 |
+
|
| 70 |
+
tokenizer = SimpleTokenizer.load("./")
|
| 71 |
+
pad_idx = tokenizer.word2idx["<PAD>"]
|
| 72 |
+
|
| 73 |
+
@torch.no_grad()
|
| 74 |
+
def generate_caption(img):
|
| 75 |
+
img_tensor = preprocess_image(img).unsqueeze(0).to(device)
|
| 76 |
+
img_feat = vit(pixel_values=img_tensor).pooler_output
|
| 77 |
+
|
| 78 |
+
beams = [([tokenizer.word2idx["<SOS>"]], 0.0)]
|
| 79 |
+
beam_size = 3
|
| 80 |
+
|
| 81 |
+
for _ in range(SEQ_LEN - 1):
|
| 82 |
+
candidates = []
|
| 83 |
+
for seq, score in beams:
|
| 84 |
+
inp = torch.tensor(seq + [pad_idx] * (SEQ_LEN - len(seq)), device=device).unsqueeze(0)
|
| 85 |
+
logits = decoder(img_feat, inp)
|
| 86 |
+
probs = torch.nn.functional.log_softmax(logits[0, len(seq)-1], dim=-1)
|
| 87 |
+
top_p, top_i = torch.topk(probs, beam_size)
|
| 88 |
+
for i in range(beam_size):
|
| 89 |
+
candidates.append((seq + [top_i[i].item()], score + top_p[i].item()))
|
| 90 |
+
beams = sorted(candidates, key=lambda x: x[1], reverse=True)[:beam_size]
|
| 91 |
+
if all(s[-1] == tokenizer.word2idx["<EOS>"] for s, _ in beams):
|
| 92 |
+
break
|
| 93 |
+
|
| 94 |
+
words = [tokenizer.idx2word.get(i, "<UNK>") for i in beams[0][0][1:] if i != pad_idx]
|
| 95 |
+
return " ".join(words)
|
| 96 |
+
|
| 97 |
+
with gr.Blocks() as demo:
|
| 98 |
+
gr.Markdown("# RADIOCAP13 β Image Captioning Demo")
|
| 99 |
+
img_in = gr.Image(type="pil", label="Upload an Image")
|
| 100 |
+
out = gr.Textbox(label="Generated Caption")
|
| 101 |
+
btn = gr.Button("Generate Caption")
|
| 102 |
+
btn.click(generate_caption, inputs=img_in, outputs=out)
|
| 103 |
+
|
| 104 |
+
if __name__ == "__main__":
|
| 105 |
+
demo.launch()
|