IncinerateZ commited on
Commit
5a470e9
β€’
1 Parent(s): 77d3b9c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -0
app.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import torch
3
+ from PIL import Image
4
+ from io import BytesIO
5
+
6
+ from transformers import AutoProcessor, AutoModelForVision2Seq
7
+ from transformers.image_utils import load_image
8
+
9
+ DEVICE = "cuda:0"
10
+
11
+ # Note that passing the image urls (instead of the actual pil images) to the processor is also possible
12
+ image1 = load_image("https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg")
13
+ image2 = load_image("https://cdn.britannica.com/59/94459-050-DBA42467/Skyline-Chicago.jpg")
14
+ image3 = load_image("https://cdn.britannica.com/68/170868-050-8DDE8263/Golden-Gate-Bridge-San-Francisco.jpg")
15
+
16
+ processor = AutoProcessor.from_pretrained("HuggingFaceM4/idefics2-8b-base")
17
+ model = AutoModelForVision2Seq.from_pretrained(
18
+ "HuggingFaceM4/idefics2-8b-base",
19
+ ).to(DEVICE)
20
+
21
+ # Create inputs
22
+ prompts = [
23
+ "<image>In this image, we can see the city of New York, and more specifically the Statue of Liberty.<image>In this image,",
24
+ "In which city is that bridge located?<image>",
25
+ ]
26
+ images = [[image1, image2], [image3]]
27
+ inputs = processor(text=prompts, images=images, padding=True, return_tensors="pt")
28
+ inputs = {k: v.to(DEVICE) for k, v in inputs.items()}
29
+
30
+
31
+ # Generate
32
+ generated_ids = model.generate(**inputs, max_new_tokens=500)
33
+ generated_texts = processor.batch_decode(generated_ids, skip_special_tokens=True)
34
+
35
+ print(generated_texts)
36
+ # ['In this image, we can see the city of New York, and more specifically the Statue of Liberty. In this image, we can see the city of Chicago, and more specifically the skyscrapers of the city.', 'In which city is that bridge located? The Golden Gate Bridge is a suspension bridge spanning the Golden Gate, the one-mile-wide (1.6 km) strait connecting San Francisco Bay and the Pacific Ocean. The structure links the American city of San Francisco, California β€” the northern tip of the San Francisco Peninsula β€” to Marin County, carrying both U.S. Route 101 and California State Route 1 across the strait. The bridge is one of the most internationally recognized symbols of San Francisco, California, and the United States. It has been declared one of the Wonders of the Modern World by the American Society of Civil Engineers.\n\nThe Golden Gate Bridge is a suspension bridge spanning the Golden Gate, the one-mile-wide (1.6 km) strait connecting San Francisco Bay and the Pacific Ocean. The structure links the American city of San Francisco, California β€” the northern tip of the San Francisco Peninsula β€” to Marin County, carrying both U.S. Route 101 and California State Route 1 across the strait. The bridge is one of the most internationally recognized symbols of San Francisco, California, and the United States. It has been declared one of the Wonders of the Modern World by the American Society of Civil Engineers.\n\nThe Golden Gate Bridge is a suspension bridge spanning the Golden Gate, the one-mile-wide (1.6 km) strait connecting San Francisco Bay and the Pacific Ocean. The structure links the American city of San Francisco, California β€” the northern tip of the San Francisco Peninsula β€” to Marin County, carrying both U.S. Route 101 and California State Route 1 across the strait. The bridge is one of the most internationally recognized symbols of San Francisco, California, and the United States. It has been declared one of the Wonders of the Modern World by the American Society of Civil Engineers.\n\nThe Golden Gate Bridge is a suspension bridge spanning the Golden Gate, the one-mile-wide (1.6 km) strait connecting San Francisco Bay and the Pacific Ocean. The structure links the American city of San Francisco, California β€” the northern tip of the San Francisco Peninsula β€” to Marin County, carrying both U.S. Route 101 and California State Route 1 across the strait. The bridge is one of the most internationally recognized symbols of San Francisco, California, and']