Pranav0gp commited on
Commit
ac39532
·
1 Parent(s): 1b4050c

Text Captioner

Browse files
Files changed (1) hide show
  1. app.py +42 -0
app.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ API_URL = "https://api-inference.huggingface.co/models/Salesforce/blip-image-captioning-base"
2
+
3
+ # Helper functions
4
+ import requests, json
5
+
6
+ #Image-to-text endpoint
7
+ def get_completion(inputs, parameters=None, ENDPOINT_URL=API_URL):
8
+ hf_api_key = "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"
9
+ headers = {
10
+ "Content-Type": "application/json"
11
+ }
12
+ data = { "inputs": inputs }
13
+ if parameters is not None:
14
+ data.update({"parameters": parameters})
15
+ response = requests.request("POST",
16
+ ENDPOINT_URL,
17
+ headers=headers,
18
+ data=json.dumps(data))
19
+ return json.loads(response.content.decode("utf-8"))
20
+ import gradio as gr
21
+
22
+ def image_to_base64_str(pil_image):
23
+ byte_arr = io.BytesIO()
24
+ pil_image.save(byte_arr, format='PNG')
25
+ byte_arr = byte_arr.getvalue()
26
+ return str(base64.b64encode(byte_arr).decode('utf-8'))
27
+
28
+ def captioner(image):
29
+ base64_image = image_to_base64_str(image)
30
+ result = get_completion(base64_image)
31
+ return result[0]['generated_text']
32
+
33
+ gr.close_all()
34
+ demo = gr.Interface(fn=captioner,
35
+ inputs=[gr.Image(label="Upload image", type="pil")],
36
+ outputs=[gr.Textbox(label="Caption")],
37
+ title="Image Captioning with BLIP",
38
+ description="Caption any image using the BLIP model",
39
+ allow_flagging="never",
40
+ examples=["/kaggle/input/images/dry land.jpg", "/kaggle/input/images/hills.jpg", "/kaggle/input/images/house.jpg"])
41
+
42
+ demo.launch(inline=False)