Spaces:
Build error
Build error
Upload app.py
Browse files
app.py
CHANGED
@@ -1,43 +1,65 @@
|
|
1 |
-
import gradio as gr
|
2 |
from PIL import Image
|
3 |
-
import
|
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 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from PIL import Image
|
2 |
+
from transformers import pipeline
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
# 🌐 Load pre-trained image classification model
|
6 |
+
classifier = pipeline("image-classification", model="microsoft/resnet-50")
|
7 |
+
|
8 |
+
# 🔍 Define bilingual label mapping
|
9 |
+
label_map = {
|
10 |
+
"agaric": ("Edible", "กินได้"),
|
11 |
+
"bolete": ("Edible", "กินได้"),
|
12 |
+
"gyromitra": ("Poisonous", "พิษ"),
|
13 |
+
"amanita": ("Poisonous", "พิษ"),
|
14 |
+
"earthstar": ("Edible", "กินได้"),
|
15 |
+
"hen-of-the-woods": ("Edible", "กินได้"),
|
16 |
+
"mushroom": ("Unknown", "ไม่ทราบ"),
|
17 |
+
"coral fungus": ("Poisonous", "พิษ"),
|
18 |
+
# Add more if needed
|
19 |
+
}
|
20 |
+
|
21 |
+
# 🧠 Classification function
|
22 |
+
def classify_mushroom(image: Image.Image):
|
23 |
+
print("✅ classify_mushroom: NEW VERSION")
|
24 |
+
|
25 |
+
try:
|
26 |
+
image = image.convert("RGB")
|
27 |
+
result = classifier(image)
|
28 |
+
print("🔍 Raw result from model:", result)
|
29 |
+
|
30 |
+
result = result[0]
|
31 |
+
label = result['label'].lower()
|
32 |
+
score = round(result['score'] * 100, 2)
|
33 |
+
|
34 |
+
prediction_en, prediction_th = label_map.get(label, ("Unknown", "ไม่ทราบ"))
|
35 |
+
print("✅ RETURNING:", prediction_en, prediction_th, f"{score:.2f}%", label)
|
36 |
+
|
37 |
+
return prediction_en, prediction_th, f"{score:.2f}%", label
|
38 |
+
|
39 |
+
except Exception as e:
|
40 |
+
print(f"❌ Error: {e}")
|
41 |
+
return "Error", "ผิดพลาด", "N/A", "N/A"
|
42 |
+
|
43 |
+
# 🎨 Gradio UI
|
44 |
+
if __name__ == "__main__":
|
45 |
+
with gr.Blocks() as demo:
|
46 |
+
gr.Markdown("## 🍄 Mushroom Safety Classifier")
|
47 |
+
gr.Markdown("Upload a mushroom photo to check if it’s edible or poisonous.\nอัปโหลดรูปเห็ดเพื่อทำนายว่าเห็ดกินได้หรือมีพิษ")
|
48 |
+
|
49 |
+
with gr.Row():
|
50 |
+
image_input = gr.Image(type="pil", label="📷 Upload Mushroom Image")
|
51 |
+
with gr.Column():
|
52 |
+
label_en = gr.Textbox(label="🧠 Prediction (English)")
|
53 |
+
label_th = gr.Textbox(label="🗣️ คำทำนาย (ภาษาไทย)")
|
54 |
+
confidence = gr.Textbox(label="📶 Confidence Score")
|
55 |
+
label_raw = gr.Textbox(label="🏷️ Predicted Mushroom Name")
|
56 |
+
|
57 |
+
classify_btn = gr.Button("🔍 Classify")
|
58 |
+
|
59 |
+
classify_btn.click(
|
60 |
+
fn=classify_mushroom,
|
61 |
+
inputs=image_input,
|
62 |
+
outputs=[label_en, label_th, confidence, label_raw]
|
63 |
+
)
|
64 |
+
|
65 |
+
demo.launch()
|