Noeies commited on
Commit
6c85081
·
verified ·
1 Parent(s): 715a639

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -0
app.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoImageProcessor, AutoModelForImageClassification
3
+ from PIL import Image
4
+ import torch
5
+
6
+ # โมเดล ViT ตัวอย่าง
7
+ model_name = "google/vit-base-patch16-224"
8
+ processor = AutoImageProcessor.from_pretrained(model_name)
9
+ model = AutoModelForImageClassification.from_pretrained(model_name)
10
+
11
+ # ชื่ออวัยวะ + คำอธิบาย
12
+ organ_labels = ["สมอง", "หัวใจ", "ปอด", "ตับ", "ไต", "กระเพาะอาหาร", "ลำไส้", "ตา", "มือ", "เท้า"]
13
+ organ_descriptions = {
14
+ "สมอง": "ศูนย์ควบคุมร่างกายและความคิด",
15
+ "หัวใจ": "สูบฉีดเลือดไปทั่วร่างกาย",
16
+ "ปอด": "แลกเปลี่ยนออกซิเจนและคาร์บอนไดออกไซด์",
17
+ "ตับ": "กรองสารพิษและผลิตน้ำดี",
18
+ "ไต": "กรองของเสียและสร้างปัสสาวะ",
19
+ "กระเพาะอาหาร": "ย่อยอาหารโดยใช้กรดและเอนไซม์",
20
+ "ลำไส้": "ดูดซึมสารอาหารเข้าสู่ร่างกาย",
21
+ "ตา": "อวัยวะรับภาพและแสง",
22
+ "มือ": "ใช้จับและสัมผัสวัตถุ",
23
+ "เท้า": "รองรับน้ำหนักและเคลื่อนที่"
24
+ }
25
+
26
+ def classify_organ(image):
27
+ image = Image.fromarray(image)
28
+ inputs = processor(images=image, return_tensors="pt")
29
+ with torch.no_grad():
30
+ outputs = model(**inputs)
31
+ probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
32
+ top_probs, top_indices = torch.topk(probs, 3)
33
+
34
+ results = {}
35
+ for j, i in enumerate(top_indices[0]):
36
+ organ_name = organ_labels[i.item() % len(organ_labels)]
37
+ confidence = float(top_probs[0][j])
38
+ description = organ_descriptions.get(organ_name, "ไม่มีคำอธิบาย")
39
+ results[f"{organ_name} ({confidence:.2f})"] = description
40
+
41
+ return results
42
+
43
+ # Gradio interface
44
+ app = gr.Interface(
45
+ fn=classify_organ,
46
+ inputs=gr.Image(type="numpy", label="📸 อัปโหลดภาพอวัยวะ"),
47
+ outputs=gr.Label(num_top_classes=3, label="ผลลัพธ์จาก AI"),
48
+ title="🧠 AI จำแนกอวัยวะในร่างกายพร้อมคำอธิบาย",
49
+ description="อัปโหลดภาพอวัยวะ แล้ว AI จะบอกว่าเป็นส่วนไหน พร้อมคำอธิบายสั้น ๆ"
50
+ )
51
+
52
+ if __name__ == "__main__":
53
+ app.launch()