llmachatbot / model.py
welcometoFightclub's picture
Update model.py
f0240fe verified
import requests
from PIL import Image
from transformers import BlipProcessor, BlipForConditionalGeneration
# Initialize processor and model
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-large").to("cuda")
# Function to process and caption an image from a URL
def caption_image(image_url):
try:
# Load image from the provided URL
raw_image = Image.open(requests.get(image_url, stream=True).raw).convert('RGB')
# Conditional image captioning
text = "a photography of"
inputs = processor(raw_image, text, return_tensors="pt").to("cuda")
out = model.generate(**inputs)
conditional_caption = processor.decode(out[0], skip_special_tokens=True)
# Unconditional image captioning
inputs = processor(raw_image, return_tensors="pt").to("cuda")
out = model.generate(**inputs)
unconditional_caption = processor.decode(out[0], skip_special_tokens=True)
# Print the results
print("Conditional Caption:", conditional_caption)
print("Unconditional Caption:", unconditional_caption)
except Exception as e:
print(f"Error occurred: {e}")
# Get image URL from user input
image_url = input("Enter the image URL: ")
caption_image(image_url)