Spaces:
Sleeping
Sleeping
Upload main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from PIL import Image
|
| 3 |
+
from transformers import AutoProcessor, AutoModelForVision2Seq
|
| 4 |
+
from transformers.image_utils import load_image
|
| 5 |
+
|
| 6 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 7 |
+
|
| 8 |
+
# Load images
|
| 9 |
+
image = load_image("https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg")
|
| 10 |
+
|
| 11 |
+
# Initialize processor and model
|
| 12 |
+
processor = AutoProcessor.from_pretrained("HuggingFaceTB/SmolVLM-500M-Instruct")
|
| 13 |
+
model = AutoModelForVision2Seq.from_pretrained(
|
| 14 |
+
"HuggingFaceTB/SmolVLM-500M-Instruct",
|
| 15 |
+
torch_dtype=torch.bfloat16,
|
| 16 |
+
_attn_implementation="flash_attention_2" if DEVICE == "cuda" else "eager",
|
| 17 |
+
).to(DEVICE)
|
| 18 |
+
|
| 19 |
+
# Create input messages
|
| 20 |
+
messages = [
|
| 21 |
+
{
|
| 22 |
+
"role": "user",
|
| 23 |
+
"content": [
|
| 24 |
+
{"type": "image"},
|
| 25 |
+
{"type": "text", "text": "Can you describe this image?"}
|
| 26 |
+
]
|
| 27 |
+
},
|
| 28 |
+
]
|
| 29 |
+
|
| 30 |
+
# Prepare inputs
|
| 31 |
+
prompt = processor.apply_chat_template(messages, add_generation_prompt=True)
|
| 32 |
+
inputs = processor(text=prompt, images=[image], return_tensors="pt")
|
| 33 |
+
inputs = inputs.to(DEVICE)
|
| 34 |
+
|
| 35 |
+
# Generate outputs
|
| 36 |
+
generated_ids = model.generate(**inputs, max_new_tokens=500)
|
| 37 |
+
generated_texts = processor.batch_decode(
|
| 38 |
+
generated_ids,
|
| 39 |
+
skip_special_tokens=True,
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
print(generated_texts[0])
|
| 43 |
+
"""
|
| 44 |
+
Assistant: The image depicts a cityscape featuring a prominent landmark, the Statue of Liberty, prominently positioned on Liberty Island. The statue is a green, humanoid figure with a crown atop its head and is situated on a small island surrounded by water. The statue is characterized by its large, detailed structure, with a statue of a woman holding a torch above her head and a tablet in her left hand. The statue is surrounded by a small, rocky island, which is partially visible in the foreground.
|
| 45 |
+
In the background, the cityscape is dominated by numerous high-rise buildings, which are densely packed and vary in height. The buildings are primarily made of glass and steel, reflecting the sunlight and creating a bright, urban skyline. The skyline is filled with various architectural styles, including modern skyscrapers and older, more traditional buildings.
|
| 46 |
+
The water surrounding the island is calm, with a few small boats visible, indicating that the area is likely a popular tourist destination. The water is a deep blue, suggesting that it is a large body of water, possibly a river or a large lake.
|
| 47 |
+
In the foreground, there is a small strip of land with trees and grass, which adds a touch of natural beauty to the urban landscape. The trees are green, indicating that it is likely spring or summer.
|
| 48 |
+
The image captures a moment of tranquility and reflection, as the statue and the cityscape come together to create a harmonious and picturesque scene. The statue's presence in the foreground draws attention to the city's grandeur, while the calm water and natural elements in the background provide a sense of peace and serenity.
|
| 49 |
+
In summary, the image showcases the Statue of Liberty, a symbol of freedom and democracy, set against a backdrop of a bustling cityscape. The statue is a prominent and iconic representation of human achievement, while the cityscape is a testament to human ingenuity and progress. The image captures the beauty and complexity of urban life, with the statue serving as a symbol of hope and freedom, while the cityscape provides a glimpse into the modern world.
|
| 50 |
+
"""
|