Spaces:
Runtime error
Runtime error
Create app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839import streamlit as stfrom transformers import BlipForConditionalGeneration, AutoTokenizerimport torchfrom PIL import Imagefrom io import BytesIO
# Load the fine-tuned modelmodel_path = '/content/model_after_5_epochs.pth'model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")model.load_state_dict(torch.load(model_path))device = torch.device("cuda" if torch.cuda.is_available() else "cpu")model.to(device)tokenizer = AutoTokenizer.from_pretrained("Salesforce/blip-image-captioning-base")
# Function to generate caption for the uploaded imagedef generate_caption(image): # Preprocess the image image = Image.open(image).convert("RGB") image = image.resize((224, 224)) # Resize the image to match model input size image_tensor = torch.tensor([torch.Tensor(image)]).permute(0, 3, 1, 2).to(device)
# Generate caption output = model.generate(pixel_values=image_tensor) caption = tokenizer.decode(output[0], skip_special_tokens=True) return caption
# Streamlit appst.title("Image Caption Generator")
uploaded_image = st.file_uploader("Upload an image", type=["jpg", "jpeg"])
if uploaded_image is not None: st.image(uploaded_image, caption='Uploaded Image.', use_column_width=True) st.write("") st.write("Generating caption...")
# Generate caption for the uploaded image caption = generate_caption(uploaded_image) st.write("Caption:", caption)app.pyAdded+39