Spaces:
Sleeping
Sleeping
import gradio as gr | |
from tensorflow.keras.models import load_model | |
from PIL import Image | |
import numpy as np | |
# Load your pre-trained model | |
model_path = 'my_model.h5' | |
model = load_model(model_path) | |
# Function to make predictions | |
def predict_image_to_text(image): | |
# Preprocess the input image (adjust based on your model's requirements) | |
img = Image.open(image) | |
# Resize the image to (224, 224) | |
img = img.resize((224, 224)) | |
# Convert the image to a NumPy array | |
img_array = np.array(img) | |
# Ensure the image has 3 channels (R, G, B) | |
if img_array.shape[-1] == 4: | |
img_array = img_array[:, :, :3] | |
# Normalize the image data if needed | |
img_array = img_array / 255.0 | |
# Add batch dimension | |
img_array = np.expand_dims(img_array, axis=0) | |
# Make prediction | |
prediction = model.predict(img_array)[0] | |
return prediction | |
# Gradio Interface | |
iface = gr.Interface( | |
fn=predict_image_to_text, | |
inputs=gr.Image(), # Gradio will automatically handle image input | |
outputs="text", | |
live=True, | |
) | |
# Launch the Gradio interface | |
iface.launch() | |