Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,10 @@
|
|
| 1 |
-
import os
|
| 2 |
|
| 3 |
-
os.system('pip install --upgrade pip')
|
| 4 |
|
| 5 |
import streamlit as st
|
| 6 |
from transformers import BlipForConditionalGeneration, AutoTokenizer
|
|
|
|
| 7 |
import torch
|
| 8 |
from PIL import Image
|
| 9 |
from io import BytesIO
|
|
@@ -21,17 +22,35 @@ model = BlipForConditionalGeneration.from_pretrained("MLInAi/CartoonCaptionGen")
|
|
| 21 |
tokenizer = AutoTokenizer.from_pretrained("MLInAi/CartoonCaptionGen")
|
| 22 |
|
| 23 |
# Function to generate caption for the uploaded image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
def generate_caption(image):
|
| 25 |
# Preprocess the image
|
| 26 |
image = Image.open(image).convert("RGB")
|
| 27 |
image = image.resize((224, 224)) # Resize the image to match model input size
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
# Generate caption
|
| 31 |
output = model.generate(pixel_values=image_tensor)
|
| 32 |
caption = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 33 |
return caption
|
| 34 |
-
|
| 35 |
# Streamlit app
|
| 36 |
st.title("Image Caption Generator")
|
| 37 |
|
|
|
|
| 1 |
+
# import os
|
| 2 |
|
| 3 |
+
# os.system('pip install --upgrade pip')
|
| 4 |
|
| 5 |
import streamlit as st
|
| 6 |
from transformers import BlipForConditionalGeneration, AutoTokenizer
|
| 7 |
+
import torchvision.transforms as transforms
|
| 8 |
import torch
|
| 9 |
from PIL import Image
|
| 10 |
from io import BytesIO
|
|
|
|
| 22 |
tokenizer = AutoTokenizer.from_pretrained("MLInAi/CartoonCaptionGen")
|
| 23 |
|
| 24 |
# Function to generate caption for the uploaded image
|
| 25 |
+
# def generate_caption(image):
|
| 26 |
+
# # Preprocess the image
|
| 27 |
+
# image = Image.open(image).convert("RGB")
|
| 28 |
+
# image = image.resize((224, 224)) # Resize the image to match model input size
|
| 29 |
+
# image_tensor = torch.tensor([torch.Tensor(image)]).permute(0, 3, 1, 2).to(device)
|
| 30 |
+
|
| 31 |
+
# # Generate caption
|
| 32 |
+
# output = model.generate(pixel_values=image_tensor)
|
| 33 |
+
# caption = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 34 |
+
# return caption
|
| 35 |
+
|
| 36 |
+
|
| 37 |
def generate_caption(image):
|
| 38 |
# Preprocess the image
|
| 39 |
image = Image.open(image).convert("RGB")
|
| 40 |
image = image.resize((224, 224)) # Resize the image to match model input size
|
| 41 |
+
|
| 42 |
+
# Convert the image to a tensor
|
| 43 |
+
transform = transforms.Compose([
|
| 44 |
+
transforms.ToTensor(),
|
| 45 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
| 46 |
+
])
|
| 47 |
+
image_tensor = transform(image).unsqueeze(0).to(device)
|
| 48 |
|
| 49 |
# Generate caption
|
| 50 |
output = model.generate(pixel_values=image_tensor)
|
| 51 |
caption = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 52 |
return caption
|
| 53 |
+
|
| 54 |
# Streamlit app
|
| 55 |
st.title("Image Caption Generator")
|
| 56 |
|