File size: 1,035 Bytes
7cb0ff9
 
 
 
 
 
 
 
5df0c51
 
7cb0ff9
 
5df0c51
 
 
 
7cb0ff9
f63e78d
 
 
5df0c51
 
 
7cb0ff9
5df0c51
 
7cb0ff9
5df0c51
 
 
7cb0ff9
5df0c51
7cb0ff9
5df0c51
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from transformers import AutoProcessor, AutoModelForCausalLM
import torch
from PIL import Image

# Load model and processor
model = AutoModelForCausalLM.from_pretrained("Chesscorner/git-chess-v3")
processor = AutoProcessor.from_pretrained("Chesscorner/git-chess-v3")

# Set up device and move model to it
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)

# Enable mixed precision if on GPU
use_fp16 = device.type == "cuda"
if use_fp16:
    model.half()

max_length = 16
num_beams = 4
gen_kwargs = {'max_length': max_length, 'num_beams': num_beams}


# Prediction function
def predict_step(image):
    # Preprocess the image
    pixel_values = processor(images=image, return_tensors="pt").pixel_values.to(device)

    # Generate predictions with no_grad for efficiency
    with torch.no_grad():
        output_ids = model.generate(pixel_values=pixel_values, **gen_kwargs)

    # Decode predictions
    preds = processor.batch_decode(output_ids, skip_special_tokens=True)
    return preds[0].strip()