File size: 835 Bytes
4113150
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()