eyupipler commited on
Commit
e24f336
·
verified ·
1 Parent(s): 30f8058

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +2 -26
app.py CHANGED
@@ -1,5 +1,3 @@
1
- # app.py
2
-
3
  import torch
4
  import warnings
5
  import numpy as np
@@ -20,28 +18,19 @@ warnings.filterwarnings("ignore", category=FutureWarning)
20
  warnings.filterwarnings("ignore", category=UserWarning)
21
 
22
 
23
- # ------------------------------------------------------------
24
- # 1) CİHAZ AYARI ve MODELLERİN ÖN YÜKLENMESİ
25
- # ------------------------------------------------------------
26
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
27
 
28
- # a) CNN modellerini yükle (Vbai-DPA-2.3 repo’su)
29
  cnn_model_f = load_classification_model(device, model_type="f", num_classes=6)
30
  cnn_model_c = load_classification_model(device, model_type="c", num_classes=6)
31
  cnn_model_q = load_classification_model(device, model_type="q", num_classes=6)
32
 
33
- # b) T5 tokenizer + modelini yükle (Tbai-DPA-1-0 repo’su)
34
  t5_tokenizer, t5_model = load_t5_model(device)
35
 
36
- # c) Performans metriklerini bir kez hesapla (opsiyonel)
37
  perf_metrics_f = calculate_performance_metrics(cnn_model_f, device)
38
  perf_metrics_c = calculate_performance_metrics(cnn_model_c, device)
39
  perf_metrics_q = calculate_performance_metrics(cnn_model_q, device)
40
 
41
 
42
- # ------------------------------------------------------------
43
- # 2) SABİT VERİLER
44
- # ------------------------------------------------------------
45
  class_names_en = [
46
  "Alzheimer Disease",
47
  "Mild Alzheimer Risk",
@@ -60,29 +49,18 @@ en2tr = {
60
  }
61
 
62
 
63
- # ------------------------------------------------------------
64
- # 3) GRADIO PREDİKTE FONKSİYONU
65
- # ------------------------------------------------------------
66
  def gradio_predict(image, model_type, question):
67
- """
68
- - image: PIL Image (kullanıcının yüklediği)
69
- - model_type: "f", "c" veya "q"
70
- - question: isteğe bağlı metin
71
- """
72
- # A) Seçilen CNN modelini al
73
  if model_type == "f":
74
  cnn_model = cnn_model_f
75
  elif model_type == "c":
76
  cnn_model = cnn_model_c
77
  else:
78
  cnn_model = cnn_model_q
79
-
80
- # B) Görüntü sınıflandırma
81
  idx, conf, inp_tensor, all_probs = predict_image(cnn_model, image, device)
82
  pred_en = class_names_en[idx]
83
  pred_tr = en2tr[pred_en]
84
-
85
- # C) T5 yorumu üret (eğer soru varsa prompt’a ekleyelim)
86
  if not question or question.strip() == "":
87
  comment = generate_comment_turkce(t5_tokenizer, t5_model, pred_tr, device)
88
  else:
@@ -105,14 +83,12 @@ def gradio_predict(image, model_type, question):
105
  )
106
  comment = t5_tokenizer.decode(out_ids[0], skip_special_tokens=True)
107
 
108
- # D) Görüntüyü normalize edip numpy array’e dönüştür
109
  inp_np = inp_tensor.squeeze(0).permute(1, 2, 0).cpu().numpy()
110
  mean = np.array([0.485, 0.456, 0.406])
111
  std = np.array([0.229, 0.224, 0.225])
112
  img_show = inp_np * std + mean
113
  img_show = np.clip(img_show, 0, 1)
114
 
115
- # E) Ekrana dönecek metinler
116
  tahmin_metni = f"Tahmin: {pred_en} — {conf:.2f}%"
117
 
118
  return img_show, tahmin_metni, comment
 
 
 
1
  import torch
2
  import warnings
3
  import numpy as np
 
18
  warnings.filterwarnings("ignore", category=UserWarning)
19
 
20
 
 
 
 
21
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
22
 
 
23
  cnn_model_f = load_classification_model(device, model_type="f", num_classes=6)
24
  cnn_model_c = load_classification_model(device, model_type="c", num_classes=6)
25
  cnn_model_q = load_classification_model(device, model_type="q", num_classes=6)
26
 
 
27
  t5_tokenizer, t5_model = load_t5_model(device)
28
 
 
29
  perf_metrics_f = calculate_performance_metrics(cnn_model_f, device)
30
  perf_metrics_c = calculate_performance_metrics(cnn_model_c, device)
31
  perf_metrics_q = calculate_performance_metrics(cnn_model_q, device)
32
 
33
 
 
 
 
34
  class_names_en = [
35
  "Alzheimer Disease",
36
  "Mild Alzheimer Risk",
 
49
  }
50
 
51
 
 
 
 
52
  def gradio_predict(image, model_type, question):
 
 
 
 
 
 
53
  if model_type == "f":
54
  cnn_model = cnn_model_f
55
  elif model_type == "c":
56
  cnn_model = cnn_model_c
57
  else:
58
  cnn_model = cnn_model_q
59
+
 
60
  idx, conf, inp_tensor, all_probs = predict_image(cnn_model, image, device)
61
  pred_en = class_names_en[idx]
62
  pred_tr = en2tr[pred_en]
63
+
 
64
  if not question or question.strip() == "":
65
  comment = generate_comment_turkce(t5_tokenizer, t5_model, pred_tr, device)
66
  else:
 
83
  )
84
  comment = t5_tokenizer.decode(out_ids[0], skip_special_tokens=True)
85
 
 
86
  inp_np = inp_tensor.squeeze(0).permute(1, 2, 0).cpu().numpy()
87
  mean = np.array([0.485, 0.456, 0.406])
88
  std = np.array([0.229, 0.224, 0.225])
89
  img_show = inp_np * std + mean
90
  img_show = np.clip(img_show, 0, 1)
91
 
 
92
  tahmin_metni = f"Tahmin: {pred_en} — {conf:.2f}%"
93
 
94
  return img_show, tahmin_metni, comment