SumantBobade commited on
Commit
11c740c
·
verified ·
1 Parent(s): 03ad1a2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -37
app.py CHANGED
@@ -1,37 +1,34 @@
1
- import gradio as gr
2
- from transformers import BlipProcessor, BlipForConditionalGeneration
3
- from PIL import Image
4
-
5
- # Load BLIP model and processor
6
- processor = BlipProcessor.from_pretrained("Salesforce/blip-vqa-base")
7
- model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-vqa-base")
8
-
9
- def answer_question(image, question):
10
- """
11
- Generates an answer based on the given image and question.
12
- """
13
- try:
14
- if not question.strip():
15
- return "Please enter a question."
16
-
17
- # Prepare inputs for the model
18
- inputs = processor(images=image, text=question, return_tensors="pt")
19
-
20
- # Generate answer
21
- outputs = model.generate(**inputs)
22
- answer = processor.decode(outputs[0], skip_special_tokens=True)
23
-
24
- return answer
25
- except Exception as e:
26
- return f"An error occurred: {str(e)}"
27
-
28
- # Gradio Interface
29
- iface = gr.Interface(
30
- fn=answer_question, # Function to process image + question
31
- inputs=[gr.Image(type="pil"), gr.Textbox(label="Ask a question about the image")],
32
- outputs="text",
33
- title="Visual Question Answering with BLIP",
34
- description="Upload an image and ask a question about it. The model will generate an answer."
35
- )
36
-
37
- iface.launch()
 
1
+ import gradio as gr
2
+ from transformers import BlipProcessor, BlipForConditionalGeneration
3
+ from PIL import Image
4
+
5
+ # Load BLIP 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
+ def caption_image(image):
10
+ """
11
+ Generates a caption for the given image.
12
+ """
13
+ try:
14
+ # Prepare inputs for image captioning
15
+ inputs = processor(images=image, return_tensors="pt")
16
+
17
+ # Generate caption
18
+ outputs = model.generate(**inputs)
19
+ caption = processor.decode(outputs[0], skip_special_tokens=True)
20
+
21
+ return caption
22
+ except Exception as e:
23
+ return f"An error occurred: {str(e)}"
24
+
25
+ # Gradio Interface
26
+ iface = gr.Interface(
27
+ fn=caption_image,
28
+ inputs=gr.Image(type="pil"),
29
+ outputs="text",
30
+ title="Image Captioning with BLIP",
31
+ description="Upload an image to generate a descriptive caption."
32
+ )
33
+
34
+ iface.launch()