trapezius60 commited on
Commit
567b247
·
verified ·
1 Parent(s): c1d8061

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -67
app.py CHANGED
@@ -1,67 +1,68 @@
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()
66
-
67
-
 
 
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
+ "Amanita muscaria":("Poisonous","พิษ")
19
+ # Add more if needed
20
+ }
21
+
22
+ # 🧠 Classification function
23
+ def classify_mushroom(image: Image.Image):
24
+ print("✅ classify_mushroom: NEW VERSION")
25
+
26
+ try:
27
+ image = image.convert("RGB")
28
+ result = classifier(image)
29
+ print("🔍 Raw result from model:", result)
30
+
31
+ result = result[0]
32
+ label = result['label'].lower()
33
+ score = round(result['score'] * 100, 2)
34
+
35
+ prediction_en, prediction_th = label_map.get(label, ("Unknown", "ไม่ทราบ"))
36
+ print("✅ RETURNING:", prediction_en, prediction_th, f"{score:.2f}%", label)
37
+
38
+ return prediction_en, prediction_th, f"{score:.2f}%", label
39
+
40
+ except Exception as e:
41
+ print(f"❌ Error: {e}")
42
+ return "Error", "ผิดพลาด", "N/A", "N/A"
43
+
44
+ # 🎨 Gradio UI
45
+ if __name__ == "__main__":
46
+ with gr.Blocks() as demo:
47
+ gr.Markdown("## 🍄 Mushroom Safety Classifier")
48
+ gr.Markdown("Upload a mushroom photo to check if it’s edible or poisonous.\nอัปโหลดรูปเห็ดเพื่อทำนายว่าเห็ดกินได้หรือมีพิษ")
49
+
50
+ with gr.Row():
51
+ image_input = gr.Image(type="pil", label="📷 Upload Mushroom Image")
52
+ with gr.Column():
53
+ label_en = gr.Textbox(label="🧠 Prediction (English)")
54
+ label_th = gr.Textbox(label="🗣️ คำทำนาย (ภาษาไทย)")
55
+ confidence = gr.Textbox(label="📶 Confidence Score")
56
+ label_raw = gr.Textbox(label="🏷️ Predicted Mushroom Name")
57
+
58
+ classify_btn = gr.Button("🔍 Classify")
59
+
60
+ classify_btn.click(
61
+ fn=classify_mushroom,
62
+ inputs=image_input,
63
+ outputs=[label_en, label_th, confidence, label_raw]
64
+ )
65
+
66
+ demo.launch()
67
+
68
+