File size: 1,307 Bytes
2256d58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

from PIL import Image
import numpy as np
import tensorflow as tf
import gradio as gr
import json

# تحميل أسماء الفئات
with open("class_names.json", "r", encoding="utf-8") as f:
    index_to_arabic = json.load(f)
    index_to_arabic = {int(k): v for k, v in index_to_arabic.items()}
arabic_names = [index_to_arabic[i] for i in sorted(index_to_arabic.keys())]

# تحميل النموذج
model = tf.keras.models.load_model("model.h5")

def predict_flower(img):
    if img is None:
        return {"⚠️ يرجى رفع صورة": 1.0}
    if img.mode != 'RGB':
        img = img.convert('RGB')
    img = img.resize((150, 150))
    img_array = np.array(img) / 255.0
    img_array = np.expand_dims(img_array, axis=0)
    preds = model.predict(img_array, verbose=0)[0]
    if np.max(preds) < 0.6:
        return {"❌ لا يمكن التعرف على الصورة": 1.0}
    return {arabic_names[i]: float(preds[i]) for i in range(len(arabic_names))}

iface = gr.Interface(
    fn=predict_flower,
    inputs=gr.Image(type="pil", label="ارفع صورة زهرة 🌸"),
    outputs=gr.Label(),
    title="🌼 مُصنّف الزهور (عربي)",
    description="يدعم: أقحوان، هندباء، توليب، عباد الشمس"
)

if __name__ == "__main__":
    iface.launch()