Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,59 +1,33 @@
|
|
|
|
|
|
|
|
| 1 |
import requests
|
| 2 |
from PIL import Image
|
| 3 |
-
from
|
| 4 |
-
from bs4 import BeautifulSoup
|
| 5 |
-
from transformers import AutoProcessor, BlipForConditionalGeneration
|
| 6 |
|
| 7 |
# Load the pretrained processor and model
|
| 8 |
-
processor =
|
| 9 |
-
model =
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
# Download the page
|
| 15 |
-
response = requests.get(url)
|
| 16 |
-
# Parse the page with BeautifulSoup
|
| 17 |
-
soup = BeautifulSoup(response.text, 'html.parser')
|
| 18 |
-
|
| 19 |
-
# Find all img elements
|
| 20 |
-
img_elements = soup.find_all('img')
|
| 21 |
|
| 22 |
# Open a file to write the captions
|
| 23 |
with open("captions.txt", "w") as caption_file:
|
| 24 |
-
# Iterate over each
|
| 25 |
-
for
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
if 'svg' in img_url or '1x1' in img_url:
|
| 30 |
-
continue
|
| 31 |
-
|
| 32 |
-
# Correct the URL if it's malformed
|
| 33 |
-
if img_url.startswith('//'):
|
| 34 |
-
img_url = 'https:' + img_url
|
| 35 |
-
elif not img_url.startswith('http://') and not img_url.startswith('https://'):
|
| 36 |
-
continue # Skip URLs that don't start with http:// or https://
|
| 37 |
|
| 38 |
-
|
| 39 |
-
# Download the image
|
| 40 |
-
response = requests.get(img_url)
|
| 41 |
-
# Convert the image data to a PIL Image
|
| 42 |
-
raw_image = Image.open(BytesIO(response.content))
|
| 43 |
-
if raw_image.size[0] * raw_image.size[1] < 400: # Skip very small images
|
| 44 |
-
continue
|
| 45 |
-
|
| 46 |
-
raw_image = raw_image.convert('RGB')
|
| 47 |
-
|
| 48 |
-
# Process the image
|
| 49 |
inputs = processor(raw_image, return_tensors="pt")
|
|
|
|
| 50 |
# Generate a caption for the image
|
| 51 |
out = model.generate(**inputs, max_new_tokens=50)
|
|
|
|
| 52 |
# Decode the generated tokens to text
|
| 53 |
caption = processor.decode(out[0], skip_special_tokens=True)
|
| 54 |
|
| 55 |
-
# Write the caption to the file, prepended by the image
|
| 56 |
-
caption_file.write(f"{
|
| 57 |
-
except Exception as e:
|
| 58 |
-
print(f"Error processing image {img_url}: {e}")
|
| 59 |
-
continue
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import glob
|
| 3 |
import requests
|
| 4 |
from PIL import Image
|
| 5 |
+
from transformers import Blip2Processor, Blip2ForConditionalGeneration #Blip2 models
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# Load the pretrained processor and model
|
| 8 |
+
processor = Blip2Processor.from_pretrained("Salesforce/blip2-opt-2.7b")
|
| 9 |
+
model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-opt-2.7b")
|
| 10 |
|
| 11 |
+
# Specify the directory where your images are
|
| 12 |
+
image_dir = "/"
|
| 13 |
+
image_exts = ["jpg", "jpeg", "png"] # specify the image file extensions to search for
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
# Open a file to write the captions
|
| 16 |
with open("captions.txt", "w") as caption_file:
|
| 17 |
+
# Iterate over each image file in the directory
|
| 18 |
+
for image_ext in image_exts:
|
| 19 |
+
for img_path in glob.glob(os.path.join(image_dir, f"*.{image_ext}")):
|
| 20 |
+
# Load your image
|
| 21 |
+
raw_image = Image.open(img_path).convert('RGB')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
# You do not need a question for image captioning
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
inputs = processor(raw_image, return_tensors="pt")
|
| 25 |
+
|
| 26 |
# Generate a caption for the image
|
| 27 |
out = model.generate(**inputs, max_new_tokens=50)
|
| 28 |
+
|
| 29 |
# Decode the generated tokens to text
|
| 30 |
caption = processor.decode(out[0], skip_special_tokens=True)
|
| 31 |
|
| 32 |
+
# Write the caption to the file, prepended by the image file name
|
| 33 |
+
caption_file.write(f"{os.path.basename(img_path)}: {caption}\n")
|
|
|
|
|
|
|
|
|