|
import tensorflow as tf |
|
import numpy as np |
|
from PIL import Image |
|
import gradio as gr |
|
|
|
|
|
model = tf.keras.models.load_model("model.h5") |
|
|
|
|
|
def preprocess_image(image): |
|
|
|
image = image.resize((224, 224)) |
|
image_array = np.array(image) / 255.0 |
|
image_array = np.expand_dims(image_array, axis=0) |
|
return image_array |
|
|
|
|
|
def generate_caption(image): |
|
preprocessed_image = preprocess_image(image) |
|
|
|
|
|
|
|
caption_tokens = model.predict(preprocessed_image) |
|
|
|
return caption_tokens |
|
|
|
|
|
iface = gr.Interface( |
|
fn=generate_caption, |
|
inputs=gr.Image(type="pil"), |
|
outputs="text", |
|
title="Image Captioning Model", |
|
description="Upload an image, and the model will generate a caption describing it." |
|
) |
|
|
|
iface.launch(share=True) |
|
|