Ashish Soni commited on
Commit
cef44ac
·
verified ·
1 Parent(s): 5dc82f7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -5
app.py CHANGED
@@ -1,18 +1,53 @@
1
  import gradio as gr
 
 
2
  from transformers import pipeline
3
 
4
- get_completion = pipeline("image-to-text",model="Salesforce/blip-image-captioning-base")
 
5
 
6
- def captioner(input):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  output = get_completion(input)
8
  return output[0]['generated_text']
9
 
10
  gr.close_all()
11
- demo = gr.Interface(fn=captioner,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  inputs=[gr.Image(label="Upload image", type="pil")],
13
  outputs=[gr.Textbox(label="Caption")],
14
- title="Image Captioning with BLIP model",
15
- description="Caption any image using the `Salesforce/blip-image-captioning-base` model ",
16
  allow_flagging="never")
17
 
18
  demo.launch()
 
1
  import gradio as gr
2
+ from PIL import Image
3
+ import spaces
4
  from transformers import pipeline
5
 
6
+ # Initialize Model
7
+ get_completion = pipeline("image-to-text",model="Salesforce/blip-image-captioning-base", device=0)
8
 
9
+ # def captioner(input):
10
+ # output = get_completion(input)
11
+ # return output[0]['generated_text']
12
+
13
+ @spaces.GPU(duration=120)
14
+ def captioner(input: Image.Image) -> str:
15
+ """
16
+ Generate a caption for the given image using the BLIP-IMAGE-CAPTIONING-BASE model.
17
+
18
+ Args:
19
+ input (Image.Image): The input image for which to generate a caption.
20
+
21
+ Returns:
22
+ str: The generated caption text.
23
+ """
24
  output = get_completion(input)
25
  return output[0]['generated_text']
26
 
27
  gr.close_all()
28
+
29
+ ####### GRADIO APP #######
30
+ title = """<h1 id="title"> Image Captioning with BLIP model </h1>"""
31
+
32
+ description = """
33
+ - The model used for Generating Captions [BLIP-IMAGE-CAPTIONING-BASE](https://huggingface.co/Salesforce/blip-image-captioning-base).
34
+ """
35
+
36
+ css = '''
37
+ h1#title {
38
+ text-align: center;
39
+ }
40
+ '''
41
+
42
+ theme = gr.themes.Soft()
43
+ demo = gr.Blocks(css=css, theme=theme)
44
+
45
+ with demo:
46
+ gr.Markdown(title)
47
+ gr.Markdown(description)
48
+ interface = gr.Interface(fn=captioner,
49
  inputs=[gr.Image(label="Upload image", type="pil")],
50
  outputs=[gr.Textbox(label="Caption")],
 
 
51
  allow_flagging="never")
52
 
53
  demo.launch()