import gradio as gr import tensorflow as tf import numpy as np from PIL import Image # ==================== تحميل الموديل ==================== model = tf.keras.models.load_model("trained_model.h5") # ==================== أسماء الكلاسات ==================== class_names = [ 'Apple___Apple_scab', 'Apple___Black_rot', 'Apple___Cedar_apple_rust', 'Apple___healthy', 'Blueberry___healthy', 'Cherry_(including_sour)___Powdery_mildew', 'Cherry_(including_sour)___healthy', 'Corn_(maize)___Cercospora_leaf_spot Gray_leaf_spot', 'Corn_(maize)___Common_rust_', 'Corn_(maize)___Northern_Leaf_Blight', 'Corn_(maize)___healthy', 'Grape___Black_rot', 'Grape___Esca_(Black_Measles)', 'Grape___Leaf_blight_(Isariopsis_Leaf_Spot)', 'Grape___healthy', 'Orange___Haunglongbing_(Citrus_greening)', 'Peach___Bacterial_spot', 'Peach___healthy', 'Pepper,_bell___Bacterial_spot', 'Pepper,_bell___healthy', 'Potato___Early_blight', 'Potato___Late_blight', 'Potato___healthy', 'Raspberry___healthy', 'Soybean___healthy', 'Squash___Powdery_mildew', 'Strawberry___Leaf_scorch', 'Strawberry___healthy', 'Tomato___Bacterial_spot', 'Tomato___Early_blight', 'Tomato___Late_blight', 'Tomato___Leaf_Mold', 'Tomato___Septoria_leaf_spot', 'Tomato___Spider_mites Two-spotted_spider_mite', 'Tomato___Target_Spot', 'Tomato___Tomato_Yellow_Leaf_Curl_Virus', 'Tomato___Tomato_mosaic_virus', 'Tomato___healthy' ] # ==================== دالة التنبؤ ==================== def predict(image): # تحضير الصورة img = Image.fromarray(image).resize((128, 128)) input_arr = np.array(img, dtype=np.float32) input_arr = np.expand_dims(input_arr, axis=0) # تحويل لـ batch # التنبؤ predictions = model.predict(input_arr) result_index = int(np.argmax(predictions)) confidence = float(np.max(predictions)) # إرجاع أعلى 5 نتائج top5_indices = np.argsort(predictions[0])[::-1][:5] top5_results = {class_names[i]: float(predictions[0][i]) for i in top5_indices} return top5_results # ==================== واجهة Gradio ==================== iface = gr.Interface( fn=predict, inputs=gr.Image(type="numpy", label="ارفع صورة الورقة"), outputs=gr.Label(num_top_classes=5, label="النتيجة"), title="🌿 Plant Disease Recognition", description="ارفع صورة ورقة نبات وسيقوم النظام بتحديد المرض", examples=[], # ممكن تضيف أمثلة هنا allow_flagging="never" ) iface.launch()