dominguezdaniel commited on
Commit
f564393
·
verified ·
1 Parent(s): fd62502

Create 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 AutoModelForCausalLM, AutoTokenizer, AutoFeatureExtractor
3
+ import torch
4
+ from PIL import Image
5
+ import requests
6
+
7
+ # Load the tokenizer, model, and feature extractor
8
+ model_name = "Salesforce/BLIP-image-captioning-base"
9
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
10
+ model = AutoModelForCausalLM.from_pretrained(model_name)
11
+ feature_extractor = AutoFeatureExtractor.from_pretrained(model_name)
12
+
13
+ def generate_caption(image):
14
+ inputs = feature_extractor(images=image, return_tensors="pt")
15
+ outputs = model.generate(**inputs, max_length=128, num_beams=4, return_dict_in_generate=True)
16
+ caption = tokenizer.decode(outputs.sequences[0], skip_special_tokens=True)
17
+ return caption
18
+
19
+ # Create the Gradio interface
20
+ interface = gr.Interface(fn=generate_caption,
21
+ inputs=gr.inputs.Image(type="pil"),
22
+ outputs="text",
23
+ title="Image Captioning with BLIP",
24
+ description="Upload an image to generate a caption.")
25
+
26
+ if __name__ == "__main__":
27
+ interface.launch()