image-captioner / app.py
Eyob-Sol's picture
Update app.py
61cc01d verified
Raw
History Blame Contribute Delete
1.32 kB
#!/usr/bin/env python3
import warnings
warnings.filterwarnings("ignore", message="Using a slow image processor as `use_fast` is unset.*")
warnings.filterwarnings("ignore", category=FutureWarning, message=".*AutoModelForVision2Seq.*")
import gradio as gr
from pathlib import Path
from app.captioner import get_captioner
cap = get_captioner()
SAMPLES_DIR = Path("./samples") #/.resolve().parents[1]
EXAMPLES = [
str(SAMPLES_DIR / "test1.jpg"),
str(SAMPLES_DIR / "test2.jpg"),
]
def caption_fn(image):
# image is a PIL.Image from gradio
return cap.generate_caption(image)
with gr.Blocks(title="Image Captioner (CPU-friendly)") as demo:
gr.Markdown("# 🖼️ Image Captioner\nUpload an image to get a concise caption.\n")
with gr.Row():
inp = gr.Image(type="pil", label="Input image")
out = gr.Textbox(label="Caption", lines=3)
btn = gr.Button("Generate caption", variant="primary")
btn.click(caption_fn, inputs=inp, outputs=out)
gr.Examples(EXAMPLES, inputs=[inp], outputs=[out], examples_per_page=2)
gr.Markdown(
"Tip: This demo uses a lightweight model by default (CPU). "
"You can switch models/devices via `.env` without code changes."
)
if __name__ == "__main__":
demo.queue().launch() # add share=True if you want a public link