Walid-Ahmed commited on
Commit
c9c2872
·
verified ·
1 Parent(s): 4322319

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -0
app.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import BlipForQuestionAnswering, AutoProcessor
4
+ from PIL import Image
5
+ import spaces
6
+
7
+ # Check device
8
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
9
+
10
+ # Load model and processor
11
+ model = BlipForQuestionAnswering.from_pretrained("Salesforce/blip-vqa-base").to(device)
12
+ processor = AutoProcessor.from_pretrained("Salesforce/blip-vqa-base")
13
+
14
+ @spaces.GPU # ZeroGPU: Allocate GPU only when generating images
15
+ def answer_question(image, question):
16
+ inputs = processor(image, question, return_tensors="pt").to(device)
17
+ out = model.generate(**inputs)
18
+ return processor.decode(out[0], skip_special_tokens=True)
19
+
20
+ iface = gr.Interface(
21
+ fn=answer_question,
22
+ inputs=[gr.Image(type="pil"), gr.Textbox(placeholder="Enter your question")],
23
+ outputs=gr.Textbox(label="Answer"),
24
+ title="Visual Question Answering with BLIP",
25
+ description="Upload an image and ask a question about its content.",
26
+ examples=[("image1.jpeg", "Is there a man or a woman in the image?")],
27
+ )
28
+
29
+ iface.launch()
30
+