File size: 830 Bytes
5f6a9dc
3ba4276
 
 
5f6a9dc
3ba4276
 
 
 
 
 
 
 
 
 
94fb728
3ba4276
5f6a9dc
 
 
 
3ba4276
5f6a9dc
94fb728
3ba4276
 
5f6a9dc
3ba4276
5f6a9dc
3ba4276
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
import io
import os

import cv2
import PIL
import requests

class ImageCaptioning:
    """
    Performing an API call to BLIP's huggingface inference API
    """
    def __init__(self):
        self.api_endpoint = os.environ["blip_api_url"]
        self.org_token = os.environ["auth_token"]
        self.headers = { "Authorization": f"Bearer {self.org_token}" }
        self.session = requests.session()
    
    def convert_to_bytes(self, image: PIL.Image.Image):
        data = io.BytesIO()
        image.save(data, format="PNG")
        return data.getvalue()

    def query(self, image: PIL.Image.Image):
        response = self.session.post(
            self.api_endpoint,
            headers=self.headers,
            data=self.convert_to_bytes(image)
        )
        print(response.json())
        return response.json()