Spaces:
Runtime error
Runtime error
Update app.py
Browse files1234567891011121314151617181920212223242526272829303132333435363738394 unmodified lines444546474849505152535455564 unmodified lines616263646566# import os# os.system('pip install --upgrade pip')import streamlit as stfrom transformers import BlipForConditionalGeneration, AutoTokenizerimport torchvision.transforms as transformsimport torchfrom PIL import Imagefrom io import BytesIO# # Load the fine-tuned model# model_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")model = BlipForConditionalGeneration.from_pretrained("MLInAi/CartoonCaptionGen")tokenizer = AutoTokenizer.from_pretrained("MLInAi/CartoonCaptionGen")# Function to generate caption for the uploaded image# def 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 captiondef generate_caption(image):# Preprocess the imageimage = Image.open(image).convert("RGB")4 unmodified linestransforms.ToTensor(),transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])])image_tensor = transform(image).unsqueeze(0)# Generate captionoutput = model.generate(pixel_values=image_tensor)caption = tokenizer.decode(output[0], skip_special_tokens=True)return caption# Streamlit appst.title("Image Caption Generator")4 unmodified linesst.write("")st.write("Generating caption...")# Generate caption for the uploaded imagecaption = generate_caption(uploaded_image)st.write("Caption:", caption)1234567891011121314151617184 unmodified lines23242526272829303132333435364 unmodified lines414243444546import streamlit as stfrom transformers import BlipForConditionalGeneration, AutoTokenizerimport torchfrom PIL import Imageimport torchvision.transforms as transforms# Load the fine-tuned model and tokenizermodel = BlipForConditionalGeneration.from_pretrained("MLInAi/CartoonCaptionGen")tokenizer = AutoTokenizer.from_pretrained("MLInAi/CartoonCaptionGen")device = torch.device("cuda" if torch.cuda.is_available() else "cpu")# Fixed promptFIXED_PROMPT = "Generate a funny caption from image"# Function to generate caption for the uploaded image with the fixed promptdef generate_caption(image):# Preprocess the imageimage = Image.open(image).convert("RGB")4 unmodified linestransforms.ToTensor(),transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])])image_tensor = transform(image).unsqueeze(0).to(device)# Generate caption with the fixed promptinput_text = FIXED_PROMPT + " "output = model.generate(pixel_values=image_tensor, input_text=input_text)caption = tokenizer.decode(output[0], skip_special_tokens=True)return caption# Streamlit appst.title("Image Caption Generator")4 unmodified linesst.write("")st.write("Generating caption...")# Generate caption for the uploaded image with the fixed promptcaption = generate_caption(uploaded_image)st.write("Caption:", caption)
app.py+13-33