Spaces:
Build error
Build error
| 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() | |