import gradio as gr
from transformers import AutoProcessor, AutoModelForCausalLM, AutoConfig


def inference(img):
    pretrained_model_path = "git-large-coco"

    processor = AutoProcessor.from_pretrained(pretrained_model_path)
    model = AutoModelForCausalLM.from_pretrained(pretrained_model_path)

    pixel_values = processor(images=img, return_tensors="pt").pixel_values

    generated_ids = model.generate(pixel_values=pixel_values, max_length=50)
    generated_caption = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
    return generated_caption

title = "Image to text:git-large-coco"
article = "<p style='text-align: center'><a href='https://github.com/bryandlee/animegan2-pytorch' target='_blank'>Github Repo Pytorch</a></p> <center><img src='https://visitor-badge.glitch.me/badge?page_id=akhaliq_animegan' alt='visitor badge'></center></p>"
examples=[['example_cat.jpg'],['Masahiro.png']]

with gr.Blocks() as demo:
    gr.Markdown(
        """
    # Image to text:git-large-coco
    这是一个git-large-coco的Gradio Demo。 上传你想要的图像或者点击下面的示例来加载它。
    """)
    with gr.Row():
        text_input = gr.Image()
        text_output = gr.Textbox()
    image_button = gr.Button("上传")
    image_button.click(inference, inputs=text_input, outputs=text_output)
    gr.Examples(examples, inputs=text_input)

demo.launch()
