Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,157 +1,182 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
from
|
| 4 |
-
import
|
| 5 |
-
import
|
| 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 |
-
gr.
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
)
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import ViTForImageClassification, ViTImageProcessor
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import numpy as np
|
| 6 |
+
|
| 7 |
+
# Senin model'inin sınıfları (config'den)
|
| 8 |
+
class_names = {
|
| 9 |
+
0: "Enfeksiyonel",
|
| 10 |
+
1: "Ekzama",
|
| 11 |
+
2: "Akne",
|
| 12 |
+
3: "Pigment",
|
| 13 |
+
4: "Benign",
|
| 14 |
+
5: "Malign",
|
| 15 |
+
6: "Acne",
|
| 16 |
+
7: "Actinic Keratosis",
|
| 17 |
+
8: "Basal Cell Carcinoma",
|
| 18 |
+
9: "Benign Keratosis",
|
| 19 |
+
10: "Dermatofibroma",
|
| 20 |
+
11: "Melanocytic Nevus",
|
| 21 |
+
12: "Melanoma",
|
| 22 |
+
13: "Vascular Lesion",
|
| 23 |
+
14: "Warts/Molluscum"
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
# Risk assessment mapping
|
| 27 |
+
risk_categories = {
|
| 28 |
+
"high_risk": [5, 12, 8], # Malign, Melanoma, Basal Cell Carcinoma
|
| 29 |
+
"medium_risk": [7, 0, 13], # Actinic Keratosis, Enfeksiyonel, Vascular
|
| 30 |
+
"low_risk": [1, 2, 3, 4, 6, 9, 10, 11, 14] # Diğerleri
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
@gr.cache
|
| 34 |
+
def load_model():
|
| 35 |
+
"""Senin trained ViT modelini yükle"""
|
| 36 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 37 |
+
|
| 38 |
+
# Model ve processor yükle (local files'dan)
|
| 39 |
+
model = ViTForImageClassification.from_pretrained("./", local_files_only=True)
|
| 40 |
+
processor = ViTImageProcessor.from_pretrained("google/vit-base-patch16-224-in21k")
|
| 41 |
+
|
| 42 |
+
model.to(device)
|
| 43 |
+
model.eval()
|
| 44 |
+
|
| 45 |
+
return model, processor, device
|
| 46 |
+
|
| 47 |
+
def predict_skin_condition(image):
|
| 48 |
+
"""Ana prediction fonksiyonu"""
|
| 49 |
+
|
| 50 |
+
if image is None:
|
| 51 |
+
return {}, "Lütfen bir görüntü yükleyin"
|
| 52 |
+
|
| 53 |
+
try:
|
| 54 |
+
# Model yükle
|
| 55 |
+
model, processor, device = load_model()
|
| 56 |
+
|
| 57 |
+
# Image preprocessing
|
| 58 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 59 |
+
inputs = {k: v.to(device) for k, v in inputs.items()}
|
| 60 |
+
|
| 61 |
+
# Prediction
|
| 62 |
+
with torch.no_grad():
|
| 63 |
+
outputs = model(**inputs)
|
| 64 |
+
logits = outputs.logits
|
| 65 |
+
probabilities = torch.softmax(logits, dim=-1)[0]
|
| 66 |
+
|
| 67 |
+
# Results dictionary oluştur
|
| 68 |
+
results = {}
|
| 69 |
+
for idx, prob in enumerate(probabilities):
|
| 70 |
+
if idx in class_names:
|
| 71 |
+
results[class_names[idx]] = float(prob)
|
| 72 |
+
|
| 73 |
+
# En yüksek tahmin
|
| 74 |
+
top_pred_idx = torch.argmax(probabilities).item()
|
| 75 |
+
top_class = class_names[top_pred_idx]
|
| 76 |
+
confidence = float(probabilities[top_pred_idx])
|
| 77 |
+
|
| 78 |
+
# Risk assessment
|
| 79 |
+
risk_html = get_risk_assessment(top_pred_idx, confidence, top_class)
|
| 80 |
+
|
| 81 |
+
return results, risk_html
|
| 82 |
+
|
| 83 |
+
except Exception as e:
|
| 84 |
+
return {}, f"Hata oluştu: {str(e)}"
|
| 85 |
+
|
| 86 |
+
def get_risk_assessment(pred_idx, confidence, class_name):
|
| 87 |
+
"""Risk değerlendirmesi yap"""
|
| 88 |
+
|
| 89 |
+
if pred_idx in risk_categories["high_risk"] and confidence > 0.7:
|
| 90 |
+
risk_level = "🚨 YÜKSEK RİSK"
|
| 91 |
+
message = f"<strong>{class_name}</strong> tespit edildi. Dermatolog konsültasyonu ÖNERİLİR."
|
| 92 |
+
color = "#FF4444"
|
| 93 |
+
elif pred_idx in risk_categories["medium_risk"] and confidence > 0.5:
|
| 94 |
+
risk_level = "⚠️ ORTA RİSK"
|
| 95 |
+
message = f"<strong>{class_name}</strong> tespit edildi. Takip önerilir."
|
| 96 |
+
color = "#FF8800"
|
| 97 |
+
else:
|
| 98 |
+
risk_level = "✅ DÜŞÜK RİSK"
|
| 99 |
+
message = f"<strong>{class_name}</strong> tespit edildi. Rutin kontrol yeterli."
|
| 100 |
+
color = "#00AA44"
|
| 101 |
+
|
| 102 |
+
return f"""
|
| 103 |
+
<div style='padding: 15px; background-color: {color}; color: white; border-radius: 10px; text-align: center; margin: 10px 0;'>
|
| 104 |
+
<h3 style='margin: 0 0 10px 0;'>{risk_level}</h3>
|
| 105 |
+
<p style='margin: 0; font-size: 16px;'>{message}</p>
|
| 106 |
+
<p style='margin: 5px 0 0 0; font-size: 14px;'>Güven Skoru: {confidence:.1%}</p>
|
| 107 |
+
</div>
|
| 108 |
+
"""
|
| 109 |
+
|
| 110 |
+
# CSS styling
|
| 111 |
+
css = """
|
| 112 |
+
.gradio-container {
|
| 113 |
+
max-width: 1000px;
|
| 114 |
+
margin: 0 auto;
|
| 115 |
+
font-family: 'Segoe UI', sans-serif;
|
| 116 |
+
}
|
| 117 |
+
.title {
|
| 118 |
+
text-align: center;
|
| 119 |
+
color: #2E86AB;
|
| 120 |
+
margin-bottom: 20px;
|
| 121 |
+
}
|
| 122 |
+
"""
|
| 123 |
+
|
| 124 |
+
# Gradio interface
|
| 125 |
+
with gr.Blocks(css=css, title="ViT Cilt Hastalığı Analizi") as demo:
|
| 126 |
+
|
| 127 |
+
gr.Markdown("""
|
| 128 |
+
# 🔬 ViT Cilt Hastalığı AI Analizi
|
| 129 |
+
### 15 farklı cilt hastalığını tespit eden yapay zeka sistemi
|
| 130 |
+
**Accuracy: %97 | Model: Vision Transformer | Classes: 15**
|
| 131 |
+
|
| 132 |
+
---
|
| 133 |
+
""")
|
| 134 |
+
|
| 135 |
+
with gr.Row():
|
| 136 |
+
with gr.Column(scale=1):
|
| 137 |
+
input_image = gr.Image(
|
| 138 |
+
label="📸 Cilt Görüntüsü Yükleyin",
|
| 139 |
+
type="pil",
|
| 140 |
+
height=400
|
| 141 |
+
)
|
| 142 |
+
|
| 143 |
+
predict_btn = gr.Button(
|
| 144 |
+
"🔍 AI Analizi Başlat",
|
| 145 |
+
variant="primary",
|
| 146 |
+
size="lg"
|
| 147 |
+
)
|
| 148 |
+
|
| 149 |
+
with gr.Column(scale=1):
|
| 150 |
+
prediction_output = gr.Label(
|
| 151 |
+
label="📊 Tahmin Sonuçları",
|
| 152 |
+
num_top_classes=5
|
| 153 |
+
)
|
| 154 |
+
|
| 155 |
+
risk_output = gr.HTML(
|
| 156 |
+
label="⚠️ Risk Değerlendirmesi"
|
| 157 |
+
)
|
| 158 |
+
|
| 159 |
+
# Sınıf açıklamaları
|
| 160 |
+
gr.Markdown("""
|
| 161 |
+
### 📋 Tespit Edilen Hastalık Kategorileri:
|
| 162 |
+
|
| 163 |
+
**Yüksek Risk:** Malign, Melanoma, Basal Cell Carcinoma
|
| 164 |
+
**Orta Risk:** Actinic Keratosis, Enfeksiyonel, Vascular Lesion
|
| 165 |
+
**Düşük Risk:** Ekzama, Akne, Pigment, Benign, Dermatofibroma, vb.
|
| 166 |
+
|
| 167 |
+
⚠️ **Önemli:** Bu sistem sadece ön değerlendirme içindir. Kesin tanı için doktora başvurun.
|
| 168 |
+
""")
|
| 169 |
+
|
| 170 |
+
# Event handling
|
| 171 |
+
predict_btn.click(
|
| 172 |
+
fn=predict_skin_condition,
|
| 173 |
+
inputs=input_image,
|
| 174 |
+
outputs=[prediction_output, risk_output]
|
| 175 |
+
)
|
| 176 |
+
|
| 177 |
+
if __name__ == "__main__":
|
| 178 |
+
demo.launch(
|
| 179 |
+
share=True,
|
| 180 |
+
server_name="0.0.0.0",
|
| 181 |
+
server_port=7860
|
| 182 |
)
|