import os.path import gdown import gradio as gr import torch from Model import TRCaptionNet, clip_transform model_ckpt = "./checkpoints/TRCaptionNet_L14_berturk_tasviret.pth" # device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') device = "cpu" preprocess = clip_transform(224) model = TRCaptionNet({ "max_length": 35, "clip": "ViT-L/14", "bert": "dbmdz/bert-base-turkish-cased", "proj": True, "proj_num_head": 16 }) model.load_state_dict(torch.load(model_ckpt, map_location=device)["model"], strict=True) model = model.to(device) model.eval() def inference(raw_image, min_length, repetition_penalty): batch = preprocess(raw_image).unsqueeze(0).to(device) caption = model.generate(batch, min_length=min_length, repetition_penalty=repetition_penalty)[0] return caption inputs = [gr.Image(type='pil', interactive=True,), gr.Slider(minimum=6, maximum=22, value=11, label="MINIMUM CAPTION LENGTH", step=1), gr.Slider(minimum=1, maximum=2, value=1.6, label="REPETITION PENALTY")] outputs = gr.components.Textbox(label="Caption") title = "TRCaptionNet" paper_link = "" github_link = "https://github.com/serdaryildiz/TRCaptionNet" description = f"
TRCaptionNet : A novel and accurate deep Turkish image captioning model with vision transformer based image encoders and deep linguistic text decoders" examples = [ ["images/test1.jpg"], ["images/test2.jpg"], ["images/test3.jpg"], ["images/test4.jpg"] ] article = f"
" css = ".output-image, .input-image, .image-preview {height: 600px !important}" iface = gr.Interface(fn=inference, inputs=inputs, outputs=outputs, title=title, description=description, examples=examples, article=article, css=css) iface.launch()