Noeies commited on
Commit
abf7b91
·
verified ·
1 Parent(s): 13956ef

Create app.py

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