Spaces:
Running
Running
| import gradio as gr | |
| import tensorflow as tf | |
| import numpy as np | |
| from PIL import Image | |
| # Load the model (make sure the file is named my_modal.h5) | |
| model = tf.keras.models.load_model("my_modal.h5") | |
| class_names = ['Class 1', 'Class 2', 'Class 3'] # Update with your real classes | |
| # Define prediction function | |
| def predict(image): | |
| image = image.resize((224, 224)) # Resize to model input shape | |
| img_array = np.array(image) / 255.0 | |
| img_array = img_array.reshape((1, 224, 224, 3)) | |
| prediction = model.predict(img_array) | |
| predicted_class = class_names[np.argmax(prediction)] | |
| confidence = float(np.max(prediction)) | |
| return {predicted_class: confidence} | |
| # Create Gradio interface | |
| gr.Interface( | |
| fn=predict, | |
| inputs=gr.Image(type="pil"), | |
| outputs=gr.Label(num_top_classes=3), | |
| title="My ML Model" | |
| ).launch() | |