File size: 1,134 Bytes
143710c
 
 
 
 
 
 
 
 
 
 
 
da4295d
 
f2a7558
da4295d
143710c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"""Gradio app for Hugging Face Spaces: upload an image, get a generated caption."""
from __future__ import annotations

import spaces
import gradio as gr
from huggingface_hub import hf_hub_download

from src.inference.predict import Predictor

MODEL_REPO_ID = "AdhamAshraf/image-caption-generator"

print("Downloading model artifacts from Hugging Face Hub...")
checkpoint_path = hf_hub_download(
    repo_id=MODEL_REPO_ID,
    filename="resnet_attention_lstm/best_model.pt",
)
vocab_path = hf_hub_download(repo_id=MODEL_REPO_ID, filename="vocab.json")

predictor = Predictor(checkpoint_path=checkpoint_path, vocab_path=vocab_path, device="cpu")
print("Model loaded.")


@spaces.GPU
def generate_caption(image):
    if image is None:
        return "Please upload an image."
    return predictor.predict(image)


demo = gr.Interface(
    fn=generate_caption,
    inputs=gr.Image(type="pil", label="Upload an image"),
    outputs=gr.Textbox(label="Generated Caption"),
    title="🖼️ Image Caption Generator",
    description="ResNet50 (frozen) + LSTM baseline, trained on Flickr8k.",
)

if __name__ == "__main__":
    demo.launch()