Spaces:
Running
Running
Commit ·
2a3ef59
1
Parent(s): b027f7e
added gradio app and image folder
Browse files- .gitattributes +2 -0
- .gradio/certificate.pem +31 -0
- app.py +140 -0
- generate.py +1 -1
- img/IMG_0747.jpg +3 -0
- img/IMG_0749.jpg +3 -0
- img/IMG_0763.jpg +3 -0
- img/IMG_0811.jpg +3 -0
- img/IMG_0812.jpg +3 -0
- img/IMG_0813.jpg +3 -0
- img/Screenshot 2025-05-01 at 10.47.21 PM.png +3 -0
- img/Screenshot 2025-05-06 at 10.32.16 AM.png +3 -0
- img/Screenshot 2025-05-06 at 9.56.27 AM.png +3 -0
- img/Screenshot 2025-05-06 at 9.56.58 AM.png +3 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
*.png filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
*.jpg filter=lfs diff=lfs merge=lfs -text
|
.gradio/certificate.pem
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
-----BEGIN CERTIFICATE-----
|
| 2 |
+
MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhv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
+
-----END CERTIFICATE-----
|
app.py
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import torchvision.transforms as transforms
|
| 5 |
+
import os
|
| 6 |
+
from model import EncoderCNN, CaptionTransformer, ImageCaptioningModel
|
| 7 |
+
from data_utils import Vocabulary, get_transforms
|
| 8 |
+
|
| 9 |
+
# Load model and vocabulary
|
| 10 |
+
def load_model(model_path="caption_model.pt", vocab_path="vocab.pkl"):
|
| 11 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 12 |
+
print(f"Using device: {device}")
|
| 13 |
+
|
| 14 |
+
# Load vocabulary
|
| 15 |
+
vocab = Vocabulary.load(vocab_path)
|
| 16 |
+
print(f"Loaded vocabulary with {len(vocab)} tokens")
|
| 17 |
+
|
| 18 |
+
# Initialize and load model
|
| 19 |
+
encoder = EncoderCNN(embed_size=512)
|
| 20 |
+
decoder = CaptionTransformer(vocab_size=len(vocab))
|
| 21 |
+
model = ImageCaptioningModel(encoder, decoder)
|
| 22 |
+
|
| 23 |
+
model.load_state_dict(torch.load(model_path, map_location=device))
|
| 24 |
+
model = model.to(device)
|
| 25 |
+
model.eval()
|
| 26 |
+
print(f"Model loaded from {model_path}")
|
| 27 |
+
|
| 28 |
+
return model, vocab, device
|
| 29 |
+
|
| 30 |
+
# Initialize model, vocab and device globally
|
| 31 |
+
model, vocab, device = load_model()
|
| 32 |
+
|
| 33 |
+
# Get the same transforms used in generate.py
|
| 34 |
+
model_transform = get_transforms(split="val")
|
| 35 |
+
|
| 36 |
+
def generate_caption(image, beam_size=5):
|
| 37 |
+
"""
|
| 38 |
+
Generate caption for a single image
|
| 39 |
+
"""
|
| 40 |
+
if image is None:
|
| 41 |
+
return "Please upload an image"
|
| 42 |
+
|
| 43 |
+
# Convert to PIL Image if needed
|
| 44 |
+
if not isinstance(image, Image.Image):
|
| 45 |
+
image = Image.fromarray(image)
|
| 46 |
+
|
| 47 |
+
# Convert to RGB if needed
|
| 48 |
+
image = image.convert("RGB")
|
| 49 |
+
|
| 50 |
+
# Apply transform for model input
|
| 51 |
+
model_input = model_transform(image).unsqueeze(0).to(device)
|
| 52 |
+
|
| 53 |
+
# Generate caption
|
| 54 |
+
with torch.no_grad():
|
| 55 |
+
beam_results = model.generate(
|
| 56 |
+
model_input,
|
| 57 |
+
vocab,
|
| 58 |
+
max_len=30,
|
| 59 |
+
beam_size=beam_size,
|
| 60 |
+
temperature=1.0 # Keep default temperature of 1.0
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
if beam_results:
|
| 64 |
+
# Get best prediction
|
| 65 |
+
best_pred_tokens, best_pred_score = beam_results[0]
|
| 66 |
+
best_pred_caption = ' '.join([t for t in best_pred_tokens if t not in {"<SOS>", "<EOS>", "<PAD>", "<UNK>"}])
|
| 67 |
+
|
| 68 |
+
# Format output with score
|
| 69 |
+
output = f"Best caption (Score: {best_pred_score:.2f}):\n{best_pred_caption}"
|
| 70 |
+
|
| 71 |
+
# Add other beam results
|
| 72 |
+
if len(beam_results) > 1:
|
| 73 |
+
output += "\n\nOther candidates:"
|
| 74 |
+
for i, (tokens, score) in enumerate(beam_results[1:], 1):
|
| 75 |
+
caption = ' '.join([t for t in tokens if t not in {"<SOS>", "<EOS>", "<PAD>", "<UNK>"}])
|
| 76 |
+
output += f"\n{i}. (Score: {score:.2f}): {caption}"
|
| 77 |
+
|
| 78 |
+
return output
|
| 79 |
+
else:
|
| 80 |
+
return "No caption generated"
|
| 81 |
+
|
| 82 |
+
def get_example_images():
|
| 83 |
+
"""Get all image files from the img directory"""
|
| 84 |
+
img_dir = "img"
|
| 85 |
+
if not os.path.exists(img_dir):
|
| 86 |
+
return []
|
| 87 |
+
|
| 88 |
+
# Get all image files
|
| 89 |
+
image_files = []
|
| 90 |
+
for file in os.listdir(img_dir):
|
| 91 |
+
if file.lower().endswith(('.png', '.jpg', '.jpeg')):
|
| 92 |
+
image_files.append([os.path.join(img_dir, file)])
|
| 93 |
+
|
| 94 |
+
return image_files
|
| 95 |
+
|
| 96 |
+
def create_interface():
|
| 97 |
+
with gr.Blocks(title="Image Captioning") as demo:
|
| 98 |
+
gr.Markdown("# Image Captioning Demo")
|
| 99 |
+
gr.Markdown("Upload an image to generate a caption using our transformer-based model.")
|
| 100 |
+
|
| 101 |
+
with gr.Row():
|
| 102 |
+
with gr.Column():
|
| 103 |
+
# Input image
|
| 104 |
+
image_input = gr.Image(type="pil", label="Upload Image")
|
| 105 |
+
|
| 106 |
+
# Parameters
|
| 107 |
+
with gr.Accordion("Advanced Parameters", open=False):
|
| 108 |
+
beam_size = gr.Slider(minimum=1, maximum=10, value=5, step=1,
|
| 109 |
+
label="Beam Size")
|
| 110 |
+
|
| 111 |
+
# Generate button
|
| 112 |
+
generate_btn = gr.Button("Generate Caption")
|
| 113 |
+
|
| 114 |
+
with gr.Column():
|
| 115 |
+
# Output caption
|
| 116 |
+
caption_output = gr.Textbox(label="Generated Caption", lines=10)
|
| 117 |
+
|
| 118 |
+
# Set up the generate function
|
| 119 |
+
generate_btn.click(
|
| 120 |
+
fn=generate_caption,
|
| 121 |
+
inputs=[image_input, beam_size],
|
| 122 |
+
outputs=caption_output
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
# Example images using all images from img directory
|
| 126 |
+
example_images = get_example_images()
|
| 127 |
+
if example_images:
|
| 128 |
+
gr.Examples(
|
| 129 |
+
examples=example_images,
|
| 130 |
+
inputs=image_input,
|
| 131 |
+
label="Example Images"
|
| 132 |
+
)
|
| 133 |
+
else:
|
| 134 |
+
gr.Markdown("No example images found in the img directory.")
|
| 135 |
+
|
| 136 |
+
return demo
|
| 137 |
+
|
| 138 |
+
if __name__ == "__main__":
|
| 139 |
+
demo = create_interface()
|
| 140 |
+
demo.launch(share=True) # share=True creates a public URL
|
generate.py
CHANGED
|
@@ -217,7 +217,7 @@ def main():
|
|
| 217 |
|
| 218 |
# Ground Truth
|
| 219 |
if use_coco_dataset:
|
| 220 |
-
gt_caption = gt_captions_map.get(img_path, "
|
| 221 |
# Clean GT caption tokens
|
| 222 |
gt_caption_cleaned = ' '.join([t for t in gt_caption.split() if t not in {"<SOS>", "<EOS>", "<PAD>", "<UNK>"}]) # Assuming gt_caption is a string
|
| 223 |
title_parts.append(f"GT: {gt_caption_cleaned}")
|
|
|
|
| 217 |
|
| 218 |
# Ground Truth
|
| 219 |
if use_coco_dataset:
|
| 220 |
+
gt_caption = gt_captions_map.get(img_path, "./img")
|
| 221 |
# Clean GT caption tokens
|
| 222 |
gt_caption_cleaned = ' '.join([t for t in gt_caption.split() if t not in {"<SOS>", "<EOS>", "<PAD>", "<UNK>"}]) # Assuming gt_caption is a string
|
| 223 |
title_parts.append(f"GT: {gt_caption_cleaned}")
|
img/IMG_0747.jpg
ADDED
|
Git LFS Details
|
img/IMG_0749.jpg
ADDED
|
Git LFS Details
|
img/IMG_0763.jpg
ADDED
|
Git LFS Details
|
img/IMG_0811.jpg
ADDED
|
Git LFS Details
|
img/IMG_0812.jpg
ADDED
|
Git LFS Details
|
img/IMG_0813.jpg
ADDED
|
Git LFS Details
|
img/Screenshot 2025-05-01 at 10.47.21 PM.png
ADDED
|
Git LFS Details
|
img/Screenshot 2025-05-06 at 10.32.16 AM.png
ADDED
|
Git LFS Details
|
img/Screenshot 2025-05-06 at 9.56.27 AM.png
ADDED
|
Git LFS Details
|
img/Screenshot 2025-05-06 at 9.56.58 AM.png
ADDED
|
Git LFS Details
|