File size: 1,151 Bytes
96bc662
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import numpy as np
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import image

# Load model
model = load_model("models.h5")

# Class labels
class_names = ['daisy', 'dandelion', 'rose', 'sunflower', 'tulip']

# Prediction function
def predict_flower(img):
    img = img.resize((224, 224))  # Resize to match training input
    img_array = image.img_to_array(img)
    img_array = img_array / 255.0  # Normalize
    img_array = np.expand_dims(img_array, axis=0)  # Add batch dimension

    predictions = model.predict(img_array)
    class_index = np.argmax(predictions)
    confidence = float(np.max(predictions))

    return {class_names[i]: float(predictions[0][i]) for i in range(5)}

# Gradio interface
interface = gr.Interface(
    fn=predict_flower,
    inputs=gr.Image(type="pil"),
    outputs=gr.Label(num_top_classes=5),
    title="Flower Classifier",
    description="Upload a flower image and the model will classify it as daisy, dandelion, rose, sunflower, or tulip.",
    allow_flagging="never"
)

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