Mewish commited on
Commit
9f5d1e5
·
verified ·
1 Parent(s): 1a1572a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -9
app.py CHANGED
@@ -1,19 +1,18 @@
1
  import gradio as gr
2
  import torch
3
  import torch.nn as nn
4
- import pandas as pd
5
  from PIL import Image
6
  from transformers import BlipProcessor, BlipForConditionalGeneration, AutoTokenizer, AutoModel
7
 
8
- # --- Load BLIP model ---
9
- blip_path = "Models/blip_skin_cancer"
10
- processor = BlipProcessor.from_pretrained(blip_path)
11
- blip_model = BlipForConditionalGeneration.from_pretrained(blip_path).to("cpu")
12
 
13
- # --- Load BioClinicalBERT + BiLSTM classifier ---
14
  tokenizer = AutoTokenizer.from_pretrained("emilyalsentzer/Bio_ClinicalBERT")
15
  bert_model = AutoModel.from_pretrained("emilyalsentzer/Bio_ClinicalBERT")
16
 
 
17
  class BiLSTMClassifier(nn.Module):
18
  def __init__(self, bert_model, hidden_dim=256, num_classes=7, dropout=0.5):
19
  super().__init__()
@@ -30,9 +29,10 @@ class BiLSTMClassifier(nn.Module):
30
  pooled = self.dropout(pooled)
31
  return self.fc(pooled)
32
 
33
- # --- Load trained classifier weights ---
34
  classifier = BiLSTMClassifier(bert_model).to("cpu")
35
- classifier.load_state_dict(torch.load("Models/classifier.pt", map_location="cpu"))
 
36
  classifier.eval()
37
 
38
  # --- Classes ---
@@ -60,4 +60,25 @@ def predict(image):
60
  attention_mask = text_inputs["attention_mask"].to("cpu")
61
 
62
  with torch.no_grad():
63
- outputs
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import torch
3
  import torch.nn as nn
 
4
  from PIL import Image
5
  from transformers import BlipProcessor, BlipForConditionalGeneration, AutoTokenizer, AutoModel
6
 
7
+ # --- Load BLIP model from Hugging Face Hub ---
8
+ processor = BlipProcessor.from_pretrained("Mewish/blip_medical")
9
+ blip_model = BlipForConditionalGeneration.from_pretrained("Mewish/blip_medical").to("cpu")
 
10
 
11
+ # --- Load BioClinicalBERT backbone ---
12
  tokenizer = AutoTokenizer.from_pretrained("emilyalsentzer/Bio_ClinicalBERT")
13
  bert_model = AutoModel.from_pretrained("emilyalsentzer/Bio_ClinicalBERT")
14
 
15
+ # --- Define BiLSTM classifier ---
16
  class BiLSTMClassifier(nn.Module):
17
  def __init__(self, bert_model, hidden_dim=256, num_classes=7, dropout=0.5):
18
  super().__init__()
 
29
  pooled = self.dropout(pooled)
30
  return self.fc(pooled)
31
 
32
+ # --- Load classifier weights from Hugging Face Hub ---
33
  classifier = BiLSTMClassifier(bert_model).to("cpu")
34
+ state_dict_url = "https://huggingface.co/Mewish/skin_cancer_classifier/resolve/main/classifier.pt"
35
+ classifier.load_state_dict(torch.hub.load_state_dict_from_url(state_dict_url, map_location="cpu"))
36
  classifier.eval()
37
 
38
  # --- Classes ---
 
60
  attention_mask = text_inputs["attention_mask"].to("cpu")
61
 
62
  with torch.no_grad():
63
+ outputs = classifier(input_ids, attention_mask)
64
+ probs = torch.softmax(outputs, dim=1).cpu().numpy()[0]
65
+ pred_class = classes[probs.argmax()]
66
+ confidence = probs.max()
67
+
68
+ return caption, pred_class, float(confidence)
69
+
70
+ # --- Gradio Interface ---
71
+ iface = gr.Interface(
72
+ fn=predict,
73
+ inputs=gr.Image(type="pil"),
74
+ outputs=[
75
+ gr.Textbox(label="Generated Caption"),
76
+ gr.Textbox(label="Predicted Class"),
77
+ gr.Number(label="Confidence Score")
78
+ ],
79
+ title="Skin Cancer AI Agent",
80
+ description="Upload a lesion image to generate a caption and predict the cancer type."
81
+ )
82
+
83
+ if __name__ == "__main__":
84
+ iface.launch