Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,32 +1,55 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
from PIL import Image
|
| 4 |
|
| 5 |
-
#
|
| 6 |
model = pipeline("image-classification", model="wellCh4n/tomato-leaf-disease-classification-vit", top_k=10)
|
| 7 |
|
|
|
|
| 8 |
def classify(image):
|
|
|
|
|
|
|
|
|
|
| 9 |
if not isinstance(image, Image.Image):
|
| 10 |
image = Image.fromarray(image)
|
| 11 |
-
|
| 12 |
-
# The model returns a list of dictionaries, e.g., [{'label': '...', 'score': ...}]
|
| 13 |
predictions = model(image)
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
# {'label_string': score_float, ...}
|
| 17 |
confidences = {p['label']: p['score'] for p in predictions}
|
| 18 |
|
| 19 |
return confidences
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
#
|
| 32 |
app.launch(show_api=True)
|
|
|
|
| 1 |
+
|
| 2 |
import gradio as gr
|
| 3 |
from transformers import pipeline
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
+
# Memuat model tetap sama
|
| 7 |
model = pipeline("image-classification", model="wellCh4n/tomato-leaf-disease-classification-vit", top_k=10)
|
| 8 |
|
| 9 |
+
# Fungsi klasifikasi tetap sama
|
| 10 |
def classify(image):
|
| 11 |
+
if image is None:
|
| 12 |
+
# Menambahkan pengecekan untuk gambar kosong untuk stabilitas
|
| 13 |
+
return None
|
| 14 |
if not isinstance(image, Image.Image):
|
| 15 |
image = Image.fromarray(image)
|
| 16 |
+
|
|
|
|
| 17 |
predictions = model(image)
|
| 18 |
|
| 19 |
+
# Mengubah format output agar sesuai dengan yang diharapkan oleh gr.Label
|
|
|
|
| 20 |
confidences = {p['label']: p['score'] for p in predictions}
|
| 21 |
|
| 22 |
return confidences
|
| 23 |
|
| 24 |
+
# --- PERUBAHAN UTAMA: Membangun UI dengan gr.Blocks ---
|
| 25 |
+
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 26 |
+
gr.Markdown(
|
| 27 |
+
"""
|
| 28 |
+
# Analisis Penyakit Tanaman 🍅
|
| 29 |
+
Unggah gambar daun tanaman untuk mengidentifikasi potensi penyakit.
|
| 30 |
+
"""
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
with gr.Row():
|
| 34 |
+
with gr.Column():
|
| 35 |
+
# 1. Definisikan komponen input gambar secara manual
|
| 36 |
+
image_input = gr.Image(type="pil", label="Unggah Gambar Daun")
|
| 37 |
+
|
| 38 |
+
# 2. Definisikan tombol untuk memulai analisis
|
| 39 |
+
submit_button = gr.Button("Analisis Gambar", variant="primary")
|
| 40 |
+
|
| 41 |
+
with gr.Column():
|
| 42 |
+
# 3. Definisikan komponen output label secara manual
|
| 43 |
+
label_output = gr.Label(label="Hasil Analisis")
|
| 44 |
+
|
| 45 |
+
# 4. Hubungkan aksi tombol 'click' dengan fungsi 'classify'
|
| 46 |
+
# Di sinilah kita mendefinisikan API secara eksplisit dan solid
|
| 47 |
+
submit_button.click(
|
| 48 |
+
fn=classify,
|
| 49 |
+
inputs=image_input,
|
| 50 |
+
outputs=label_output,
|
| 51 |
+
api_name="predict" # Nama API sekarang terikat pada aksi, bukan interface
|
| 52 |
+
)
|
| 53 |
|
| 54 |
+
# Meluncurkan aplikasi tetap sama
|
| 55 |
app.launch(show_api=True)
|