Spaces:
Sleeping
Sleeping
Update caption.py
Browse files- caption.py +18 -11
caption.py
CHANGED
@@ -6,21 +6,28 @@ from PIL import Image
|
|
6 |
model = AutoModelForCausalLM.from_pretrained("Chesscorner/git-chess-v3")
|
7 |
processor = AutoProcessor.from_pretrained("Chesscorner/git-chess-v3")
|
8 |
|
9 |
-
|
|
|
10 |
model.to(device)
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
|
|
15 |
|
|
|
|
|
|
|
|
|
|
|
16 |
def predict_step(image):
|
17 |
-
|
18 |
-
|
19 |
-
pixel_values = pixel_values.to(device)
|
20 |
|
21 |
-
|
|
|
|
|
22 |
|
|
|
23 |
preds = processor.batch_decode(output_ids, skip_special_tokens=True)
|
24 |
-
preds
|
25 |
-
return preds[0]
|
26 |
-
|
|
|
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': 10, '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()
|
|
|
|