|
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 |
|
|
|
|
|
model_url = "https://huggingface.co/diabolic6045/indian_cities_image_classification/resolve/main/model.h5" |
|
model_path = "model.h5" |
|
|
|
|
|
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") |
|
|
|
model = load_model(model_path, compile=False) |
|
|
|
|
|
model.compile(optimizer=Adam(), loss="categorical_crossentropy") |
|
|
|
|
|
class_labels = ['Ahmedabad', 'Delhi', 'Kerala', 'Kolkata', 'Mumbai'] |
|
|
|
|
|
def classify_city(img): |
|
|
|
img = img.resize((175, 175)) |
|
img = image.img_to_array(img) |
|
img = np.expand_dims(img, axis=0) |
|
img = img / 175.0 |
|
|
|
|
|
predictions = model.predict(img) |
|
predicted_class = np.argmax(predictions) |
|
predicted_city = class_labels[predicted_class] |
|
|
|
return f"Predicted City: {predicted_city}" |
|
|
|
|
|
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.", |
|
) |
|
|
|
|
|
iface.launch() |
|
|
|
|