Chesscorner commited on
Commit
84a91ca
·
verified ·
1 Parent(s): d6e806b

Rename caption.py to app.py

Browse files
Files changed (2) hide show
  1. app.py +15 -0
  2. caption.py +0 -44
app.py ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ from caption import predict_step
4
+
5
+ with gr.Blocks() as demo:
6
+ image = gr.Image(type='pil', label='Image')
7
+ label = gr.Text(label='Generated Caption')
8
+ image.upload(
9
+ predict_step,
10
+ [image],
11
+ [label]
12
+ )
13
+
14
+ if __name__ == '__main__':
15
+ demo.launch(share=True)
caption.py DELETED
@@ -1,44 +0,0 @@
1
- import gradio as gr
2
- from transformers import AutoProcessor, AutoModelForCausalLM
3
- import torch
4
- from PIL import Image
5
-
6
- with gr.Blocks() as demo:
7
- image = gr.Image(type='pil', label='Image')
8
- label = gr.Text(label='Generated Caption')
9
- image.upload(
10
- [image],
11
- [label]
12
- )
13
-
14
- if __name__ == '__main__':
15
- demo.launch(share=True)
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()