import gradio as gr import tensorflow as tf from tensorflow.keras.preprocessing import image from huggingface_hub import hf_hub_download import numpy as np import os from PIL import Image MODEL_REPO = "zotthytt12/vegetable-classifier" MODEL_FILENAME = "model/veg_model.h5" # pobierz model z Hugging Face Hub print("Pobieranie modelu...") model_path = hf_hub_download(repo_id=MODEL_REPO, filename=MODEL_FILENAME) print("Model pobrany, ładowanie...") model = tf.keras.models.load_model(model_path) print("Model załadowany.") CLASS_NAMES = ['Bean', 'Bitter_Gourd', 'Bottle_Gourd', 'Brinjal', 'Broccoli', 'Cabbage', 'Capsicum', 'Carrot', 'Cauliflower', 'Cucumber', 'Papaya', 'Potato', 'Pumpkin', 'Radish', 'Tomato'] IMG_SIZE = (128, 128) def predict(img_path): img = Image.open(img_path) img = img.resize(IMG_SIZE) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) / 255.0 preds = model.predict(x) probs = preds[0] return {CLASS_NAMES[i]: float(probs[i]) for i in range(len(CLASS_NAMES))} iface = gr.Interface( fn=predict, inputs=gr.Image(type="filepath"), outputs=gr.Label(num_top_classes=3), title="Vegetable Classifier", description="Wgraj zdjęcie warzywa, a model powie co to jest.") if __name__ == "__main__": iface.launch(show_error=True)