import gradio as gr import numpy as np import os os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0' from tensorflow.keras.preprocessing import image from huggingface_hub import from_pretrained_keras import requests # URL of the model file (adjust if needed) model_url = "https://huggingface.co/diabolic6045/indian_cities_image_classification/resolve/main/model.h5" model_path = "model.h5" # Download the model if it doesn't exist if not os.path.exists(model_path): print("Downloading the model...") response = requests.get(model_url) with open(model_path, "wb") as f: f.write(response.content) print("Model downloaded.") from tensorflow.keras.models import load_model from tensorflow.keras.optimizers import Adam print("loading model") # Load the model, ignoring the optimizer argument model = load_model(model_path, compile=False) # Recompile the model with a valid optimizer model.compile(optimizer=Adam(), loss="categorical_crossentropy") # Define the class labels class_labels = ['Ahmedabad', 'Delhi', 'Kerala', 'Kolkata', 'Mumbai'] # Function to preprocess the image and predict the city def classify_city(img): # Preprocess the image img = img.resize((175, 175)) img = image.img_to_array(img) img = np.expand_dims(img, axis=0) img = img / 175.0 # Normalize the image # Make predictions predictions = model.predict(img) predicted_class = np.argmax(predictions) predicted_city = class_labels[predicted_class] return f"Predicted City: {predicted_city}" # Gradio Interface iface = gr.Interface( fn=classify_city, inputs=gr.Image(type="pil", label="Upload an image of an Indian city"), outputs=gr.Textbox(label="Predicted City"), title="Indian Cities Image Classification", description="Upload an image of a city in India, and the model will predict which city it is: Ahmedabad, Delhi, Kerala, Kolkata, or Mumbai.", ) # Launch the Gradio app iface.launch()