Spaces:
Sleeping
Sleeping
| 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() | |