发票抽取问答、海报抽取问答、网页抽取问答、表格抽取问答、试卷抽取问答。
from pprint import pprint
from paddlenlp import Taskflow
import gradio as gr
from paddlenlp import Taskflow
import numpy as np
from PIL import Image
import uuid
docprompt = Taskflow("document_intelligence")
def model_inference(image, prompt):
prompt = prompt.split("?")[:-1]
# TODO:非得要个网络或本地地址,ndarray不行
img = Image.fromarray(np.uint8(image))
filename = "./image/" + str(uuid.uuid4()) + ".png"
img.save(filename)
res = docprompt([{"doc": filename, "prompt": prompt}])
json_out = {"result": res}
return image,json_out
def clear_all():
return None, None, None
with gr.Blocks() as demo:
gr.Markdown("ERNIE-Layout")
with gr.Column(scale=1, min_width=100):
img_in = gr.Image(value="https://bj.bcebos.com/paddlenlp/taskflow/document_intelligence/images/invoice.jpg",
label="Input")
text = gr.Textbox(
value="发票号码是多少?校验码是多少?",
label="输入问题:",
lines=2)
with gr.Row():
btn1 = gr.Button("Clear")
btn2 = gr.Button("Submit")
json_out = gr.JSON(label="Information Extraction Output")
img_out = gr.Image(label="Output").style(height=400)
btn1.click(fn=clear_all, inputs=None, outputs=[img_in, img_out, json_out])
btn2.click(fn=model_inference, inputs=[img_in, text], outputs=[img_out,json_out])
gr.Button.style(1)
demo.launch(server_port=7007)