lynz1910 commited on
Commit
624fb6c
Β·
verified Β·
1 Parent(s): ab46dfd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +166 -32
app.py CHANGED
@@ -1,34 +1,168 @@
 
 
1
  import gradio as gr
 
 
 
 
2
 
3
- # Fungsi untuk tab "Home"
4
- def home_content():
5
- return "Selamat datang di halaman Home!"
6
-
7
- # Fungsi untuk tab "Coba sekarang"
8
- def coba_sekarang_content():
9
- return "Ini adalah halaman 'Coba sekarang'!"
10
-
11
- # Membuat antarmuka dengan tab
12
- with gr.Blocks() as demo:
13
- with gr.Tab("Home"):
14
- with gr.Column():
15
- gr.Markdown("### Home Page")
16
- gr.Textbox(home_content, label="Home", elem_id="home_box")
17
-
18
- with gr.Tab("Coba sekarang"):
19
- with gr.Column():
20
- gr.Markdown("### Coba sekarang")
21
- gr.Textbox(coba_sekarang_content, label="Coba sekarang", elem_id="coba_sekarang_box")
22
-
23
- # Menggunakan CSS untuk mengganti warna latar belakang tiap tab
24
- demo.css = """
25
- #home_box {
26
- background-color: lightblue;
27
- }
28
- #coba_sekarang_box {
29
- background-color: lightgreen;
30
- }
31
- """
32
-
33
- # Menjalankan aplikasi Gradio
34
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import tensorflow as tf
2
+ from tensorflow.keras.models import load_model
3
  import gradio as gr
4
+ import cv2
5
+ import numpy as np
6
+ from tensorflow.keras.applications.vgg19 import preprocess_input
7
+ import gdown
8
 
