tharu22 commited on
Commit
4297f41
Β·
1 Parent(s): dbab50b
Files changed (1) hide show
  1. app.py +101 -0
app.py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import streamlit as st
3
+ import requests
4
+ import torch
5
+ import pinecone
6
+ import numpy as np
7
+ from io import BytesIO
8
+ from PIL import Image
9
+ from transformers import AutoProcessor, CLIPModel
10
+ import logging
11
+ import time
12
+
13
+ # βœ… Configure Logging
14
+ logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
15
+ logger = logging.getLogger(__name__)
16
+
17
+ # βœ… Initialize Pinecone
18
+ pc = pinecone.Pinecone(api_key="pcsk_4r5jtC_N5ZNTHpGq2DJUAfZx33GmuXz7Jib6jKVQJuovkdUfB5qrw9njgCRTVrmJVMXbpC") # Replace with your API key
19
+ index_name = "unsplash-index"
20
+
21
+ # βœ… Check if the index exists, otherwise create it
22
+ existing_indexes = [index.name for index in pc.list_indexes()]
23
+ if index_name not in existing_indexes:
24
+ pc.create_index(
25
+ name=index_name,
26
+ metric="cosine",
27
+ dimension=512,
28
+ spec=pinecone.ServerlessSpec(cloud="aws", region="us-east-1")
29
+ )
30
+ while not pc.describe_index(index_name).status.get("ready", False):
31
+ logger.info("Waiting for index to be ready...")
32
+ time.sleep(1)
33
+
34
+ # Connect to Pinecone index
35
+ index = pc.Index(index_name)
36
+
37
+ # βœ… Load CLIP Model
38
+ @st.cache_resource
39
+ def load_clip():
40
+ model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
41
+ processor = AutoProcessor.from_pretrained("openai/clip-vit-base-patch32")
42
+ return model, processor
43
+
44
+ model, processor = load_clip()
45
+
46
+ # βœ… Streamlit UI
47
+ st.title("πŸ” Image & Text Search with CLIP & Pinecone")
48
+
49
+ # πŸ“Œ **Option 1: Upload Image for Search**
50
+ st.subheader("πŸ“€ Upload an Image to Search")
51
+ uploaded_file = st.file_uploader("Upload an image...", type=["jpg", "png", "jpeg"])
52
+
53
+ if uploaded_file:
54
+ # Convert file to Image
55
+ image = Image.open(uploaded_file).convert("RGB")
56
+ st.image(image, caption="Uploaded Image", use_column_width=True)
57
+
58
+ # Process image with CLIP
59
+ inputs = processor(images=image, return_tensors="pt")
60
+ with torch.no_grad():
61
+ image_features = model.get_image_features(**inputs)
62
+
63
+ # Convert to NumPy & flatten
64
+ embeddings = image_features.detach().cpu().numpy().flatten().tolist()
65
+
66
+ # βœ… Search for Similar Images
67
+ st.subheader("πŸ”Ž Find Similar Images")
68
+ if st.button("Search Similar Images"):
69
+ search_results = index.query(vector=embeddings, top_k=5, include_metadata=True)
70
+
71
+ if search_results and "matches" in search_results:
72
+ for match in search_results["matches"]:
73
+ st.write(f"πŸ”Ή **Match Score:** {match['score']}")
74
+ st.image(match["metadata"]["url"], caption=f"Similar Image - {match['id']}")
75
+ else:
76
+ st.warning("No similar images found.")
77
+
78
+ # πŸ“Œ **Option 2: Text Search**
79
+ st.subheader("πŸ” Search Images with Text")
80
+ text_query = st.text_input("Enter a description (e.g., 'a cute cat' or 'a red car')")
81
+
82
+ if text_query and st.button("Search with Text"):
83
+ # Convert text to CLIP embedding
84
+ inputs = processor(text=text_query, return_tensors="pt")
85
+ with torch.no_grad():
86
+ text_features = model.get_text_features(**inputs)
87
+
88
+ # Convert to NumPy & flatten
89
+ text_embeddings = text_features.detach().cpu().numpy().flatten().tolist()
90
+
91
+ # βœ… Search in Pinecone
92
+ search_results = index.query(vector=text_embeddings, top_k=5, include_metadata=True)
93
+
94
+ # βœ… Display results
95
+ if search_results and "matches" in search_results:
96
+ for match in search_results["matches"]:
97
+ st.write(f"πŸ”Ή **Match Score:** {match['score']}")
98
+ st.image(match["metadata"]["url"], caption=f"Matched Image - {match['id']}")
99
+ else:
100
+ st.warning("No matching images found.")
101
+