Luca137 commited on
Commit
d7d532c
·
verified ·
1 Parent(s): 4b433e6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -8
app.py CHANGED
@@ -1,10 +1,24 @@
1
  import gradio as gr
 
 
2
 
3
- with gr.Blocks(fill_height=True) as demo:
4
- with gr.Sidebar():
5
- gr.Markdown("# Inference Provider")
6
- gr.Markdown("This Space showcases the Salesforce/blip-image-captioning-base model, served by the hf-inference API. Sign in with your Hugging Face account to use this API.")
7
- button = gr.LoginButton("Sign in")
8
- gr.load("models/Salesforce/blip-image-captioning-base", accept_token=button, provider="hf-inference")
9
-
10
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from transformers import BlipProcessor, BlipForConditionalGeneration
3
+ from PIL import Image
4
 
5
+ # Load model and processor
6
+ processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
7
+ model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
8
+
9
+ # Inference function
10
+ def generate_caption(image):
11
+ inputs = processor(images=image, return_tensors="pt")
12
+ out = model.generate(**inputs)
13
+ caption = processor.decode(out[0], skip_special_tokens=True)
14
+ return caption
15
+
16
+ # Gradio interface
17
+ with gr.Blocks() as demo:
18
+ gr.Markdown("## 🖼️ Upload an image to generate a caption using BLIP")
19
+ image_input = gr.Image(type="pil", label="Image")
20
+ caption_output = gr.Textbox(label="Caption")
21
+ btn = gr.Button("Generate")
22
+ btn.click(fn=generate_caption, inputs=image_input, outputs=caption_output)
23
+
24
+ demo.launch()