45 lines
1.4 KiB
Python
45 lines
1.4 KiB
Python
from transformers import pipeline
|
|
import gradio as gr
|
|
from PIL import Image
|
|
from gradio.themes.utils import sizes
|
|
|
|
|
|
theme = gr.themes.Default(radius_size=sizes.radius_none).set(
|
|
block_label_text_color = '#4D63FF',
|
|
block_title_text_color = '#4D63FF',
|
|
button_primary_text_color = '#4D63FF',
|
|
button_primary_background_fill='#FFFFFF',
|
|
button_primary_border_color='#4D63FF',
|
|
button_primary_background_fill_hover='#EDEFFF',
|
|
)
|
|
|
|
|
|
nlp = pipeline(
|
|
"document-question-answering",
|
|
model="impira/layoutlm-document-qa",
|
|
)
|
|
|
|
def vqa(image, question):
|
|
inp = Image.fromarray(image.astype('uint8'), 'RGB')
|
|
|
|
return nlp(inp, question)
|
|
|
|
with gr.Blocks(theme=theme, css="footer {visibility: hidden}") as demo:
|
|
gr.Markdown("""
|
|
<div align='center' ><font size='60'>图片问答</font></div>
|
|
""")
|
|
with gr.Row():
|
|
with gr.Column():
|
|
image = gr.Image(label="图片")
|
|
question = gr.Textbox(label="问题")
|
|
with gr.Row():
|
|
button = gr.Button("提交", variant="primary")
|
|
box2 = gr.Textbox(label="文本")
|
|
|
|
button.click(fn=vqa, inputs=[image, question], outputs=box2)
|
|
examples = gr.Examples(examples=[['income.png', 'What are the 2020 net sales?'], ['invoice.png','What is the invoice number?']], inputs=[image, question], label="例子")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
demo.queue(concurrency_count=3).launch(server_name = "0.0.0.0")
|