Noeies commited on
Commit
a2201c6
·
verified ·
1 Parent(s): 12309ed

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -0
app.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ แอปจำแนกอวัยวะ / ส่วนของผิวหนัง
4
+ โดยใช้โมเดลที่เทรนมาแล้วจาก Hugging Face
5
+ """
6
+
7
+ from transformers import AutoImageProcessor, AutoModelForImageClassification
8
+ import torch
9
+ from PIL import Image
10
+ import requests
11
+
12
+ # -----------------------------
13
+ # กำหนดชื่อโมเดล
14
+ # -----------------------------
15
+ model_name = "histai/SPIDER-skin-model"
16
+
17
+ # -----------------------------
18
+ # โหลดโมเดลและตัวประมวลผล
19
+ # -----------------------------
20
+ print("กำลังโหลดโมเดลและตัวประมวลผล...")
21
+ processor = AutoImageProcessor.from_pretrained(model_name)
22
+ model = AutoModelForImageClassification.from_pretrained(model_name)
23
+ print("โหลดโมเดลเรียบร้อย!")
24
+
25
+ # -----------------------------
26
+ # คำอธิบายของอวัยวะ / สภาพผิวหนัง (ตัวอย่าง)
27
+ # -----------------------------
28
+ organ_descriptions = {
29
+ 0: "ผิวหนังปกติ",
30
+ 1: "แผลผิวหนัง",
31
+ 2: "ตุ่มน้ำ / ฟองอากาศ",
32
+ 3: "จุดเลือดออกใต้ผิวหนัง",
33
+ 4: "รอยไหม้",
34
+ 5: "การอักเสบ",
35
+ # สามารถเพิ่มอวัยวะ/สภาพผิวเพิ่มเติมได้
36
+ }
37
+
38
+ # -----------------------------
39
+ # โหลดภาพ (ใส่ URL หรือ path ของไฟล์)
40
+ # -----------------------------
41
+ # ตัวอย่าง: โหลดจาก URL
42
+ url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers_logo.png"
43
+ image = Image.open(requests.get(url, stream=True).raw)
44
+
45
+ # หากต้องการใช้ไฟล์ในเครื่อง uncomment ด้านล่าง
46
+ # image_path = "path/to/your/image.jpg"
47
+ # image = Image.open(image_path)
48
+
49
+ # -----------------------------
50
+ # เตรียมข้อมูลสำหรับโมเดล
51
+ # -----------------------------
52
+ inputs = processor(images=image, return_tensors="pt")
53
+
54
+ # -----------------------------
55
+ # ทำนายผล
56
+ # -----------------------------
57
+ with torch.no_grad():
58
+ logits = model(**inputs).logits
59
+ predicted_class_idx = logits.argmax(-1).item()
60
+
61
+ # -----------------------------
62
+ # แสดงผลลัพธ์
63
+ # -----------------------------
64
+ description = organ_descriptions.get(predicted_class_idx, "ไม่ทราบอวัยวะ / สภาพผิว")
65
+ print("------ ผลลัพธ์ ------")
66
+ print(f"รหัสคลาสที่ทำนาย: {predicted_class_idx}")
67
+ print(f"คำอธิบาย: {description}")