eyupipler commited on
Commit
72c85cf
·
verified ·
1 Parent(s): e0843a5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -3
app.py CHANGED
@@ -1,3 +1,5 @@
 
 
1
  import torch
2
  import warnings
3
  import numpy as np
@@ -17,18 +19,29 @@ from model import (
17
  warnings.filterwarnings("ignore", category=FutureWarning)
18
  warnings.filterwarnings("ignore", category=UserWarning)
19
 
 
 
 
 
20
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
21
 
 
22
  cnn_model_f = load_classification_model(device, model_type="f", num_classes=6)
23
  cnn_model_c = load_classification_model(device, model_type="c", num_classes=6)
24
  cnn_model_q = load_classification_model(device, model_type="q", num_classes=6)
25
 
 
26
  t5_tokenizer, t5_model = load_t5_model(device)
27
 
 
28
  perf_metrics_f = calculate_performance_metrics(cnn_model_f, device)
29
  perf_metrics_c = calculate_performance_metrics(cnn_model_c, device)
30
  perf_metrics_q = calculate_performance_metrics(cnn_model_q, device)
31
 
 
 
 
 
32
  class_names_en = [
33
  "Alzheimer Disease",
34
  "Mild Alzheimer Risk",
@@ -46,21 +59,30 @@ en2tr = {
46
  "Parkinson Disease": "Parkinson Hastalığı"
47
  }
48
 
 
 
 
 
49
  def gradio_predict(image, model_type, question):
 
 
 
 
 
 
50
  if model_type == "f":
51
  cnn_model = cnn_model_f
52
- perf_metrics = perf_metrics_f
53
  elif model_type == "c":
54
  cnn_model = cnn_model_c
55
- perf_metrics = perf_metrics_c
56
  else:
57
  cnn_model = cnn_model_q
58
- perf_metrics = perf_metrics_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,12 +105,14 @@ def gradio_predict(image, model_type, question):
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
 
1
+ # app.py
2
+
3
  import torch
4
  import warnings
5
  import numpy as np
 
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",
 
59
  "Parkinson Disease": "Parkinson Hastalığı"
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
  )
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