File size: 1,165 Bytes
0282d40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import io
import base64
import os

import requests
from PIL import Image

import numpy as np
import gradio as gr


def image_to_base64(image):
    if isinstance(image, np.ndarray):
        image = Image.fromarray(image)

    buffered = io.BytesIO()
    image.save(buffered, format="JPEG")
    img_str = base64.b64encode(buffered.getvalue()).decode()

    return f"data:image/jpeg;base64,{img_str}"


def process_image(image: Image.Image):
    base64_url = image_to_base64(image)
    api_hostname = os.getenv('CARE_LABEL_API', 'http://0.0.0.0:8000')
    response = requests.post(
        url=f'{api_hostname}/v1/care-label/extract-info',
        json={
            "imageUrl": base64_url
        }
    )
    json_response = response.json()
    for key, value in json_response.items():
        if isinstance(value, str):
            json_response[key] = value.replace('\n', '<br>')
    return json_response


iface = gr.Interface(
    fn=process_image,
    inputs="image",
    outputs="json",
    title='Care Label - Information Extraction',
    description='The demo to extract care instruction from care label image.',
    allow_flagging='never',

)
iface.launch()