Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from PIL import Image
|
| 3 |
+
from transformers import AutoProcessor, AutoModelForVision2Seq
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 8 |
+
|
| 9 |
+
token = os.environ.get("HF_TOKEN")
|
| 10 |
+
|
| 11 |
+
processor = AutoProcessor.from_pretrained("HuggingFaceTB/SmolVLM-Instruct", token=token)
|
| 12 |
+
model = AutoModelForVision2Seq.from_pretrained(
|
| 13 |
+
"HuggingFaceTB/SmolVLM-Instruct",
|
| 14 |
+
torch_dtype=torch.bfloat16 if DEVICE == "cuda" else torch.float32,
|
| 15 |
+
_attn_implementation="flash_attention_2" if DEVICE == "cuda" else "eager",
|
| 16 |
+
token=token
|
| 17 |
+
).to(DEVICE)
|
| 18 |
+
|
| 19 |
+
def describe_image(image):
|
| 20 |
+
messages = [
|
| 21 |
+
{
|
| 22 |
+
"role": "user",
|
| 23 |
+
"content": [
|
| 24 |
+
{"type": "image"},
|
| 25 |
+
{"type": "text", "text": "Describe this image in detail."}
|
| 26 |
+
]
|
| 27 |
+
},
|
| 28 |
+
]
|
| 29 |
+
|
| 30 |
+
prompt = processor.apply_chat_template(messages, add_generation_prompt=True)
|
| 31 |
+
inputs = processor(text=prompt, images=[image], return_tensors="pt").to(DEVICE)
|
| 32 |
+
|
| 33 |
+
with torch.no_grad():
|
| 34 |
+
generated_ids = model.generate(**inputs, max_new_tokens=500)
|
| 35 |
+
|
| 36 |
+
result = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 37 |
+
return result
|
| 38 |
+
|
| 39 |
+
demo = gr.Interface(
|
| 40 |
+
fn=describe_image,
|
| 41 |
+
inputs=gr.Image(type="pil", label="Upload an Image"),
|
| 42 |
+
outputs="text",
|
| 43 |
+
title="VISIONSAGE",
|
| 44 |
+
description="Upload an image and get a detailed description."
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
demo.launch()
|