File size: 13,063 Bytes
129fbd5 cbd040c 129fbd5 cbd040c 7232a5d cbd040c 7232a5d 129fbd5 cbd040c 129fbd5 cbd040c 129fbd5 cbd040c 129fbd5 71e10db 129fbd5 7232a5d 129fbd5 7232a5d cbd040c 129fbd5 71e10db 129fbd5 cbd040c 129fbd5 cbd040c 129fbd5 cbd040c 71e10db 129fbd5 7232a5d 129fbd5 71e10db cbd040c 71e10db 7232a5d 129fbd5 71e10db 129fbd5 71e10db 129fbd5 71e10db 129fbd5 cbd040c 129fbd5 7232a5d 129fbd5 cea28a4 129fbd5 cea28a4 129fbd5 71e10db cea28a4 7232a5d 129fbd5 cea28a4 71e10db 129fbd5 cbd040c 129fbd5 7232a5d 129fbd5 cbd040c 129fbd5 cbd040c cea28a4 71e10db cea28a4 129fbd5 7232a5d 129fbd5 7232a5d 129fbd5 cbd040c 7232a5d 129fbd5 cbd040c 129fbd5 cbd040c 129fbd5 71e10db 129fbd5 c046085 129fbd5 7232a5d 129fbd5 7232a5d 129fbd5 cbd040c 129fbd5 cbd040c 129fbd5 7232a5d cbd040c 129fbd5 cbd040c 129fbd5 7232a5d cbd040c 129fbd5 cbd040c 129fbd5 cbd040c 129fbd5 cbd040c 7232a5d cbd040c 129fbd5 7232a5d 129fbd5 7232a5d 129fbd5 cbd040c 129fbd5 cbd040c 129fbd5 71e10db 129fbd5 71e10db 129fbd5 cbd040c 129fbd5 71e10db 129fbd5 cbd040c 129fbd5 cbd040c 7232a5d cbd040c 7232a5d cbd040c 129fbd5 7232a5d 71e10db 129fbd5 cbd040c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 | """
VisualRAG β Multi-Modal AI System
==================================
Stack : YOLOv8n Β· CLIP ViT-B/32 Β· FAISS Β· Zephyr-7B Β· Gradio 4.40.0
Deploy: HuggingFace Spaces (CPU Basic β free tier)
Pipeline
Index : Image β YOLOv8 detection β CLIP embedding β FAISS vector store
Query : Text β CLIP text embedding β cosine k-NN β LLM answer generation
No monkey-patching needed with gradio 4.40.0 β the schema introspector
bug and starlette TemplateResponse API mismatch only affect 4.44.x.
"""
import json
import os
from datetime import datetime
import faiss
import gradio as gr
import numpy as np
import torch
from huggingface_hub import InferenceClient
from PIL import Image
from transformers import CLIPModel, CLIPProcessor
from ultralytics import YOLO
# ββ Model loading (runs once at Space start-up) ββββββββββββββββββββββββββββββββ
print("β³ Loading CLIP ViT-B/32 ...")
CLIP_MODEL_ID = "openai/clip-vit-base-patch32"
clip_model = CLIPModel.from_pretrained(CLIP_MODEL_ID)
clip_processor = CLIPProcessor.from_pretrained(CLIP_MODEL_ID)
clip_model.eval()
print("β³ Loading YOLOv8n ...")
yolo = YOLO("yolov8n.pt") # auto-downloads ~6 MB on first run
print("β³ Initialising LLM client ...")
# Free HF Serverless Inference β LLM runs on HF servers, not in the Space.
# Add HF_TOKEN as a Space Secret for higher rate limits.
HF_TOKEN = os.environ.get("HF_TOKEN", None)
llm = InferenceClient(model="HuggingFaceH4/zephyr-7b-beta", token=HF_TOKEN)
print("β
All models ready.")
# ββ FAISS vector store (in-memory, session-scoped) βββββββββββββββββββββββββββββ
EMBED_DIM = 512 # CLIP ViT-B/32 output dimension
faiss_index = faiss.IndexFlatIP(EMBED_DIM) # cosine similarity via L2-normalised dot product
image_store = [] # parallel list: one dict per indexed image
# ββ Embedding helpers ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def embed_image(pil_img: Image.Image) -> np.ndarray:
"""Return L2-normalised 512-d CLIP image embedding (shape 1Γ512)."""
inputs = clip_processor(images=pil_img, return_tensors="pt")
with torch.no_grad():
features = clip_model.get_image_features(**inputs)
emb = features.numpy().astype("float32")
faiss.normalize_L2(emb)
return emb
def embed_text(text: str) -> np.ndarray:
"""Return L2-normalised 512-d CLIP text embedding (shape 1Γ512)."""
inputs = clip_processor(text=[text], return_tensors="pt",
padding=True, truncation=True)
with torch.no_grad():
features = clip_model.get_text_features(**inputs)
emb = features.numpy().astype("float32")
faiss.normalize_L2(emb)
return emb
# ββ Detection pipeline βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def run_detection(pil_img: Image.Image):
"""Run YOLOv8n β return (annotated PIL, detections list, summary string)."""
results = yolo(np.array(pil_img))[0]
annotated = Image.fromarray(results.plot())
detections = []
if results.boxes is not None:
for box in results.boxes:
detections.append({
"label": yolo.names[int(box.cls[0])],
"confidence": round(float(box.conf[0]), 3),
})
counts = {}
for d in detections:
counts[d["label"]] = counts.get(d["label"], 0) + 1
summary = ", ".join(f"{v} {k}" for k, v in counts.items()) or "no objects detected"
return annotated, detections, summary
# ββ Index pipeline βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def index_image(image_np, note: str):
"""Detect β embed β store in FAISS. Called by the Index button."""
if image_np is None:
return None, "β Please upload an image first.", _badge()
pil_img = Image.fromarray(image_np)
annotated, detections, summary = run_detection(pil_img)
embedding = embed_image(pil_img)
faiss_index.add(embedding)
image_store.append({
"id": len(image_store),
"image": pil_img.copy(),
"annotated": annotated,
"detections": detections,
"summary": summary,
"note": note.strip() or "β",
"ts": datetime.now().strftime("%H:%M:%S"),
})
msg = f"β
Image #{len(image_store) - 1} indexed Β· Found: {summary}"
return annotated, msg, _badge()
def _badge() -> str:
return f"π¦ {len(image_store)} image(s) in vector store"
# ββ RAG query pipeline βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def query_images(text_query: str, top_k: int):
"""Text β CLIP embed β FAISS k-NN β RAG prompt β Zephyr-7B answer."""
if not image_store:
return None, "β No images indexed yet β upload images in the 'Detect & Index' tab first.", ""
if not text_query.strip():
return None, "β Please enter a question.", ""
query_emb = embed_text(text_query)
k = min(int(top_k), len(image_store))
scores, idxs = faiss_index.search(query_emb, k)
hits, ctx_lines = [], []
for rank, (score, idx) in enumerate(zip(scores[0], idxs[0])):
if idx < 0:
continue
item = image_store[int(idx)]
hits.append({
"rank": rank + 1,
"img_id": int(idx),
"score": round(float(score), 4),
"objects": item["summary"],
"note": item["note"],
})
ctx_lines.append(
f"[Image #{idx}] objects: {item['summary']} | "
f"note: {item['note']} | indexed at: {item['ts']} | "
f"cosine similarity: {score:.3f}"
)
context = "\n".join(ctx_lines)
prompt = (
"<|system|>\n"
"You are a concise visual-AI assistant. "
"Answer using only the retrieved image context below. "
"If context is insufficient, say so.\n"
"<|user|>\n"
f"Retrieved context:\n{context}\n\n"
f"Question: {text_query}\n"
"<|assistant|>\n"
)
try:
answer = llm.text_generation(
prompt,
max_new_tokens=300,
temperature=0.2,
repetition_penalty=1.1,
stop_sequences=["<|user|>", "<|system|>"],
).strip()
except Exception as exc:
answer = f"β οΈ LLM unavailable ({exc}).\n\nRaw retrieval context:\n{context}"
best_idx = int(idxs[0][0]) if len(idxs[0]) > 0 and idxs[0][0] >= 0 else None
best_image = image_store[best_idx]["annotated"] if best_idx is not None else None
return best_image, answer, json.dumps(hits, indent=2)
# ββ Gradio UI ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
CSS = """
.hero{text-align:center;padding:20px 0 4px}
.hero h1{font-size:28px;margin:0}
.hero p{color:var(--color-subdued);margin:6px 0 0}
.badge-row{display:flex;gap:8px;justify-content:center;flex-wrap:wrap;margin-top:10px}
.badge{background:var(--color-background-secondary);border:1px solid var(--border-color-primary);
border-radius:20px;padding:3px 12px;font-size:12px;color:var(--color-text-body)}
"""
with gr.Blocks(title="VisualRAG", theme=gr.themes.Soft(primary_hue="blue"), css=CSS) as demo:
gr.HTML("""
<div class="hero">
<h1>π VisualRAG</h1>
<p>Multi-Modal AI Β· Object Detection + Visual Embeddings + RAG + LLM</p>
<div class="badge-row">
<span class="badge">YOLOv8</span>
<span class="badge">CLIP ViT-B/32</span>
<span class="badge">FAISS</span>
<span class="badge">Zephyr-7B</span>
<span class="badge">Gradio 4.40.0</span>
</div>
</div>
""")
with gr.Tabs():
# ββ TAB 1: Detect & Index ββββββββββββββββββββββββββββββββββββββββββββββ
with gr.Tab("π€ Detect & Index"):
gr.Markdown(
"Upload any image. YOLOv8n detects objects, then CLIP ViT-B/32 "
"encodes it into a 512-d embedding stored in FAISS for later retrieval."
)
with gr.Row():
with gr.Column(scale=1):
img_in = gr.Image(label="Upload image", type="numpy")
note_in = gr.Textbox(label="Context note (optional)",
placeholder="e.g. 'Warehouse camera, aisle 3'")
index_btn = gr.Button("π Detect & Index", variant="primary")
with gr.Column(scale=1):
det_out = gr.Image(label="Detection result")
status_out = gr.Textbox(label="Status", interactive=False)
badge_out = gr.Textbox(label="Vector store", interactive=False,
value=_badge())
index_btn.click(
fn=index_image,
inputs=[img_in, note_in],
outputs=[det_out, status_out, badge_out],
)
# ββ TAB 2: Query (RAG) βββββββββββββββββββββββββββββββββββββββββββββββββ
with gr.Tab("π¬ Query (RAG)"):
gr.Markdown(
"Ask any question about your indexed images. CLIP embeds the query, "
"FAISS retrieves the most similar images by cosine similarity, "
"and Zephyr-7B generates a grounded answer."
)
with gr.Row():
with gr.Column(scale=1):
query_in = gr.Textbox(
label="Your question",
placeholder="e.g. 'How many people are visible?' or 'Are there any vehicles?'",
lines=3,
)
topk_sl = gr.Slider(minimum=1, maximum=5, value=3, step=1,
label="Top-K images to retrieve")
query_btn = gr.Button("π Search & Generate Answer", variant="primary")
with gr.Column(scale=1):
match_img = gr.Image(label="Best matching image")
llm_out = gr.Textbox(label="AI Answer (RAG-grounded)",
lines=6, interactive=False)
hits_out = gr.Textbox(label="Retrieval scores", interactive=False, lines=8)
query_btn.click(
fn=query_images,
inputs=[query_in, topk_sl],
outputs=[match_img, llm_out, hits_out],
)
# ββ TAB 3: How it works ββββββββββββββββββββββββββββββββββββββββββββββββ
with gr.Tab("ποΈ How it works"):
gr.Markdown("""
## System overview
### Index pipeline
```
Image β YOLOv8n detection (objects + confidence scores)
β CLIP ViT-B/32 image encoder β 512-d embedding
β L2 normalisation
β FAISS IndexFlatIP (cosine similarity store)
```
### Query / RAG pipeline
```
Text query β CLIP text encoder β 512-d query embedding
β L2 normalisation
β FAISS k-NN search (cosine similarity, top-K)
β RAG prompt = query + retrieved context
β Zephyr-7B-Ξ² (HF Serverless Inference API)
β Natural language answer
```
## Stack
| Component | Technology |
|---|---|
| Object detection | YOLOv8n (Ultralytics) |
| Visual embedding | CLIP ViT-B/32 (OpenAI via HF) |
| Vector index | FAISS IndexFlatIP (cosine sim) |
| LLM | Zephyr-7B-Ξ² (HF Serverless API) |
| UI | Gradio 4.40.0 |
## Why gradio 4.40.0
Version 4.44.1 has three cascading runtime bugs on HF Spaces: a schema
introspector TypeError, a non-existent gradio_client pin, and a starlette
TemplateResponse API mismatch that causes a Jinja2 `unhashable type: dict`
crash. Version 4.40.0 is widely deployed and has none of these issues.
""")
gr.HTML("""
<div style="text-align:center;padding:14px 0 4px;color:var(--color-subdued);font-size:12px">
VisualRAG Β· YOLOv8 + CLIP + FAISS + LLM Β· HuggingFace Spaces
</div>
""")
demo.launch(server_name="0.0.0.0") |