Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import numpy as np | |
| from PIL import Image | |
| import tensorflow as tf | |
| # Load the TFLite model | |
| interpreter = tf.lite.Interpreter(model_path=r"model.tflite") | |
| interpreter.allocate_tensors() | |
| # Get input and output details | |
| input_details = interpreter.get_input_details() | |
| output_details = interpreter.get_output_details() | |
| CLASS_NAMES = ['Abyssinian', | |
| 'Bengal', | |
| 'Birman', | |
| 'Bombay', | |
| 'British_Shorthair', | |
| 'Egyptian_Mau', | |
| 'Maine_Coon', | |
| 'Persian', | |
| 'Ragdoll', | |
| 'Russian_Blue', | |
| 'Siamese', | |
| 'Sphynx', | |
| 'american_bulldog', | |
| 'american_pit_bull_terrier', | |
| 'basset_hound', | |
| 'beagle', | |
| 'boxer', | |
| 'chihuahua', | |
| 'english_cocker_spaniel', | |
| 'english_setter', | |
| 'german_shorthaired', | |
| 'great_pyrenees', | |
| 'havanese', | |
| 'japanese_chin', | |
| 'keeshond', | |
| 'leonberger', | |
| 'miniature_pinscher', | |
| 'newfoundland', | |
| 'pomeranian', | |
| 'pug', | |
| 'saint_bernard', | |
| 'samoyed', | |
| 'scottish_terrier', | |
| 'shiba_inu', | |
| 'staffordshire_bull_terrier', | |
| 'wheaten_terrier', | |
| 'yorkshire_terrier'] | |
| IMAGE_SIZE = 256 | |
| def predict_image(image: Image.Image): | |
| image = image.convert("RGB") | |
| image = image.resize((IMAGE_SIZE, IMAGE_SIZE)) | |
| image_np = np.array(image) / 255.0 | |
| img_batch = np.expand_dims(image_np, 0).astype(np.float32) | |
| interpreter.set_tensor(input_details[0]['index'], img_batch) | |
| interpreter.invoke() | |
| output = interpreter.get_tensor(output_details[0]['index'])[0] | |
| predicted_class = CLASS_NAMES[np.argmax(output)] | |
| confidence = float(np.max(output)) * 100 | |
| return predicted_class, f"{confidence:.1f}%" | |
| # Gradio interface | |
| interface = gr.Interface( | |
| fn=predict_image, | |
| inputs=gr.Image(type="pil"), | |
| outputs=[gr.Label(label="Predicted Class"), gr.Label(label="Confidence")], | |
| title="Cat & Dog Breed Classifier", | |
| description="Upload an image of a cat or dog to identify its breed." | |
| ) | |
| if __name__ == "__main__": | |
| interface.launch() | |