Spaces:
Sleeping
Sleeping
File size: 1,277 Bytes
7066020 efb1d2b 7066020 |
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 35 36 37 38 39 40 41 42 43 44 45 |
import gradio as gr
from transformers import AutoProcessor, AutoModelForCausalLM
import torch
from PIL import Image
with gr.Blocks() as demo:
image = gr.Image(type='pil', label='Image')
label = gr.Text(label='Generated Caption')
image.upload(
[image],
[label]
)
if __name__ == '__main__':
demo.launch(share=True)
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()
# Set generation parameters
gen_kwargs = {'max_length': 100, 'num_beams': 2} # Adjust num_beams if needed
# 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()
|