Spaces:
Runtime error
Runtime error
Download app.py from trs/Image_Classification_with_pre-traind_Models: direct link, hf CLI and curl.
- Browser
- Download file 2.42 kB
-
https://huggingface.co/spaces/trs/Image_Classification_with_pre-traind_Models/resolve/ae76df704c97d9cdde709a2b2d04979c79065a8a/app.py
- Command line
-
hf download hf://spaces/trs/Image_Classification_with_pre-traind_Models@ae76df704c97d9cdde709a2b2d04979c79065a8a/app.py
-
curl -L -o app.py https://huggingface.co/spaces/trs/Image_Classification_with_pre-traind_Models/resolve/ae76df704c97d9cdde709a2b2d04979c79065a8a/app.py
2.42 kB
| import gradio as gr | |
| import cv2 | |
| import numpy as np | |
| from tensorflow.keras.models import load_model | |
| from transformers import AutoTokenizer | |
| # Define class names corresponding to your datasets | |
| class_names = ["Ajloun Castle", "Jeresh", "Petra", "Roman amphitheater", "Umm_Qais", "Wadi_Rum"] # Replace with your actual class names | |
| # Define the Hugging Face Model Hub names for your models | |
| resnet_model_path = './resnet50-saved-model-79-val_acc-1.00.hdf5' | |
| efficientnet_model_path = './efficientnet-saved-model-06-val_acc-0.98.hdf5' | |
| vggnet_model_path = './vgg_16_-saved-model-89-acc-0.93.hdf5' | |
| inceptionv3_model_path = './inceptionv3_-saved-model-59-val_accuracy-0.94.hdf5' | |
| # Load models | |
| resnet_model = load_model(resnet_model_path) | |
| efficientnet_model = load_model(efficientnet_model_path) | |
| vggnet_model = load_model(vggnet_model_path) | |
| inceptionv3_model = load_model(inceptionv3_model_path) | |
| # Define the tokenizers | |
| resnet_tokenizer = AutoTokenizer.from_pretrained("resnet_model") # Replace with the actual transformer model name | |
| efficientnet_tokenizer = AutoTokenizer.from_pretrained("efficientnet_model") | |
| vggnet_tokenizer = AutoTokenizer.from_pretrained("vggnet_model") | |
| inceptionv3_tokenizer = AutoTokenizer.from_pretrained("inceptionv3_model") | |
| # Define the classify_image function | |
| def classify_image(img, model_choice): | |
| try: | |
| img = cv2.resize(img, (150, 150)) | |
| img = np.expand_dims(img, axis=0) | |
| img = img.astype(np.float32) / 255.0 | |
| if model_choice == "ResNet": | |
| predictions = resnet_model.predict(img) | |
| elif model_choice == "EfficientNet": | |
| predictions = efficientnet_model.predict(img) | |
| elif model_choice == "VGGNet": | |
| predictions = vggnet_model.predict(img) | |
| elif model_choice == "InceptionV3": | |
| predictions = inceptionv3_model.predict(img) | |
| else: | |
| return "Invalid model choice" | |
| class_idx = np.argmax(predictions[0]) | |
| class_name = class_names[class_idx] | |
| return f"Predicted class using {model_choice}: {class_name}" | |
| except Exception as e: | |
| return f"Error: {str(e)}" | |
| # Create a Gradio interface with a dropdown for model selection | |
| iface = gr.Interface( | |
| fn=classify_image, | |
| inputs=["image", gr.Dropdown(["ResNet", "EfficientNet", "VGGNet", "InceptionV3"], label="Select Model")], | |
| outputs="text", | |
| live=True | |
| ) | |
| # Launch the Gradio interface | |
| iface.launch() | |