CoolFace
Apppublic

creativeforce/CareLabel

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py49 linesDownload Raw Back to root
1import io2import base643import os4 5import requests6from PIL import Image7 8import numpy as np9import gradio as gr10 11 12def image_to_base64(image):13    if isinstance(image, np.ndarray):14        image = Image.fromarray(image)15 16    buffered = io.BytesIO()17    image.save(buffered, format="JPEG")18    img_str = base64.b64encode(buffered.getvalue()).decode()19 20    return f"data:image/jpeg;base64,{img_str}"21 22 23def process_image(image: Image.Image):24    base64_url = image_to_base64(image)25    api_hostname = os.getenv('CARE_LABEL_API', 'http://0.0.0.0:8000')26    response = requests.post(27        url=f'{api_hostname}/v1/care-label/extract-info',28        json={29            "imageUrl": base64_url30        }31    )32    json_response = response.json()33    for key, value in json_response.items():34        if isinstance(value, str):35            json_response[key] = value.replace('\n', '<br>')36    return json_response37 38 39iface = gr.Interface(40    fn=process_image,41    inputs="image",42    outputs="json",43    title='Care Label - Information Extraction',44    description='The demo to extract care instruction from care label image.',45    allow_flagging='never',46 47)48iface.launch()49