abhinav37kr commited on
Commit
c6a0369
·
verified ·
1 Parent(s): 9244f34

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -0
app.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import OFATokenizer, OFAModel
3
+ from PIL import Image
4
+ import torch
5
+
6
+ # Load the OFA tokenizer and model
7
+ tokenizer = OFATokenizer.from_pretrained("OFA-Sys/ofa-base")
8
+ model = OFAModel.from_pretrained("OFA-Sys/ofa-base", use_cache=True)
9
+
10
+ def image_captioning(image):
11
+ # Preprocess the image
12
+ img = Image.open(image).convert("RGB")
13
+
14
+ # Generate the caption
15
+ inputs = tokenizer([img], return_tensors="pt")
16
+ with torch.no_grad():
17
+ outputs = model.generate(**inputs)
18
+
19
+ # Decode the output
20
+ caption = tokenizer.decode(outputs[0], skip_special_tokens=True)
21
+ return caption
22
+
23
+ # Create a Gradio interface
24
+ interface = gr.Interface(
25
+ fn=image_captioning,
26
+ inputs=gr.Image(label="Upload an Image", type="filepath"),
27
+ outputs=gr.Textbox(label="Generated Caption"),
28
+ title="OFA Image Captioning",
29
+ description="Upload an image to generate a caption using the OFA model.",
30
+ )
31
+
32
+ # Launch the interface
33
+ interface.launch()