GSMK commited on
Commit
90df017
·
verified ·
1 Parent(s): 1492b06

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -143
app.py CHANGED
@@ -11,11 +11,10 @@ from transformers import (
11
 
12
  import open_clip
13
 
14
-
15
  st.set_page_config(page_title="Multi-Domain Zero Shot AI", layout="wide")
16
 
17
  st.title("Multi-Domain Zero Shot Image Classification")
18
- st.write("BioMedCLIP + RemoteCLIP + CLIP + BLIP Captioning")
19
 
20
  device = "cpu"
21
 
@@ -27,22 +26,19 @@ device = "cpu"
27
  @st.cache_resource
28
  def load_models():
29
 
30
- # -------- BIO MED CLIP --------
31
- biomed_model, _, biomed_preprocess = open_clip.create_model_and_transforms(
32
- "ViT-B-16",
33
- pretrained="hf-hub:microsoft/BiomedCLIP-PubMedBERT_256-vit_base_patch16_224"
34
- )
35
 
36
- biomed_tokenizer = open_clip.get_tokenizer(
37
- "hf-hub:microsoft/BiomedCLIP-PubMedBERT_256-vit_base_patch16_224"
38
  )
39
 
40
- biomed_model = biomed_model.to(device).eval()
41
-
42
  # -------- REMOTE CLIP --------
43
  remote_model, _, remote_preprocess = open_clip.create_model_and_transforms(
44
  "ViT-B-32",
45
- pretrained="hf-hub:microsoft/remoteclip-vit-base"
46
  )
47
 
48
  remote_tokenizer = open_clip.get_tokenizer("ViT-B-32")
@@ -69,8 +65,7 @@ def load_models():
69
 
70
  return (
71
  biomed_model,
72
- biomed_preprocess,
73
- biomed_tokenizer,
74
  remote_model,
75
  remote_preprocess,
76
  remote_tokenizer,
@@ -83,8 +78,7 @@ def load_models():
83
 
84
  (
85
  biomed_model,
86
- biomed_preprocess,
87
- biomed_tokenizer,
88
  remote_model,
89
  remote_preprocess,
90
  remote_tokenizer,
@@ -144,110 +138,6 @@ templates = {
144
  }
145
 
146
 
147
- # --------------------------------------------------
148
- # EXPLANATIONS
149
- # --------------------------------------------------
150
-
151
- EXPLANATIONS = {
152
-
153
- "pneumonia": """
154
- Cloudy or opaque regions may appear in lung areas.
155
- These patterns indicate possible infection or inflammation.
156
- Fluid buildup inside air sacs reduces oxygen exchange.
157
- Symptoms may include cough and breathing difficulty.
158
- Medical confirmation is required.
159
- """,
160
-
161
- "Normal": """
162
- Lungs appear clear without visible opacities.
163
- Both lung regions look symmetrical.
164
- No fluid accumulation is visible.
165
- The diaphragm boundaries appear normal.
166
- These features indicate healthy lungs.
167
- """,
168
-
169
- "Melanoma": """
170
- A dark pigmented lesion may appear on skin.
171
- Irregular borders and uneven color can occur.
172
- Melanoma is a serious form of skin cancer.
173
- Early detection improves treatment success.
174
- Consult a dermatologist for confirmation.
175
- """,
176
-
177
- "eczema": """
178
- Skin may appear red and inflamed.
179
- Dry and flaky patches are visible.
180
- Eczema causes itching and irritation.
181
- Often triggered by allergies or skin sensitivity.
182
- Treatment helps control symptoms.
183
- """,
184
-
185
- "psoriasis": """
186
- Thick red patches with white scales may appear.
187
- Skin cell growth becomes abnormally rapid.
188
- Affected regions can become itchy or cracked.
189
- It is an autoimmune skin condition.
190
- Dermatology treatment is recommended.
191
- """,
192
-
193
- "Normal Skin": """
194
- Skin tone appears even and smooth.
195
- No visible lesions or inflammation.
196
- Texture looks consistent across the surface.
197
- No abnormal pigmentation is visible.
198
- These features indicate healthy skin.
199
- """,
200
-
201
- "HIGHWAY": """
202
- A long linear road structure is visible.
203
- The road may extend across large areas.
204
- Vehicles typically use these routes for travel.
205
- Nearby regions may include urban infrastructure.
206
- Such patterns indicate highways.
207
- """,
208
-
209
- "RIVER": """
210
- A long winding water body is visible.
211
- Rivers often appear curved across terrain.
212
- They transport water through landscapes.
213
- Vegetation or farmland may surround them.
214
- This pattern indicates a natural river.
215
- """,
216
-
217
- "FOREST": """
218
- The region appears densely covered with trees.
219
- Vegetation creates textured green patterns.
220
- Different shades indicate varying tree heights.
221
- Forests support diverse ecosystems.
222
- This pattern indicates forest land.
223
- """,
224
-
225
- "INDUSTRIAL": """
226
- Large rectangular buildings are visible.
227
- Industrial zones contain factories and warehouses.
228
- Road networks connect production facilities.
229
- Structures appear dense and organized.
230
- Such patterns indicate industrial areas.
231
- """,
232
-
233
- "CROP": """
234
- Fields appear in organized rectangular patterns.
235
- Different shades indicate crop growth stages.
236
- Irrigation channels may be visible.
237
- Agricultural land is clearly structured.
238
- This pattern indicates cultivated crops.
239
- """,
240
-
241
- "HEALTHY": """
242
- Leaf surface appears green and smooth.
243
- No visible fungal spots are present.
244
- Leaf veins look healthy and intact.
245
- Edges appear natural and undamaged.
246
- These features indicate a healthy plant.
247
- """
248
- }
249
-
250
-
251
  # --------------------------------------------------
252
  # SIDEBAR
253
  # --------------------------------------------------
@@ -284,20 +174,27 @@ if uploaded_file:
284
 
285
 
286
  # --------------------------------------------------
287
- # MODEL SELECTION
288
  # --------------------------------------------------
289
 
290
  if dataset_key in ["medical", "skin_disease"]:
291
 
292
- img = biomed_preprocess(image).unsqueeze(0).to(device)
293
- text = biomed_tokenizer(text_queries)
 
 
 
 
294
 
295
  with torch.no_grad():
296
- image_features = biomed_model.encode_image(img)
297
- text_features = biomed_model.encode_text(text)
 
298
 
299
- similarity = (image_features @ text_features.T).softmax(dim=-1)
300
 
 
 
 
301
 
302
  elif dataset_key == "satellite":
303
 
@@ -311,6 +208,10 @@ if uploaded_file:
311
  similarity = (image_features @ text_features.T).softmax(dim=-1)
312
 
313
 
 
 
 
 
314
  else:
315
 
316
  inputs = clip_processor(
@@ -349,20 +250,4 @@ if uploaded_file:
349
  )
350
 
351
  st.subheader("Image Description (BLIP)")
352
- st.write(caption)
353
-
354
-
355
- # --------------------------------------------------
356
- # EXPLANATION
357
- # --------------------------------------------------
358
-
359
- explanation = EXPLANATIONS.get(
360
- predicted_class,
361
- "No explanation available."
362
- )
363
-
364
- st.subheader("Detailed Explanation")
365
-
366
- for line in explanation.strip().split("\n"):
367
- if line.strip():
368
- st.write(line.strip())
 
11
 
12
  import open_clip
13
 
 
14
  st.set_page_config(page_title="Multi-Domain Zero Shot AI", layout="wide")
15
 
16
  st.title("Multi-Domain Zero Shot Image Classification")
17
+ st.write("BiomedCLIP + RemoteCLIP + CLIP + BLIP Captioning")
18
 
19
  device = "cpu"
20
 
 
26
  @st.cache_resource
27
  def load_models():
28
 
29
+ # -------- BIOMED CLIP (via Transformers CLIP) --------
30
+ biomed_model = CLIPModel.from_pretrained(
31
+ "microsoft/BiomedCLIP-PubMedBERT_256-vit_base_patch16_224"
32
+ ).to(device).eval()
 
33
 
34
+ biomed_processor = CLIPProcessor.from_pretrained(
35
+ "microsoft/BiomedCLIP-PubMedBERT_256-vit_base_patch16_224"
36
  )
37
 
 
 
38
  # -------- REMOTE CLIP --------
39
  remote_model, _, remote_preprocess = open_clip.create_model_and_transforms(
40
  "ViT-B-32",
41
+ pretrained="laion2b_s34b_b79k"
42
  )
43
 
44
  remote_tokenizer = open_clip.get_tokenizer("ViT-B-32")
 
65
 
66
  return (
67
  biomed_model,
68
+ biomed_processor,
 
69
  remote_model,
70
  remote_preprocess,
71
  remote_tokenizer,
 
78
 
79
  (
80
  biomed_model,
81
+ biomed_processor,
 
82
  remote_model,
83
  remote_preprocess,
84
  remote_tokenizer,
 
138
  }
139
 
140
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
141
  # --------------------------------------------------
142
  # SIDEBAR
143
  # --------------------------------------------------
 
174
 
175
 
176
  # --------------------------------------------------
177
+ # MEDICAL / SKIN (BIOMEDCLIP)
178
  # --------------------------------------------------
179
 
180
  if dataset_key in ["medical", "skin_disease"]:
181
 
182
+ inputs = biomed_processor(
183
+ text=text_queries,
184
+ images=image,
185
+ return_tensors="pt",
186
+ padding=True
187
+ ).to(device)
188
 
189
  with torch.no_grad():
190
+ outputs = biomed_model(**inputs)
191
+
192
+ similarity = outputs.logits_per_image.softmax(dim=1)
193
 
 
194
 
195
+ # --------------------------------------------------
196
+ # SATELLITE (REMOTE CLIP)
197
+ # --------------------------------------------------
198
 
199
  elif dataset_key == "satellite":
200
 
 
208
  similarity = (image_features @ text_features.T).softmax(dim=-1)
209
 
210
 
211
+ # --------------------------------------------------
212
+ # AGRICULTURE (CLIP)
213
+ # --------------------------------------------------
214
+
215
  else:
216
 
217
  inputs = clip_processor(
 
250
  )
251
 
252
  st.subheader("Image Description (BLIP)")
253
+ st.write(caption)