File size: 8,119 Bytes
34f0d94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fecc2d4
34f0d94
cb86281
fecc2d4
cb86281
 
 
34f0d94
fecc2d4
 
34f0d94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fecc2d4
0ef55ef
34f0d94
 
 
 
 
 
 
 
 
9bb6270
34f0d94
 
fecc2d4
34f0d94
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import requests
import gradio as gr
from PIL import Image
import io
import numpy as np
import os
trans_disease = {
    "acne": "mụn",
    "actinic_keratosis": "chứng dày sừng quang hóa",
    "alopecia_androgenetica": "chứng rụng tóc nội tiết tố androgen",
    "alopecia_areata": "chứng rụng tóc từng vùng",
    "bullous_dermatosis": "bệnh da bọng nước",
    "chloasma": "nám da",
    "corn": "chứng chai da",
    "dermatofibroma": "u xơ da",
    "eczema_dermatitis": "viêm da chàm",
    "erysipelas": "viêm quầng",
    "erythema_multiforme": "ban đỏ đa dạng",
    "folliculitis": "viêm nang lông",
    "furuncle": "mụn nhọt",
    "haemangioma": "bệnh u máu",
    "herpes": "mụn rộp",
    "herpes_simplex": "nhiễm trùng do virus Herpes Simplex",
    "iga_vasculitis": "viêm mạch máu Iga",
    "keloid": "sẹo lồi",
    "keratosis_follicularism": "bệnh nang lông dày sừng",
    "lichen_planus": "bệnh lichen phẳng",
    "lupus_erythematosus": "bệnh ban đỏ",
    "molluscum_contagiosum": "u mềm lây",
    "nevus": "nốt ruồi",
    "paronychia": "viêm quanh móng",
    "pityriasis_alba": "bệnh vẩy phấn trắng",
    "pityriasis_rosea": "bệnh vảy phấn hồng",
    "prurigo_nodularis": "bệnh sẩn ngứa",
    "psoriasis": "bệnh vẩy nến",
    "rosacea": "bệnh trứng cá đỏ rosacea",
    "sebaceous_cyst": "u nang bã nhờn",
    "sebaceousnevus": "bớt tuyến bã",
    "seborrheic_dermatitis": "viêm da tiết bã",
    "seborrheic_keratosis": "chứng dày sừng tiết bã",
    "skin_tag": "mụn thịt dư",
    "stasis_dermatitis": "viêm da ứ đọng",
    "syringoma": "u ống tuyến mồ hôi",
    "tinea_capitis": "nấm da đầu",
    "tinea_corporis": "nấm cơ thể",
    "tinea_cruris": "nấm bẹn",
    "tinea_manuum": "",
    "tinea_pedis": "nấm chân",
    "tinea_unguium": "nấm móng tay móng chân",
    "tinea_versicolor": "bệnh lang ben",
    "urticaria": "phát ban",
    "urticaria_papular": "nổi mề đay",
    "varicella": "thủy đậu",
    "verruca_plana": "mụn cóc phẳng",
    "verruca_vulgaris": "mụn cóc thông thường",
    "vitiligo": "bệnh bạch biến"
}
trans_body = {
    "head": "đầu",
    "neck": "cổ",
    "hand": "tay",
    "arm": "cánh tay",
    "leg": "chân",
    "foot": "bàn chân",
    "back": "lưng",
    "chest": "ngực",
    "abdomen": "bụng",
    "face": "mặt",
    "ear": "tai",
    "eye": "mắt",
    "nose": "mũi",
    "mouth": "miệng",
    "lip": "môi",
    "cheek": "má",
    "tongue": "lưỡi",
    "throat": "cổ họng",
    "forehead": "trán",
    "chin": "cằm",
    "unknown" : "bộ phận chưa rõ"
}
trans_description = {
    "acne": "Tình trạng da phổ biến với mụn trứng cá",
    "pityriasis_rosea": "Phát ban thường bắt đầu bằng một đốm lớn",
    "actinic_keratosis": "Các mảng da thô ráp, có vảy do tổn thương từ ánh nắng",
    "prurigo_nodularis": "Các cục cứng và ngứa trên da",
    "alopecia_androgenetica": "Rụng tóc theo kiểu mẫu",
    "alopecia_areata": "Rụng tóc từng mảng",
    "bullous_dermatosis": "Tình trạng da bị phồng rộp",
    "chloasma": "Các mảng nâu trên da",
    "corn": "Vùng da bị dày lên",
    "dermatofibroma": "Sự phát triển da lành tính phổ biến",
    "eczema_dermatitis": "Da bị viêm và ngứa",
    "erysipelas": "Nhiễm trùng da do vi khuẩn",
    "erythema_multiforme": "Phản ứng trên da",
    "folliculitis": "Viêm nang lông",
    "furuncle": "Nhiễm trùng sâu trong nang lông",
    "haemangioma": "Sự phát triển lành tính của mạch máu",
    "herpes": "Bệnh zona thần kinh",
    "herpes_simplex": "Mụn rộp môi",
    "iga_vasculitis": "Viêm mạch máu",
    "keloid": "Mô sẹo phát triển quá mức",
    "keratosis_follicularism": "Các mảng thô ráp và mụn nhỏ",
    "lichen_planus": "Tình trạng viêm",
    "lupus_erythematosus": "Bệnh tự miễn",
    "molluscum_contagiosum": "Nhiễm trùng da do virus",
    "nevus": "Nốt ruồi",
    "paronychia": "Nhiễm trùng móng tay/móng chân",
    "pityriasis_alba": "Vùng da sáng màu",
    "psoriasis": "Tình trạng da mãn tính",
    "rosacea": "Mặt đỏ và có mụn nhỏ",
    "sebaceous_cyst": "U nang không phải ung thư",
    "sebaceousnevus": "Dấu vết bẩm sinh",
    "seborrheic_dermatitis": "Da đỏ và có vảy",
    "seborrheic_keratosis": "Sự phát triển màu nâu và có vảy",
    "skin_tag": "Sự phát triển nhỏ, mềm trên da",
    "stasis_dermatitis": "Viêm da",
    "syringoma": "Các nốt nhỏ trên da",
    "tinea_capitis": "Nấm da đầu",
    "tinea_corporis": "Nấm trên cơ thể",
    "tinea_cruris": "Nấm bẹn",
    "tinea_manuum": "Nấm tay",
    "tinea_pedis": "Nấm chân",
    "tinea_unguium": "Nấm móng tay/móng chân",
    "tinea_versicolor": "Nhiễm trùng do nấm",
    "urticaria": "Phát ban mề đay",
    "urticaria_papular": "Nốt ngứa",
    "varicella": "Bệnh thủy đậu",
    "verruca_plana": "Mụn cóc phẳng",
    "verruca_vulgaris": "Mụn cóc thông thường",
    "vitiligo": "Mất màu da"
}
def detect_skin_disease(image, key):
    try:
        img = Image.fromarray((image).astype('uint8'))
        # image = Image.open(image)
        img_byte_array = io.BytesIO()
        img.save(img_byte_array, format='PNG')
        img_byte_array.seek(0)



        url = "https://detect-skin-disease1.p.rapidapi.com/skin-disease"
        files = {"image": ("image.png", img_byte_array, "image/png")}
        headers = {
            "X-RapidAPI-Key": key,
            "X-RapidAPI-Host": "detect-skin-disease1.p.rapidapi.com"
        }
        response = requests.post(url, files=files, headers=headers)

        if response.status_code != 200:
            return f"Lỗi API: HTTP {response.status_code} - {response.reason}"
        response_json = response.json()

        output = ""

        # Get image type
        image_type = response_json.get('imageType')
        if image_type=="normal_skin":
            return "Da của bạn bình thường\n"
        if image_type=="non_skin":
            return "Ảnh của bạn không phải da\n"

        # Get body part
        body_part = response_json.get('bodyPart')
        if body_part is not None:
            vnese_body = trans_body.get(body_part, body_part)
            output += f"Phần của cơ thể: {vnese_body}\n"

        # Get results and probabilities
        results = response_json.get('results')
        if results is not None:
            output += "Kết quả phân tích:\n"

            # Sort results by probability in descending order
            sorted_results = sorted(results.items(), key=lambda x: x[1], reverse=True)

            for disease, probability in sorted_results:
                probability_percent = probability * 100
                vnese_disease = trans_disease.get(disease, disease)
                description = trans_description.get(disease, "Không có mô tả")
                
                if probability_percent >= 10:
                    output += f"- {vnese_disease} {probability_percent:.2f}%, mô tả thêm về bệnh: {description} \n"

        return output
    except Exception as e:
        return f"Lỗi: {str(e)}"






def create_skin_tab_new(skinkey="bc004194c0mshbc777f53d4f88bbp11c4f9jsn68418134659f"):
    css = """
    .textboxskin {
        font-sxxxxize: 50px; !important;
    }
    """
    with gr.Blocks(css=css) as demo:
        keybox = gr.Text(value=skinkey,visible=False)
        gr.Markdown("Hãy tải ảnh lên và nhấn **Xử Lý** để chẩn đoán bệnh ngoài da.")
        with gr.Row():
          inp = gr.Image(type="numpy",height=512, width=512)
          out = gr.Label(label="Kết Quả Dự Đoán",elem_classes="textboxskin")
        btn = gr.Button("Xử Lý")
        btn.click(fn=detect_skin_disease, inputs=[inp,keybox], outputs=out)
    return demo