Chesscorner commited on
Commit
7066020
·
verified ·
1 Parent(s): 387fc64

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -0
app.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoProcessor, AutoModelForCausalLM
3
+ import torch
4
+ from PIL import Image
5
+
6
+ from caption import predict_step
7
+
8
+ with gr.Blocks() as demo:
9
+ image = gr.Image(type='pil', label='Image')
10
+ label = gr.Text(label='Generated Caption')
11
+ image.upload(
12
+ predict_step,
13
+ [image],
14
+ [label]
15
+ )
16
+
17
+ model = AutoModelForCausalLM.from_pretrained("Chesscorner/git-chess-v3")
18
+ processor = AutoProcessor.from_pretrained("Chesscorner/git-chess-v3")
19
+
20
+ # Set up device and move model to it
21
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
22
+ model.to(device)
23
+
24
+ # Enable mixed precision if on GPU
25
+ use_fp16 = device.type == "cuda"
26
+ if use_fp16:
27
+ model.half()
28
+
29
+ # Set generation parameters
30
+ gen_kwargs = {'max_length': 100, 'num_beams': 2} # Adjust num_beams if needed
31
+
32
+
33
+ # Prediction function
34
+ def predict_step(image):
35
+ # Preprocess the image
36
+ pixel_values = processor(images=image, return_tensors="pt").pixel_values.to(device)
37
+
38
+ # Generate predictions with no_grad for efficiency
39
+ with torch.no_grad():
40
+ output_ids = model.generate(pixel_values=pixel_values, **gen_kwargs)
41
+
42
+ # Decode predictions
43
+ preds = processor.batch_decode(output_ids, skip_special_tokens=True)
44
+ return preds[0].strip()
45
+
46
+ if __name__ == '__main__':
47
+ demo.launch(share=True)