aryn25 commited on
Commit
d9803e8
·
verified ·
1 Parent(s): facd27b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -0
app.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import BlipProcessor, BlipForConditionalGeneration
2
+ from sentence_transformers import SentenceTransformer
3
+ import gradio as gr
4
+ from PIL import Image
5
+ import json
6
+ import torch
7
+ import faiss
8
+
9
+ # -----------------------------
10
+ # 1. Load BLIP Model
11
+ # -----------------------------
12
+ blip_processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
13
+ blip_model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
14
+
15
+ # -----------------------------
16
+ # 2. Load Vector DB (Fish Facts)
17
+ # -----------------------------
18
+ # Sample fish knowledge base
19
+ fish_data = [
20
+ {"name": "stonefish", "text": "Stonefish is highly venomous and lives in Indo-Pacific coastal waters."},
21
+ {"name": "pufferfish", "text": "Pufferfish contains tetrodotoxin and is poisonous if not properly prepared."},
22
+ {"name": "lionfish", "text": "Lionfish has venomous spines and can cause painful stings."},
23
+ {"name": "tuna", "text": "Tuna is a large, non-poisonous fish consumed globally."},
24
+ {"name": "salmon", "text": "Salmon is a commonly eaten, non-poisonous fish found in freshwater and ocean habitats."}
25
+ ]
26
+
27
+ texts = [item["text"] for item in fish_data]
28
+ names = [item["name"] for item in fish_data]
29
+
30
+ # Encode fish data
31
+ embedder = SentenceTransformer('all-MiniLM-L6-v2')
32
+ embeddings = embedder.encode(texts)
33
+
34
+ # Create FAISS index
35
+ dim = embeddings[0].shape[0]
36
+ index = faiss.IndexFlatL2(dim)
37
+ index.add(embeddings)
38
+
39
+ # Toxic fish list
40
+ toxic_list = ["stonefish", "pufferfish", "lionfish"]
41
+
42
+ # -----------------------------
43
+ # 3. Fish Identifier Function
44
+ # -----------------------------
45
+ def identify_fish(image):
46
+ # Step 1: Generate caption from image
47
+ inputs = blip_processor(image, return_tensors="pt")
48
+ out = blip_model.generate(**inputs)
49
+ caption = blip_processor.decode(out[0], skip_special_tokens=True)
50
+
51
+ # Step 2: Search fish knowledge base
52
+ query_embed = embedder.encode([caption])
53
+ D, I = index.search(query_embed, k=1)
54
+ matched_fish = fish_data[I[0][0]]
55
+ name = matched_fish["name"]
56
+ info = matched_fish["text"]
57
+
58
+ # Step 3: Classify toxicity
59
+ is_poisonous = "Yes 🧪" if name in toxic_list else "No ✅"
60
+
61
+ # Step 4: Return result
62
+ return f"**Image Caption:** {caption}\n\n**Detected Fish:** {name.title()}\n**Poisonous:** {is_poisonous}\n**Fact:** {info}"
63
+
64
+ # -----------------------------
65
+ # 4. Gradio UI
66
+ # -----------------------------
67
+ demo = gr.Interface(
68
+ fn=identify_fish,
69
+ inputs=gr.Image(type="pil"),
70
+ outputs="markdown",
71
+ title="🧠 Smart Fish Identifier (BLIP + RAG)",
72
+ description="Upload a fish image. We use BLIP to describe the fish, then match it with our fish fact knowledge base using RAG to determine if it's poisonous."
73
+ )
74
+
75
+ if __name__ == '__main__':
76
+ demo.launch()