garychew commited on
Commit
e7a9c69
·
verified ·
1 Parent(s): 6595301

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -0
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ from PIL import Image
3
+ from io import BytesIO
4
+ from bs4 import BeautifulSoup
5
+ from transformers import AutoProcessor, BlipForConditionalGeneration
6
+
7
+ # Load the pretrained processor and model
8
+ processor = # fill the pretrained model
9
+ model = # load the blip model
10
+
11
+ # URL of the page to scrape
12
+ url = "https://en.wikipedia.org/wiki/IBM"
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 img element
25
+ for img_element in img_elements:
26
+ img_url = img_element.get('src')
27
+
28
+ # Skip if the image is an SVG or too small (likely an icon)
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
+ try:
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 URL
56
+ caption_file.write(f"{img_url}: {caption}\n")
57
+ except Exception as e:
58
+ print(f"Error processing image {img_url}: {e}")
59
+ continue