9
+ # Download model dari Google Drive
10
+ file_id = '1YUV4xVBwXfUMwBiCfZ53G32vsZ2XJtp6'
11
+ gdown.download(f"https://drive.google.com/uc?export=download&id={file_id}", "AiCNE_Model_Akhir.h5", quiet=False)
12
+
13
+ # Load model
14
+ model = load_model("AiCNE_Model_Akhir.h5")
15
+
16
+ # Labels dan saran pengobatan untuk deteksi acne
17
+ acne_labels = {
18
+ 0: 'Acne',
19
+ 1: 'Clear',
20
+ 2: 'Comedo'
21
+ }
22
+
23
+ acne_treatment = {
24
+ 0: '- Topical anti-acne agents, such as benzoyl peroxide, azelaic acid, and tretinoin or adapalene gel and some antibiotics (clindamycin)\n- New bioactive proteins may also prove successful\n- Newer topical agents such as clascoterone\n- Low-dose combined oral contraceptive\n- Antiseptic or keratolytic washes containing salicylic acid\n- Light/laser therapy',
25
+ 1: 'Keep up the good work! ',
26
+ 2: '- Benzoyl peroxide\n- Azelaic acid\n- Salicylic acid +/- sulfur and resorcinol\n- Glycolic acid\n- Retinoids such as tretinoin, isotretinoin, adapalene (these require a doctors prescription)'
27
+ }
28
+
29
+ # Fungsi untuk mendeteksi acne
30
+ def detect_acne(image, threshold=0.4):
31
+ # Resize gambar menjadi 224x224 piksel agar sesuai dengan ukuran input model
32
+ image_resized = cv2.resize(image, (224, 224))
33
+
34
+ # Proses gambar dengan preprocess_input untuk menyesuaikan format input model
35
+ input_data = preprocess_input(np.expand_dims(image_resized, axis=0)) # Menambah dimensi untuk batch
36
+
37
+ # Mendapatkan prediksi dari model
38
+ predictions = model.predict(input_data)
39
+
40
+ # Menemukan indeks kelas dengan probabilitas tertinggi
41
+ max_index = np.argmax(predictions[0]) # Indeks dengan probabilitas tertinggi
42
+ max_prob = predictions[0][max_index] # Nilai probabilitas tertinggi
43
+
44
+ # Inisialisasi variabel untuk hasil deteksi dan saran pengobatan
45
+ detections = [] # Daftar untuk menyimpan label deteksi (misalnya 'Acne', 'Clear', 'Comedo')
46
+ treatment_suggestion = "" # Saran pengobatan yang sesuai
47
+
48
+ # Jika probabilitas tertinggi lebih besar dari threshold, deteksi berhasil
49
+ if max_prob > threshold:
50
+ detections.append(acne_labels[max_index]) # Menambahkan label deteksi ke dalam daftar
51
+ treatment_suggestion = acne_treatment[max_index] # Mendapatkan saran pengobatan berdasarkan deteksi
52
+
53
+ # Mengembalikan hasil: gambar yang sudah dianotasi, hasil deteksi, dan saran pengobatan
54
+ return f"Detected face: {detections}", treatment_suggestion
55
+
56
+
57
+ # CSS untuk mempercantik tampilan antarmuka
58
+ custom_css = """
59
+ @import url('https://fonts.googleapis.com/css2?family=Itim&display=swap');
60
+ @import url('https://fonts.googleapis.com/css2?family=Instrument+Sans:wght@400;700&display=swap');
61
+ body {
62
+ background: linear-gradient(to bottom, #FFA500, #1E90FF);
63
+ color: #ffffff !important;
64
+ margin: 0;
65
+ padding: 0;
66
+ overflow: hidden;
67
+ }
68
+
69
+ .gradio-container {
70
+ max-height: 90vh;
71
+ overflow-y: auto;
72
+ background: transparent !important;
73
+ color: #ffffff !important;
74
+ border-radius: 12px;
75
+ text-align: center;
76
+ box-sizing: border-box;
77
+ padding: 10px;
78
+ }
79
+
80
+ .gradio-container .wrap h1 {
81
+ font-size: 100px !important;
82
+ font-family: 'Itim', cursive !important;
83
+ font-weight: bold !important;
84
+ text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
85
+ color: #000000 !important;
86
+ }
87
+
88
+ h2 {
89
+ font-size: 40px;
90
+ font-family: 'Instrument Sans', sans-serif;
91
+ font-weight: bold;
92
+ color: #000000;
93
+ text-align: center;
94
+ }
95
+
96
+ p {
97
+ font-size: 30px;
98
+ font-family: 'Instrument Sans', sans-serif;
99
+ font-weight: bold;
100
+ color: #000000;
101
+ text-align: center;
102
+ }
103
+
104
+ .gradio-container .wrap {
105
+ font-size: 40px !important;
106
+ line-height: 1.6;
107
+ padding: 20px
108
+ }
109
+
110
+ .output-textbox textarea {
111
+ background-color: #1e1e1e !important;
112
+ color: #ffffff !important;
113
+ border: 2px solid #000000;
114
+ font-weight: bold;
115
+ font-size: 16px;
116
+ padding: 10px;
117
+ border-radius: 8px;
118
+ }
119
+
120
+ .gradio-container::-webkit-scrollbar {
121
+ width: 12px;
122
+ }
123
+
124
+ .gradio-container::-webkit-scrollbar-track {
125
+ background: rgba(255, 255, 255, 0.2);
126
+ border-radius: 6px;
127
+ }
128
+
129
+ .gradio-container::-webkit-scrollbar-thumb {
130
+ background: #D3D3D3;
131
+ border-radius: 6px;
132
+ border: #555555;
133
+ }
134
+
135
+ .gradio-container::-webkit-scrollbar-thumb:active {
136
+ background: #555555;
137
+ border: #555555;
138
+ }
139
+ """
140
+
141
+ interface = gr.Interface(
142
+ fn=detect_acne,
143
+ inputs=gr.Image(type="numpy", label="Upload an image"),
144
+ outputs=[
145
+ gr.Textbox(label="Detection Result"),
146
+ gr.Textbox(label="Treatment Suggestion")
147
+ ],
148
+ title="🌟 AiCNE 🌟",
149
+ description=(
150
+ "<div style='text-align: center;'>"
151
+ "<h2>Welcome to <b>AiCNE</b>, your AI-powered assistant for acne detection!</h2>"
152
+ "<h2>Upload a clear image of your face to analyze and classify acne types.</h2>"
153
+ "<h2><b>Get instant results</b> and take a step closer to understanding your skin!</h2>"
154
+ "<br>"
155
+ "<br>"
156
+ "<h2>What is AiCNE?</h2>"
157
+ "<p>AiCNE is an AI-Powered Acne Detection tool, a smart solution to understand your skin condition with AI Technology.</p>"
158
+ "<br>"
159
+ "<h2>Why use AiCNE?</h2>"
160
+ "<p>AiCNE detects acne within seconds with high accuracy, offering a user-friendly interface for your convenience.</p>"
161
+ "<br>"
162
+ "<br>"
163
+ "</div>"
164
+ ),
165
+ css=custom_css,
166
+ )
167
+
168
+ interface.launch()