soilnet-app / app.py
arpitsharrrma's picture
Update app.py
4d7735c verified
raw
history blame
1.17 kB
import gradio as gr
import tensorflow as tf
from huggingface_hub import hf_hub_download
from PIL import Image
import numpy as np
# Load model from Hugging Face Hub
model_path = hf_hub_download(repo_id="arpitsharrrma/soilnet-model", filename="SoilNet.keras")
model = tf.keras.models.load_model(model_path)
# Define class labels (adjust if needed)
class_names = ['Alluvial Soil', 'Black Soil', 'Clay Soil', 'Red Soil', 'Sandy Soil']
def predict_soil(image):
image = image.resize((150, 150)) # Model input size
img_array = np.array(image) / 255.0
img_array = img_array.reshape(1, 150, 150, 3)
predictions = model.predict(img_array)
predicted_class = class_names[np.argmax(predictions)]
confidence = float(np.max(predictions)) * 100
return f"{predicted_class} ({confidence:.2f}% confidence)"
# Gradio Interface
interface = gr.Interface(
fn=predict_soil,
inputs=gr.Image(type="pil", label="Upload Soil Image"),
outputs=gr.Textbox(label="Predicted Soil Type"),
title="SoilNet - Soil Type Classifier",
description="Upload a soil image and the model will predict the soil type using deep learning."
)
interface.launch()