Spaces:
Sleeping
Sleeping
Upload 5009 files
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- .gitattributes +0 -0
- app.py +145 -0
- data/2b9adfc7-6437-464a-9a6e-68d5e8f953a8/data_level0.bin +3 -0
- data/2b9adfc7-6437-464a-9a6e-68d5e8f953a8/header.bin +3 -0
- data/2b9adfc7-6437-464a-9a6e-68d5e8f953a8/index_metadata.pickle +3 -0
- data/2b9adfc7-6437-464a-9a6e-68d5e8f953a8/length.bin +3 -0
- data/2b9adfc7-6437-464a-9a6e-68d5e8f953a8/link_lists.bin +3 -0
- data/chroma.sqlite3 +3 -0
- images/000000000139.jpg +3 -0
- images/000000000285.jpg +3 -0
- images/000000000632.jpg +3 -0
- images/000000000724.jpg +3 -0
- images/000000000776.jpg +3 -0
- images/000000000785.jpg +3 -0
- images/000000000802.jpg +0 -0
- images/000000000872.jpg +3 -0
- images/000000000885.jpg +3 -0
- images/000000001000.jpg +3 -0
- images/000000001268.jpg +3 -0
- images/000000001296.jpg +3 -0
- images/000000001353.jpg +0 -0
- images/000000001425.jpg +3 -0
- images/000000001490.jpg +3 -0
- images/000000001503.jpg +0 -0
- images/000000001532.jpg +3 -0
- images/000000001584.jpg +3 -0
- images/000000001675.jpg +0 -0
- images/000000001761.jpg +3 -0
- images/000000001818.jpg +3 -0
- images/000000001993.jpg +3 -0
- images/000000002006.jpg +3 -0
- images/000000002149.jpg +0 -0
- images/000000002153.jpg +3 -0
- images/000000002157.jpg +3 -0
- images/000000002261.jpg +3 -0
- images/000000002299.jpg +3 -0
- images/000000002431.jpg +3 -0
- images/000000002473.jpg +0 -0
- images/000000002532.jpg +3 -0
- images/000000002587.jpg +3 -0
- images/000000002592.jpg +3 -0
- images/000000002685.jpg +3 -0
- images/000000002923.jpg +0 -0
- images/000000003156.jpg +3 -0
- images/000000003255.jpg +3 -0
- images/000000003501.jpg +3 -0
- images/000000003553.jpg +3 -0
- images/000000003661.jpg +3 -0
- images/000000003845.jpg +3 -0
- images/000000003934.jpg +3 -0
.gitattributes
CHANGED
|
The diff for this file is too large to render.
See raw diff
|
|
|
app.py
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import chromadb
|
| 2 |
+
from chromadb.config import Settings
|
| 3 |
+
import torchvision.models as models
|
| 4 |
+
import torch
|
| 5 |
+
from torchvision import transforms
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import logging
|
| 8 |
+
import streamlit as st
|
| 9 |
+
import requests
|
| 10 |
+
import json
|
| 11 |
+
import uuid
|
| 12 |
+
import os
|
| 13 |
+
|
| 14 |
+
try:
|
| 15 |
+
|
| 16 |
+
logging.basicConfig(level=logging.INFO)
|
| 17 |
+
logger = logging.getLogger(__name__)
|
| 18 |
+
|
| 19 |
+
@st.cache_resource
|
| 20 |
+
def load_mobilenet_model():
|
| 21 |
+
device = 'cpu'
|
| 22 |
+
model = models.mobilenet_v3_small(pretrained=False)
|
| 23 |
+
model.classifier[3] = torch.nn.Linear(1024, 768)
|
| 24 |
+
model.load_state_dict(torch.load(
|
| 25 |
+
'mobilenet_v3_small_distilled_new_state_dict.pth', map_location=device))
|
| 26 |
+
model.eval().to(device)
|
| 27 |
+
return model
|
| 28 |
+
|
| 29 |
+
@st.cache_resource
|
| 30 |
+
def load_chromadb():
|
| 31 |
+
chroma_client = chromadb.PersistentClient(
|
| 32 |
+
path='data', settings=Settings(anonymized_telemetry=False))
|
| 33 |
+
collection = chroma_client.get_collection(name='images')
|
| 34 |
+
return collection
|
| 35 |
+
|
| 36 |
+
model = load_mobilenet_model()
|
| 37 |
+
logger.info("MobileNet loaded")
|
| 38 |
+
collection = load_chromadb()
|
| 39 |
+
logger.info("ChromaDB loaded")
|
| 40 |
+
logger.info(
|
| 41 |
+
f"Connected to ChromaDB collection images with {collection.count()} items")
|
| 42 |
+
|
| 43 |
+
preprocess = transforms.Compose([
|
| 44 |
+
transforms.Resize((224, 224)),
|
| 45 |
+
transforms.ToTensor(),
|
| 46 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[
|
| 47 |
+
0.229, 0.224, 0.225])
|
| 48 |
+
])
|
| 49 |
+
|
| 50 |
+
def get_image_embedding(image):
|
| 51 |
+
if isinstance(image, str):
|
| 52 |
+
img = Image.open(image).convert('RGB')
|
| 53 |
+
else:
|
| 54 |
+
img = Image.open(image).convert('RGB')
|
| 55 |
+
input_tensor = preprocess(img).unsqueeze(0).to('cpu')
|
| 56 |
+
with torch.no_grad():
|
| 57 |
+
student_embedding = model(input_tensor)
|
| 58 |
+
|
| 59 |
+
return torch.nn.functional.normalize(student_embedding, p=2, dim=1).squeeze(0).tolist()
|
| 60 |
+
|
| 61 |
+
def save_image(image_file):
|
| 62 |
+
unique_filename = f"{image_file.name}"
|
| 63 |
+
save_path = os.path.join('images', unique_filename)
|
| 64 |
+
with open(save_path, "wb") as f:
|
| 65 |
+
f.write(image_file.getbuffer())
|
| 66 |
+
return save_path
|
| 67 |
+
|
| 68 |
+
def resize_image(image_path, size=(224, 224)):
|
| 69 |
+
if isinstance(image_path, str):
|
| 70 |
+
img = Image.open(image_path).convert("RGB")
|
| 71 |
+
else:
|
| 72 |
+
# Handle uploaded file
|
| 73 |
+
img = Image.open(image_path).convert("RGB")
|
| 74 |
+
img_resized = img.resize(size, Image.LANCZOS) # High-quality resizing
|
| 75 |
+
return img_resized
|
| 76 |
+
|
| 77 |
+
st.sidebar.header("Upload Images")
|
| 78 |
+
image_files = st.sidebar.file_uploader(
|
| 79 |
+
"Upload images", type=["png", "jpg", "jpeg"], accept_multiple_files=True)
|
| 80 |
+
num_images = st.sidebar.slider(
|
| 81 |
+
"Number of results to return", min_value=1, max_value=10, value=3)
|
| 82 |
+
|
| 83 |
+
if image_files:
|
| 84 |
+
st.sidebar.subheader(
|
| 85 |
+
"Add Images to collection")
|
| 86 |
+
if st.sidebar.button("Add uploaded images"):
|
| 87 |
+
for idx, image_file in enumerate(image_files):
|
| 88 |
+
image_embedding = get_image_embedding(image_file)
|
| 89 |
+
saved_path = save_image(image_file)
|
| 90 |
+
unique_id = str(uuid.uuid4())
|
| 91 |
+
metadata = {
|
| 92 |
+
'path': f'images/{image_file.name}', "type": "photo"
|
| 93 |
+
}
|
| 94 |
+
collection.add(
|
| 95 |
+
embeddings=[image_embedding],
|
| 96 |
+
ids=[unique_id],
|
| 97 |
+
metadatas=[metadata]
|
| 98 |
+
)
|
| 99 |
+
st.sidebar.success(
|
| 100 |
+
f"Image {image_file.name} added to the collection")
|
| 101 |
+
|
| 102 |
+
st.title('Image Search Using Text')
|
| 103 |
+
st.write(
|
| 104 |
+
"The images stored in this database are sourced from the [COCO 2017 Validation Dataset](https://cocodataset.org/#download).")
|
| 105 |
+
st.write('Enter the text to search for images with matching description')
|
| 106 |
+
text_input = st.text_input("Description", "Playground")
|
| 107 |
+
if st.button("Search"):
|
| 108 |
+
if text_input.strip():
|
| 109 |
+
params = {'text': text_input}
|
| 110 |
+
response = requests.get(
|
| 111 |
+
'https://ashish-001-text-embedding-api.hf.space/embedding', params=params)
|
| 112 |
+
if response.status_code == 200:
|
| 113 |
+
logger.info("Embedding returned by API successfully")
|
| 114 |
+
data = json.loads(response.content)
|
| 115 |
+
embedding = data['embedding']
|
| 116 |
+
results = collection.query(
|
| 117 |
+
query_embeddings=[embedding],
|
| 118 |
+
n_results=num_images
|
| 119 |
+
)
|
| 120 |
+
images = [results['metadatas'][0][i]['path']
|
| 121 |
+
for i in range(len(results['metadatas'][0]))]
|
| 122 |
+
distances = [results['distances'][0][i]
|
| 123 |
+
for i in range(len(results['metadatas'][0]))]
|
| 124 |
+
if images:
|
| 125 |
+
cols_per_row = 3
|
| 126 |
+
rows = (len(images)+cols_per_row-1)//cols_per_row
|
| 127 |
+
for row in range(rows):
|
| 128 |
+
cols = st.columns(cols_per_row)
|
| 129 |
+
for col_idx, col in enumerate(cols):
|
| 130 |
+
img_idx = row*cols_per_row+col_idx
|
| 131 |
+
if img_idx < len(images):
|
| 132 |
+
resized_img = resize_image(
|
| 133 |
+
images[img_idx], size=(224, 224))
|
| 134 |
+
col.image(resized_img,
|
| 135 |
+
caption=f"Image {img_idx+1}\ndistance {distances[img_idx]}", use_container_width=True)
|
| 136 |
+
else:
|
| 137 |
+
st.write("No image found")
|
| 138 |
+
else:
|
| 139 |
+
st.write("Please try again later")
|
| 140 |
+
logger.info(f"status code {response.status_code} returned")
|
| 141 |
+
else:
|
| 142 |
+
st.write("Please enter text in the text area")
|
| 143 |
+
|
| 144 |
+
except Exception as e:
|
| 145 |
+
logger.info(f"Exception occured: {e}")
|
data/2b9adfc7-6437-464a-9a6e-68d5e8f953a8/data_level0.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:b2615c0e69a7f28c18f6e0617b4bcea2e66e69d562e34b1b87636a78dcf368b7
|
| 3 |
+
size 16060000
|
data/2b9adfc7-6437-464a-9a6e-68d5e8f953a8/header.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:aa80aba5314c8ce52cf6f6e7f1d45e6b8021453f9c7f7b292ee6a5dd1a776372
|
| 3 |
+
size 100
|
data/2b9adfc7-6437-464a-9a6e-68d5e8f953a8/index_metadata.pickle
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:dc389628547534447cc61f658675b83226ca3ac7d507fc74e3700d004400bfab
|
| 3 |
+
size 187994
|
data/2b9adfc7-6437-464a-9a6e-68d5e8f953a8/length.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:e3e5a4c01928bba72fd04ee278bf472b3293b60b979c0104fdc71105a9cbc587
|
| 3 |
+
size 20000
|
data/2b9adfc7-6437-464a-9a6e-68d5e8f953a8/link_lists.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:1de5334c70f1165524f9ccc0309d5c67d6b37783b27179109edd39233e529914
|
| 3 |
+
size 42780
|
data/chroma.sqlite3
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3cc86101247d3b61131d449cb045e687c8b808f69cc94ee0a48c9da0d52df6bc
|
| 3 |
+
size 22306816
|
images/000000000139.jpg
ADDED
|
Git LFS Details
|
images/000000000285.jpg
ADDED
|
Git LFS Details
|
images/000000000632.jpg
ADDED
|
Git LFS Details
|
images/000000000724.jpg
ADDED
|
Git LFS Details
|
images/000000000776.jpg
ADDED
|
Git LFS Details
|
images/000000000785.jpg
ADDED
|
Git LFS Details
|
images/000000000802.jpg
ADDED
|
images/000000000872.jpg
ADDED
|
Git LFS Details
|
images/000000000885.jpg
ADDED
|
Git LFS Details
|
images/000000001000.jpg
ADDED
|
Git LFS Details
|
images/000000001268.jpg
ADDED
|
Git LFS Details
|
images/000000001296.jpg
ADDED
|
Git LFS Details
|
images/000000001353.jpg
ADDED
|
images/000000001425.jpg
ADDED
|
Git LFS Details
|
images/000000001490.jpg
ADDED
|
Git LFS Details
|
images/000000001503.jpg
ADDED
|
images/000000001532.jpg
ADDED
|
Git LFS Details
|
images/000000001584.jpg
ADDED
|
Git LFS Details
|
images/000000001675.jpg
ADDED
|
images/000000001761.jpg
ADDED
|
Git LFS Details
|
images/000000001818.jpg
ADDED
|
Git LFS Details
|
images/000000001993.jpg
ADDED
|
Git LFS Details
|
images/000000002006.jpg
ADDED
|
Git LFS Details
|
images/000000002149.jpg
ADDED
|
images/000000002153.jpg
ADDED
|
Git LFS Details
|
images/000000002157.jpg
ADDED
|
Git LFS Details
|
images/000000002261.jpg
ADDED
|
Git LFS Details
|
images/000000002299.jpg
ADDED
|
Git LFS Details
|
images/000000002431.jpg
ADDED
|
Git LFS Details
|
images/000000002473.jpg
ADDED
|
images/000000002532.jpg
ADDED
|
Git LFS Details
|
images/000000002587.jpg
ADDED
|
Git LFS Details
|
images/000000002592.jpg
ADDED
|
Git LFS Details
|
images/000000002685.jpg
ADDED
|
Git LFS Details
|
images/000000002923.jpg
ADDED
|
images/000000003156.jpg
ADDED
|
Git LFS Details
|
images/000000003255.jpg
ADDED
|
Git LFS Details
|
images/000000003501.jpg
ADDED
|
Git LFS Details
|
images/000000003553.jpg
ADDED
|
Git LFS Details
|
images/000000003661.jpg
ADDED
|
Git LFS Details
|
images/000000003845.jpg
ADDED
|
Git LFS Details
|
images/000000003934.jpg
ADDED
|
Git LFS Details
|