Spaces:
Sleeping
Sleeping
Create caption.py
Browse files- caption.py +34 -0
caption.py
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import AutoProcessor, AutoModelForCausalLM
|
2 |
+
import torch
|
3 |
+
from PIL import Image
|
4 |
+
|
5 |
+
# Load model and processor
|
6 |
+
model = AutoModelForCausalLM.from_pretrained("Chesscorner/git-chess-v3")
|
7 |
+
processor = AutoProcessor.from_pretrained("Chesscorner/git-chess-v3")
|
8 |
+
|
9 |
+
# Set up device and move model to it
|
10 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
11 |
+
model.to(device)
|
12 |
+
|
13 |
+
# Enable mixed precision if on GPU
|
14 |
+
use_fp16 = device.type == "cuda"
|
15 |
+
if use_fp16:
|
16 |
+
model.half()
|
17 |
+
|
18 |
+
# Set generation parameters
|
19 |
+
gen_kwargs = {'max_length': 100, 'num_beams': 2} # Adjust num_beams if needed
|
20 |
+
|
21 |
+
|
22 |
+
# Prediction function
|
23 |
+
def predict_step(image):
|
24 |
+
# Preprocess the image
|
25 |
+
pixel_values = processor(images=image, return_tensors="pt").pixel_values.to(device)
|
26 |
+
|
27 |
+
# Generate predictions with no_grad for efficiency
|
28 |
+
with torch.no_grad():
|
29 |
+
output_ids = model.generate(pixel_values=pixel_values, **gen_kwargs)
|
30 |
+
|
31 |
+
# Decode predictions
|
32 |
+
preds = processor.batch_decode(output_ids, skip_special_tokens=True)
|
33 |
+
return preds[0].strip()
|
34 |
+
|