marconilab commited on
Commit
8df3bcd
·
verified ·
1 Parent(s): ed203f4

created app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -0
app.py ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import BlipForConditionalGeneration, AutoProcessor
3
+ from PIL import Image
4
+ import torch
5
+
6
+ # Load model and processor
7
+ processor = AutoProcessor.from_pretrained("blip-fine-tuned/")
8
+ processor.tokenizer.padding_size = 'left'
9
+ model = BlipForConditionalGeneration.from_pretrained("blip-fine-tuned/")
10
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
11
+ model.to(device)
12
+
13
+ def predict(image):
14
+ # Preprocess the image
15
+ inputs = processor(images=image, return_tensors="pt").to(device)
16
+ pixel_values = inputs.pixel_values
17
+
18
+ # get predictions
19
+ with torch.no_grad():
20
+ generated_ids = model.generate(pixel_values=pixel_values, max_length=100)
21
+
22
+ generated_caption = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
23
+ return generated_caption
24
+
25
+
26
+ interface = gr.Interface(fn=predict, inputs=gr.Image(type="pil"), outputs="text")
27
+ interface.launch()