Spaces:
Runtime error
Runtime error
Download app.py from danendradipa/cat-verse-api: direct link, hf CLI and curl.
- Browser
- Download file 1.41 kB
-
https://huggingface.co/spaces/danendradipa/cat-verse-api/resolve/main/app.py
- Command line
-
hf download hf://spaces/danendradipa/cat-verse-api/app.py
-
curl -L -o app.py https://huggingface.co/spaces/danendradipa/cat-verse-api/resolve/main/app.py
1.41 kB
| import gradio as gr | |
| import tensorflow as tf | |
| import numpy as np | |
| from PIL import Image | |
| # Inisialisasi model secara global sebagai None | |
| model = None | |
| classes = ["Abyssinian", "Bengal", "Bombay", "British Shorthair", "Domestic", | |
| "Maine Coon", "Persian", "Ragdoll", "Siamese", "Sphynx"] | |
| def predict_cat(image): | |
| global model | |
| if image is None: | |
| return "Error", 0.0 | |
| # Memuat model secara Lazy Loading di dalam fungsi agar aman saat startup | |
| if model is None: | |
| model = tf.keras.models.load_model('cat_model.keras', compile=False) | |
| if image.mode != 'RGB': | |
| image = image.convert('RGB') | |
| img = image.resize((224, 224)) | |
| img_array = np.array(img) / 255.0 | |
| img_array = np.expand_dims(img_array, axis=0) | |
| # Inferensi dijalankan dengan aman | |
| prediction = model.predict(img_array) | |
| predicted_class = np.argmax(prediction, axis=1)[0] | |
| confidence_score = float(np.max(prediction) * 100) | |
| breed = classes[predicted_class] | |
| return breed, confidence_score | |
| # Pastikan nama variabel demo menggunakan 'demo' alih-alih 'iface' | |
| demo = gr.Interface( | |
| fn=predict_cat, | |
| inputs=gr.Image(type="pil"), | |
| outputs=[gr.Textbox(label="Breed"), gr.Number(label="Confidence")], | |
| title="Cat Breed Classifier" | |
| ) | |
| # Tambahkan parameter server_name dan port secara eksplisit | |
| demo.launch(server_name="0.0.0.0", server_port=7860) | |