trapezius60 commited on
Commit
757d49e
·
verified ·
1 Parent(s): 119abb1

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -42
app.py CHANGED
@@ -1,43 +1,65 @@
1
- import gradio as gr
2
  from PIL import Image
3
- import random
4
-
5
- # 💡 Placeholder classifier — replace with actual model or API call
6
- def classify_mushroom(image):
7
- # Simulate prediction
8
- prediction = random.choice(["Edible", "Poisonous"])
9
- confidence = round(random.uniform(0.7, 0.99), 2)
10
-
11
- bilingual_map = {
12
- "Edible": "กินได้",
13
- "Poisonous": "พิษ"
14
- }
15
-
16
- return {
17
- "Prediction (EN)": prediction,
18
- "คำทำนาย (TH)": bilingual_map[prediction],
19
- "Confidence": f"{confidence * 100:.1f}%"
20
- }
21
-
22
- # 🧪 Gradio App UI
23
- with gr.Blocks() as demo:
24
- gr.Markdown("## 🍄 Mushroom Safety Classifier")
25
- gr.Markdown("Upload a photo of a mushroom to check if it's edible or poisonous.\nอัปโหลดรูปเห็ดเพื่อทำนายว่าเห็ดกินได้หรือมีพิษ")
26
-
27
- with gr.Row():
28
- image_input = gr.Image(type="pil", label="📷 Upload Mushroom Image")
29
- with gr.Column():
30
- label_en = gr.Textbox(label="🧠 Prediction (English)")
31
- label_th = gr.Textbox(label="🗣️ คำทำนาย (ภาษาไทย)")
32
- confidence = gr.Textbox(label="📶 Confidence Score")
33
-
34
- classify_btn = gr.Button("🔍 Classify")
35
-
36
- # 🔁 Connect button to function
37
- classify_btn.click(
38
- fn=classify_mushroom,
39
- inputs=image_input,
40
- outputs=[label_en, label_th, confidence]
41
- )
42
-
43
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()