Spaces:
Build error
Build error
create care label demo app
Browse files- app.py +48 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import io
|
| 2 |
+
import base64
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
import requests
|
| 6 |
+
from PIL import Image
|
| 7 |
+
|
| 8 |
+
import numpy as np
|
| 9 |
+
import gradio as gr
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def 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 |
+
|
| 23 |
+
def 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_url
|
| 30 |
+
}
|
| 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_response
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
iface = 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 |
+
)
|
| 48 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
numpy
|
| 2 |
+
gradio
|
| 3 |
+
pillow
|