Shaffique Aljoofri commited on
Commit
e55c61a
·
1 Parent(s): 41b50cd

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -0
app.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import io
3
+ import IPython.display
4
+ from PIL import Image
5
+ import base64
6
+ from dotenv import load_dotenv, find_dotenv
7
+ _ = load_dotenv(find_dotenv()) # read local .env file
8
+
9
+ hf_api_key = os.environ['HF_API_KEY']
10
+
11
+ # Helper functions
12
+ import requests, json
13
+
14
+ #Image-to-text endpoint
15
+ def get_completion(inputs, parameters = None, ENDPOINT_URL = os.environ['HF_API_ITT_BASE']):
16
+ headers = {
17
+ "Authorization": f"Bearer {hf_api_key}",
18
+ "Content-Type": "application/json"
19
+ }
20
+ data = { "inputs": inputs }
21
+ if parameters is not None:
22
+ data.update({"parameters": parameters})
23
+ response = requests.request("POST",
24
+ ENDPOINT_URL,
25
+ headers=headers,
26
+ data=json.dumps(data))
27
+ return json.loads(response.content.decode("utf-8"))
28
+
29
+ # Gradio App
30
+
31
+ import gradio as gr
32
+
33
+ def image_to_base64_str(pil_image):
34
+ byte_arr = io.BytesIO()
35
+ pil_image.save(byte_arr, format='PNG')
36
+ byte_arr = byte_arr.getvalue()
37
+ return str(base64.b64encode(byte_arr).decode('utf-8'))
38
+
39
+ def captioner(image):
40
+ base64_image = image_to_base64_str(image)
41
+ result = get_completion(base64_image)
42
+ return result[0]['generated_text']
43
+
44
+ gr.close_all()
45
+ demo = gr.Interface(fn=captioner,
46
+ inputs=[gr.Image(label="Upload image", type="pil")],
47
+ outputs=[gr.Textbox(label="Caption")],
48
+ title="Image Captioning with BLIP",
49
+ description="Caption any image using the BLIP model",
50
+ allow_flagging="never",
51
+ examples=["christmas_dog.jpeg", "bird_flight.jpeg", "cow.jpeg"])
52
+
53
+ demo.launch()
54
+