File size: 1,957 Bytes
2f1ea34
9d0734f
c05c698
 
03cd4ef
eb950ec
561cfe3
9d0734f
2f1ea34
c05c698
 
e1e57f7
c05c698
ccbf183
c05c698
 
 
 
 
 
 
 
 
 
 
 
 
eb950ec
 
 
 
 
2f1ea34
eb950ec
 
 
 
 
 
 
 
 
 
 
6a20c9d
 
 
 
 
 
 
 
 
 
 
eb950ec
2fc8e08
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import gradio as gr
import requests, json
import os
import io
import IPython.display
from PIL import Image
#import base64 



#Image-to-text endpoint
def get_completion(inputs, parameters=None, ENDPOINT_URL="http://internal-aws-prod-internal-revproxy-alb-11660607.us-west-1.elb.amazonaws.com/rev-proxy/huggingface/itt"): 
    headers = {
      "Authorization": f"Bearer {HF_TOKENS}",
      "Content-Type": "application/json"
    }
    data = { "inputs": inputs }
    if parameters is not None:
        data.update({"parameters": parameters})
    response = requests.request("POST",
                                ENDPOINT_URL,
                                headers=headers,
                                data=json.dumps(data))
    return json.loads(response.content.decode("utf-8"))



def image_to_base64_str(pil_image):
    byte_arr = io.BytesIO()
    pil_image.save(byte_arr, format='PNG')
    byte_arr = byte_arr.getvalue()
    return str(base64.b64encode(byte_arr).decode('utf-8'))

def captioner(image):
    base64_image = image_to_base64_str(image)
    result = get_completion(base64_image)
    return result[0]['generated_text']

gr.close_all()
demo = gr.Interface(fn=captioner,
                    inputs=[gr.Image(label="Upload image", type="pil")],
                    outputs=[gr.Textbox(label="Caption")],
                    title="Image Captioning with BLIP",
                    description="Caption any image using the BLIP model",
                    allow_flagging="never")



#demo = gr.Interface(fn=captioner,
 #                   inputs=[gr.Image(label="Upload image", type="pil")],
 #//                   outputs=[gr.Textbox(label="Caption")],
 # //                  title="Image Captioning with BLIP",
 #  //                 description="Caption any image using the BLIP model",
 #   //                allow_flagging="never",
 #    //               examples=["christmas_dog.jpeg", "bird_flight.jpeg", "cow.jpeg"])

demo.launch()