Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
os.environ['HF_HUB_DISABLE_SYMLINKS_WARNING'] = '1'
|
| 3 |
+
|
| 4 |
+
import torch
|
| 5 |
+
from torchvision import models, transforms
|
| 6 |
+
from PIL import Image
|
| 7 |
+
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
| 8 |
+
import streamlit as st
|
| 9 |
+
|
| 10 |
+
# Load the pre-trained image feature extraction model
|
| 11 |
+
resnet = models.resnet50(pretrained=True)
|
| 12 |
+
resnet.eval()
|
| 13 |
+
|
| 14 |
+
# Load the pre-trained language model
|
| 15 |
+
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
|
| 16 |
+
model = GPT2LMHeadModel.from_pretrained("gpt2")
|
| 17 |
+
model.eval()
|
| 18 |
+
|
| 19 |
+
# Preprocess the image
|
| 20 |
+
def preprocess_image(image_path):
|
| 21 |
+
image = Image.open(image_path)
|
| 22 |
+
preprocess = transforms.Compose([
|
| 23 |
+
transforms.Resize(256),
|
| 24 |
+
transforms.CenterCrop(224),
|
| 25 |
+
transforms.ToTensor(),
|
| 26 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
| 27 |
+
])
|
| 28 |
+
input_tensor = preprocess(image)
|
| 29 |
+
input_batch = input_tensor.unsqueeze(0)
|
| 30 |
+
return input_batch
|
| 31 |
+
|
| 32 |
+
# Extract image features
|
| 33 |
+
def extract_image_features(image_path):
|
| 34 |
+
input_batch = preprocess_image(image_path)
|
| 35 |
+
with torch.no_grad():
|
| 36 |
+
output = resnet(input_batch)
|
| 37 |
+
image_features = output.squeeze(0)
|
| 38 |
+
return image_features
|
| 39 |
+
|
| 40 |
+
# Generate caption
|
| 41 |
+
def generate_caption(image_features):
|
| 42 |
+
caption = tokenizer.decode(model.generate(input_ids=model.config.pad_token_id,
|
| 43 |
+
max_length=50,
|
| 44 |
+
eos_token_id=model.config.eos_token_id,
|
| 45 |
+
no_repeat_ngram_size=2,
|
| 46 |
+
num_return_sequences=1,
|
| 47 |
+
attention_mask=None,
|
| 48 |
+
encoder_outputs=None,
|
| 49 |
+
decoder_start_token_id=None,
|
| 50 |
+
use_cache=None,
|
| 51 |
+
labels=None,
|
| 52 |
+
output_attentions=None,
|
| 53 |
+
output_hidden_states=None,
|
| 54 |
+
output_scores=None,
|
| 55 |
+
return_dict=None,
|
| 56 |
+
input_embeds=image_features.unsqueeze(0)))
|
| 57 |
+
return caption
|
| 58 |
+
|
| 59 |
+
# Streamlit app
|
| 60 |
+
st.title("Image Captioning with GPT-2")
|
| 61 |
+
uploaded_file = st.file_uploader("Choose an image...", type="jpg")
|
| 62 |
+
|
| 63 |
+
if uploaded_file is not None:
|
| 64 |
+
# Display the uploaded image
|
| 65 |
+
image = Image.open(uploaded_file)
|
| 66 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 67 |
+
|
| 68 |
+
# Generate caption when the image is uploaded
|
| 69 |
+
image_features = extract_image_features(uploaded_file)
|
| 70 |
+
caption = generate_caption(image_features)
|
| 71 |
+
st.write("Generated Caption:", caption)
|