ProfRom commited on
Commit
7719da2
·
verified ·
1 Parent(s): b1b7ab3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -0
app.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """app.ipynb
3
+
4
+ Automatically generated by Colab.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1_HQHDuRl3mgto6slVIJGSlZ5DZeSs4El
8
+ """
9
+
10
+ import torch
11
+ from transformers import pipeline
12
+ import gradio as gr
13
+
14
+ # Choose device: GPU if available, otherwise CPU. On Hugging Face Spaces, unless you explicitly pick a GPU runtime, you’re on CPU only
15
+ if torch.cuda.is_available():
16
+ vqa = pipeline(
17
+ task="visual-question-answering",
18
+ model="Salesforce/blip-vqa-base",
19
+ torch_dtype=torch.float16,#newer versions of TRANSFORMERS in Hugging face is torch_dtype not dtype. dtype is still working fine in Google Colab space
20
+ device=0, # GPU
21
+ use_fast=False,
22
+ )
23
+ else:
24
+ vqa = pipeline(
25
+ task="visual-question-answering",
26
+ model="Salesforce/blip-vqa-base",
27
+ device=-1, # CPU
28
+ use_fast=False,
29
+ )
30
+
31
+ def answer_question(image, question):
32
+ if not question:
33
+ return "Please type a question about the image."
34
+ # vqa returns a list of dicts like [{'score':..., 'answer':...}]
35
+ result = vqa(question=question, image=image)
36
+ return result[0]["answer"]
37
+
38
+ demo = gr.Interface(
39
+ fn=answer_question,
40
+ inputs=[
41
+ gr.Image(type="pil", label="Upload an image"),
42
+ gr.Textbox(label="Question", placeholder="e.g. What is the weather in this image?"),
43
+ ],
44
+ outputs=gr.Textbox(label="Answer"),
45
+ title="BLIP Visual Question Answering",
46
+ description="Ask a question about the uploaded image using Salesforce/blip-vqa-base.",
47
+ )
48
+
49
+ if __name__ == "__main__":
50
+ demo.launch()