File size: 5,402 Bytes
be0b780
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e362a29
be0b780
 
 
 
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
import requests
import gradio as gr
from PIL import Image
import io
import numpy as np
import os
key = "2cff2aab49msh5191ef59693cc02p1091a7jsnd7100bb29621"
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õ"
}
def detect_skin_disease(image,key):
    try:

        # Convert NumPy array to image file-like object
        img = Image.fromarray((image).astype('uint8'))
        img_byte_array = io.BytesIO()
        img.save(img_byte_array, format='PNG')
        img_byte_array.seek(0)

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

        if 'data' in response_json:
            body_part = response_json['data'].get('body_part')
            results = response_json['data'].get('results_english')

            if body_part is not None:
                vnese_body = trans_body[body_part]
                output += f"Phần của cơ thể: {vnese_body} ({body_part})\n"
                
            if results is not None:
              output += " Kết quả phân tích:      "

              # Sort the results by probability percentage 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)
                if probability_percent >= 10:
                  output += f"{vnese_disease} : {probability_percent:.2f}%\n"

            return output
        else:
            return "Không có dữ liệu phản hồi từ API."
    except Exception as e:
        return f"Error: {str(e)}"

def create_skin_tab(skinkey="2cff2aab49msh5191ef59693cc02p1091a7jsnd7100bb29621"):
    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,
          value=os.path.join(os.path.dirname(__file__), "../anh/thuydau.jpg"))
          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