Spaces:
Running
Running
Create skin.py
Browse files- MeAI_Maincode/skin.py +207 -0
MeAI_Maincode/skin.py
ADDED
@@ -0,0 +1,207 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import gradio as gr
|
3 |
+
from PIL import Image
|
4 |
+
import io
|
5 |
+
import numpy as np
|
6 |
+
import os
|
7 |
+
key = "a8108953dbmsh2baabfa3db731fdp14177bjsnfebbe465261d"
|
8 |
+
trans_disease = {
|
9 |
+
"acne": "mụn",
|
10 |
+
"actinic_keratosis": "chứng dày sừng quang hóa",
|
11 |
+
"alopecia_androgenetica": "chứng rụng tóc nội tiết tố androgen",
|
12 |
+
"alopecia_areata": "chứng rụng tóc từng vùng",
|
13 |
+
"bullous_dermatosis": "bệnh da bọng nước",
|
14 |
+
"chloasma": "nám da",
|
15 |
+
"corn": "chứng chai da",
|
16 |
+
"dermatofibroma": "u xơ da",
|
17 |
+
"eczema_dermatitis": "viêm da chàm",
|
18 |
+
"erysipelas": "viêm quầng",
|
19 |
+
"erythema_multiforme": "ban đỏ đa dạng",
|
20 |
+
"folliculitis": "viêm nang lông",
|
21 |
+
"furuncle": "mụn nhọt",
|
22 |
+
"haemangioma": "bệnh u máu",
|
23 |
+
"herpes": "mụn rộp",
|
24 |
+
"herpes_simplex": "nhiễm trùng do virus Herpes Simplex",
|
25 |
+
"iga_vasculitis": "viêm mạch máu Iga",
|
26 |
+
"keloid": "sẹo lồi",
|
27 |
+
"keratosis_follicularism": "bệnh nang lông dày sừng",
|
28 |
+
"lichen_planus": "bệnh lichen phẳng",
|
29 |
+
"lupus_erythematosus": "bệnh ban đỏ",
|
30 |
+
"molluscum_contagiosum": "u mềm lây",
|
31 |
+
"nevus": "nốt ruồi",
|
32 |
+
"paronychia": "viêm quanh móng",
|
33 |
+
"pityriasis_alba": "bệnh vẩy phấn trắng",
|
34 |
+
"pityriasis_rosea": "bệnh vảy phấn hồng",
|
35 |
+
"prurigo_nodularis": "bệnh sẩn ngứa",
|
36 |
+
"psoriasis": "bệnh vẩy nến",
|
37 |
+
"rosacea": "bệnh trứng cá đỏ rosacea",
|
38 |
+
"sebaceous_cyst": "u nang bã nhờn",
|
39 |
+
"sebaceousnevus": "bớt tuyến bã",
|
40 |
+
"seborrheic_dermatitis": "viêm da tiết bã",
|
41 |
+
"seborrheic_keratosis": "chứng dày sừng tiết bã",
|
42 |
+
"skin_tag": "mụn thịt dư",
|
43 |
+
"stasis_dermatitis": "viêm da ứ đọng",
|
44 |
+
"syringoma": "u ống tuyến mồ hôi",
|
45 |
+
"tinea_capitis": "nấm da đầu",
|
46 |
+
"tinea_corporis": "nấm cơ thể",
|
47 |
+
"tinea_cruris": "nấm bẹn",
|
48 |
+
"tinea_manuum": "",
|
49 |
+
"tinea_pedis": "nấm chân",
|
50 |
+
"tinea_unguium": "nấm móng tay móng chân",
|
51 |
+
"tinea_versicolor": "bệnh lang ben",
|
52 |
+
"urticaria": "phát ban",
|
53 |
+
"urticaria_papular": "nổi mề đay",
|
54 |
+
"varicella": "thủy đậu",
|
55 |
+
"verruca_plana": "mụn cóc phẳng",
|
56 |
+
"verruca_vulgaris": "mụn cóc thông thường",
|
57 |
+
"vitiligo": "bệnh bạch biến"
|
58 |
+
}
|
59 |
+
trans_body = {
|
60 |
+
"head": "đầu",
|
61 |
+
"neck": "cổ",
|
62 |
+
"hand": "tay",
|
63 |
+
"arm": "cánh tay",
|
64 |
+
"leg": "chân",
|
65 |
+
"foot": "bàn chân",
|
66 |
+
"back": "lưng",
|
67 |
+
"chest": "ngực",
|
68 |
+
"abdomen": "bụng",
|
69 |
+
"face": "mặt",
|
70 |
+
"ear": "tai",
|
71 |
+
"eye": "mắt",
|
72 |
+
"nose": "mũi",
|
73 |
+
"mouth": "miệng",
|
74 |
+
"lip": "môi",
|
75 |
+
"cheek": "má",
|
76 |
+
"tongue": "lưỡi",
|
77 |
+
"throat": "cổ họng",
|
78 |
+
"forehead": "trán",
|
79 |
+
"chin": "cằm",
|
80 |
+
"unknown" : "bộ phận chưa rõ"
|
81 |
+
}
|
82 |
+
trans_description = {
|
83 |
+
"acne": "Tình trạng da phổ biến với mụn trứng cá",
|
84 |
+
"pityriasis_rosea": "Phát ban thường bắt đầu bằng một đốm lớn",
|
85 |
+
"actinic_keratosis": "Các mảng da thô ráp, có vảy do tổn thương từ ánh nắng",
|
86 |
+
"prurigo_nodularis": "Các cục cứng và ngứa trên da",
|
87 |
+
"alopecia_androgenetica": "Rụng tóc theo kiểu mẫu",
|
88 |
+
"alopecia_areata": "Rụng tóc từng mảng",
|
89 |
+
"bullous_dermatosis": "Tình trạng da bị phồng rộp",
|
90 |
+
"chloasma": "Các mảng nâu trên da",
|
91 |
+
"corn": "Vùng da bị dày lên",
|
92 |
+
"dermatofibroma": "Sự phát triển da lành tính phổ biến",
|
93 |
+
"eczema_dermatitis": "Da bị viêm và ngứa",
|
94 |
+
"erysipelas": "Nhiễm trùng da do vi khuẩn",
|
95 |
+
"erythema_multiforme": "Phản ứng trên da",
|
96 |
+
"folliculitis": "Viêm nang lông",
|
97 |
+
"furuncle": "Nhiễm trùng sâu trong nang lông",
|
98 |
+
"haemangioma": "Sự phát triển lành tính của mạch máu",
|
99 |
+
"herpes": "Bệnh zona thần kinh",
|
100 |
+
"herpes_simplex": "Mụn rộp môi",
|
101 |
+
"iga_vasculitis": "Viêm mạch máu",
|
102 |
+
"keloid": "Mô sẹo phát triển quá mức",
|
103 |
+
"keratosis_follicularism": "Các mảng thô ráp và mụn nhỏ",
|
104 |
+
"lichen_planus": "Tình trạng viêm",
|
105 |
+
"lupus_erythematosus": "Bệnh tự miễn",
|
106 |
+
"molluscum_contagiosum": "Nhiễm trùng da do virus",
|
107 |
+
"nevus": "Nốt ruồi",
|
108 |
+
"paronychia": "Nhiễm trùng móng tay/móng chân",
|
109 |
+
"pityriasis_alba": "Vùng da sáng màu",
|
110 |
+
"psoriasis": "Tình trạng da mãn tính",
|
111 |
+
"rosacea": "Mặt đỏ và có mụn nhỏ",
|
112 |
+
"sebaceous_cyst": "U nang không phải ung thư",
|
113 |
+
"sebaceousnevus": "Dấu vết bẩm sinh",
|
114 |
+
"seborrheic_dermatitis": "Da đỏ và có vảy",
|
115 |
+
"seborrheic_keratosis": "Sự phát triển màu nâu và có vảy",
|
116 |
+
"skin_tag": "Sự phát triển nhỏ, mềm trên da",
|
117 |
+
"stasis_dermatitis": "Viêm da",
|
118 |
+
"syringoma": "Các nốt nhỏ trên da",
|
119 |
+
"tinea_capitis": "Nấm da đầu",
|
120 |
+
"tinea_corporis": "Nấm trên cơ thể",
|
121 |
+
"tinea_cruris": "Nấm bẹn",
|
122 |
+
"tinea_manuum": "Nấm tay",
|
123 |
+
"tinea_pedis": "Nấm chân",
|
124 |
+
"tinea_unguium": "Nấm móng tay/móng chân",
|
125 |
+
"tinea_versicolor": "Nhiễm trùng do nấm",
|
126 |
+
"urticaria": "Phát ban mề đay",
|
127 |
+
"urticaria_papular": "Nốt ngứa",
|
128 |
+
"varicella": "Bệnh thủy đậu",
|
129 |
+
"verruca_plana": "Mụn cóc phẳng",
|
130 |
+
"verruca_vulgaris": "Mụn cóc thông thường",
|
131 |
+
"vitiligo": "Mất màu da"
|
132 |
+
}
|
133 |
+
def detect_skin_disease_new(image, key):
|
134 |
+
try:
|
135 |
+
|
136 |
+
img = Image.fromarray(image.astype('uint8')) # Tạo ảnh từ numpy array
|
137 |
+
img_byte_array = io.BytesIO() # Tạo buffer giống tệp
|
138 |
+
img.save(img_byte_array, format='PNG') # Lưu ảnh dưới dạng PNG
|
139 |
+
img_byte_array.seek(0) # Quay về đầu buffer
|
140 |
+
|
141 |
+
url = "https://detect-skin-disease1.p.rapidapi.com/skin-disease"
|
142 |
+
files = {"image": ("image.png", img_byte_array, "image/png")}
|
143 |
+
headers = {
|
144 |
+
"X-RapidAPI-Key": key,
|
145 |
+
"X-RapidAPI-Host": "detect-skin-disease1.p.rapidapi.com"
|
146 |
+
}
|
147 |
+
response = requests.post(url, files=files, headers=headers)
|
148 |
+
|
149 |
+
if response.status_code != 200:
|
150 |
+
return f"Lỗi API: HTTP {response.status_code} - {response.reason}"
|
151 |
+
response_json = response.json()
|
152 |
+
|
153 |
+
output = ""
|
154 |
+
|
155 |
+
# Get image type
|
156 |
+
image_type = response_json.get('imageType')
|
157 |
+
if image_type=="normal_skin":
|
158 |
+
return "Da của bạn bình thường\n"
|
159 |
+
if image_type=="non_skin":
|
160 |
+
return "Ảnh của bạn không phải da\n"
|
161 |
+
|
162 |
+
# Get body part
|
163 |
+
body_part = response_json.get('bodyPart')
|
164 |
+
if body_part is not None:
|
165 |
+
vnese_body = trans_body.get(body_part, body_part)
|
166 |
+
output += f"Phần của cơ thể: {vnese_body}\n"
|
167 |
+
|
168 |
+
# Get results and probabilities
|
169 |
+
results = response_json.get('results')
|
170 |
+
if results is not None:
|
171 |
+
output += "Kết quả phân tích:\n"
|
172 |
+
|
173 |
+
# Sort results by probability in descending order
|
174 |
+
sorted_results = sorted(results.items(), key=lambda x: x[1], reverse=True)
|
175 |
+
|
176 |
+
for disease, probability in sorted_results:
|
177 |
+
probability_percent = probability * 100
|
178 |
+
vnese_disease = trans_disease.get(disease, disease)
|
179 |
+
description = trans_description.get(disease, "Không có mô tả")
|
180 |
+
|
181 |
+
if probability_percent >= 10:
|
182 |
+
output += f"- {vnese_disease} {probability_percent:.2f}%, mô tả thêm về bệnh: {description} \n"
|
183 |
+
|
184 |
+
return output
|
185 |
+
except Exception as e:
|
186 |
+
return f"Lỗi: {str(e)}"
|
187 |
+
|
188 |
+
|
189 |
+
|
190 |
+
|
191 |
+
|
192 |
+
def create_skin_tab_new(skinkey="a8108953dbmsh2baabfa3db731fdp14177bjsnfebbe465261d"):
|
193 |
+
css = """
|
194 |
+
.textboxskin {
|
195 |
+
font-sxxxxize: 50px; !important;
|
196 |
+
}
|
197 |
+
"""
|
198 |
+
with gr.Blocks(css=css) as demo:
|
199 |
+
keybox = gr.Text(value=skinkey,visible=False)
|
200 |
+
gr.Markdown("Hãy tải ảnh lên và nhấn **Xử Lý** để chẩn đoán bệnh ngoài da.")
|
201 |
+
with gr.Row():
|
202 |
+
inp = gr.Image(type="numpy",height=512, width=512,
|
203 |
+
value=os.path.join(os.path.dirname(__file__), "../anh/thuydau.jpg"))
|
204 |
+
out = gr.Label(label="Kết Quả Dự Đoán",elem_classes="textboxskin")
|
205 |
+
btn = gr.Button("Xử Lý")
|
206 |
+
btn.click(fn=detect_skin_disease, inputs=[inp,keybox], outputs=out)
|
207 |
+
return demo
|