Spaces:
Sleeping
Sleeping
Integrated with new API
Browse files
app.py
CHANGED
|
@@ -1,116 +1,116 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from Clustering import ClusteringData
|
| 3 |
-
import numpy as np
|
| 4 |
-
from PIL import Image
|
| 5 |
-
import requests
|
| 6 |
-
import tempfile
|
| 7 |
-
import os
|
| 8 |
-
import logging
|
| 9 |
-
import json
|
| 10 |
-
|
| 11 |
-
logging.basicConfig(level=logging.INFO)
|
| 12 |
-
logger = logging.getLogger(__name__)
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
cd = ClusteringData()
|
| 16 |
-
cd.load_model_data()
|
| 17 |
-
logger.info("Clustering data loaded")
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
def search_images(text_query, uploaded_image, search_mode, top_k):
|
| 21 |
-
preview = None
|
| 22 |
-
results = []
|
| 23 |
-
|
| 24 |
-
if search_mode == "Text" and text_query.strip():
|
| 25 |
-
response = requests.get(
|
| 26 |
-
f"https://ashish-001-
|
| 27 |
-
if response.status_code == 200:
|
| 28 |
-
logger.info("Embedding returned successfully by text API")
|
| 29 |
-
data = json.loads(response.content)
|
| 30 |
-
embedding = data["embedding"]
|
| 31 |
-
results = cd.find_similar_records(embedding, k=top_k)
|
| 32 |
-
else:
|
| 33 |
-
logger.info(f"{response.status_code} returned by the text API")
|
| 34 |
-
results = []
|
| 35 |
-
results = [os.path.join("coco", "val2017", "val2017", fname)
|
| 36 |
-
for i, fname in enumerate(results)]
|
| 37 |
-
return None, results
|
| 38 |
-
|
| 39 |
-
elif search_mode == "Image":
|
| 40 |
-
if uploaded_image is not None:
|
| 41 |
-
preview = uploaded_image
|
| 42 |
-
tmp_path = uploaded_image
|
| 43 |
-
# with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmp_file:
|
| 44 |
-
# uploaded_image.save(tmp_file.name)
|
| 45 |
-
# tmp_path = tmp_file.name
|
| 46 |
-
else:
|
| 47 |
-
preview = 'Image.jpg'
|
| 48 |
-
tmp_path = 'Image.jpg'
|
| 49 |
-
url = "https://ashish-001-clip-
|
| 50 |
-
files = {"file": open(tmp_path, "rb")}
|
| 51 |
-
response = requests.post(url, files=files)
|
| 52 |
-
if response.status_code == 200:
|
| 53 |
-
embedding = np.array(response.json()['embedding']).squeeze()
|
| 54 |
-
logger.info("Embedding returned successfully by image API")
|
| 55 |
-
results = cd.find_similar_records(embedding, k=top_k)
|
| 56 |
-
else:
|
| 57 |
-
logger.info(
|
| 58 |
-
f"{response.status_code} returned by the image API")
|
| 59 |
-
results = []
|
| 60 |
-
results = [os.path.join("coco", "val2017", "val2017", fname)
|
| 61 |
-
for i, fname in enumerate(results)]
|
| 62 |
-
|
| 63 |
-
return preview, results
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
with gr.Blocks() as demo:
|
| 67 |
-
gr.Markdown("## Multimodal Image Search with CLIP")
|
| 68 |
-
gr.Markdown("Search images using **text** or **image upload**.")
|
| 69 |
-
|
| 70 |
-
with gr.Row():
|
| 71 |
-
with gr.Column(scale=1):
|
| 72 |
-
# Inputs
|
| 73 |
-
search_mode = gr.Radio(
|
| 74 |
-
["Text", "Image"], label="Search Mode", value="Text")
|
| 75 |
-
text_input = gr.Textbox(
|
| 76 |
-
label="Enter text query", placeholder="Type something...", visible=True, value='Empty street')
|
| 77 |
-
file_input = gr.Image(
|
| 78 |
-
type="filepath",
|
| 79 |
-
label="Upload image",
|
| 80 |
-
value="Image.jpg",
|
| 81 |
-
visible=False
|
| 82 |
-
)
|
| 83 |
-
top_k = gr.Slider(1, 20, value=6, step=1,
|
| 84 |
-
label="Number of results")
|
| 85 |
-
submit_btn = gr.Button("Search")
|
| 86 |
-
|
| 87 |
-
with gr.Column(scale=2):
|
| 88 |
-
preview_img = gr.Image(label="Uploaded / Default Image")
|
| 89 |
-
result_gallery = gr.Gallery(
|
| 90 |
-
label="Results", columns=3, height="auto")
|
| 91 |
-
|
| 92 |
-
def toggle_inputs(mode):
|
| 93 |
-
if mode == "Text":
|
| 94 |
-
return (
|
| 95 |
-
gr.update(visible=True),
|
| 96 |
-
gr.update(visible=False, value=None),
|
| 97 |
-
[],
|
| 98 |
-
None
|
| 99 |
-
)
|
| 100 |
-
else:
|
| 101 |
-
return (
|
| 102 |
-
gr.update(visible=False),
|
| 103 |
-
gr.update(visible=True, value=None),
|
| 104 |
-
[],
|
| 105 |
-
"Image.jpg"
|
| 106 |
-
)
|
| 107 |
-
|
| 108 |
-
search_mode.change(toggle_inputs, inputs=search_mode,
|
| 109 |
-
outputs=[text_input, file_input, result_gallery, preview_img])
|
| 110 |
-
|
| 111 |
-
submit_btn.click(fn=search_images,
|
| 112 |
-
inputs=[text_input,
|
| 113 |
-
file_input, search_mode, top_k],
|
| 114 |
-
outputs=[preview_img, result_gallery,])
|
| 115 |
-
|
| 116 |
-
demo.launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from Clustering import ClusteringData
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import requests
|
| 6 |
+
import tempfile
|
| 7 |
+
import os
|
| 8 |
+
import logging
|
| 9 |
+
import json
|
| 10 |
+
|
| 11 |
+
logging.basicConfig(level=logging.INFO)
|
| 12 |
+
logger = logging.getLogger(__name__)
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
cd = ClusteringData()
|
| 16 |
+
cd.load_model_data()
|
| 17 |
+
logger.info("Clustering data loaded")
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def search_images(text_query, uploaded_image, search_mode, top_k):
|
| 21 |
+
preview = None
|
| 22 |
+
results = []
|
| 23 |
+
|
| 24 |
+
if search_mode == "Text" and text_query.strip():
|
| 25 |
+
response = requests.get(
|
| 26 |
+
f"https://ashish-001-clip-api.hf.space/embedding?text={text_query.strip()}")
|
| 27 |
+
if response.status_code == 200:
|
| 28 |
+
logger.info("Embedding returned successfully by text API")
|
| 29 |
+
data = json.loads(response.content)
|
| 30 |
+
embedding = data["embedding"]
|
| 31 |
+
results = cd.find_similar_records(embedding, k=top_k)
|
| 32 |
+
else:
|
| 33 |
+
logger.info(f"{response.status_code} returned by the text API")
|
| 34 |
+
results = []
|
| 35 |
+
results = [os.path.join("coco", "val2017", "val2017", fname)
|
| 36 |
+
for i, fname in enumerate(results)]
|
| 37 |
+
return None, results
|
| 38 |
+
|
| 39 |
+
elif search_mode == "Image":
|
| 40 |
+
if uploaded_image is not None:
|
| 41 |
+
preview = uploaded_image
|
| 42 |
+
tmp_path = uploaded_image
|
| 43 |
+
# with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmp_file:
|
| 44 |
+
# uploaded_image.save(tmp_file.name)
|
| 45 |
+
# tmp_path = tmp_file.name
|
| 46 |
+
else:
|
| 47 |
+
preview = 'Image.jpg'
|
| 48 |
+
tmp_path = 'Image.jpg'
|
| 49 |
+
url = "https://ashish-001-clip-api.hf.space/clip/process"
|
| 50 |
+
files = {"file": open(tmp_path, "rb")}
|
| 51 |
+
response = requests.post(url, files=files)
|
| 52 |
+
if response.status_code == 200:
|
| 53 |
+
embedding = np.array(response.json()['embedding']).squeeze()
|
| 54 |
+
logger.info("Embedding returned successfully by image API")
|
| 55 |
+
results = cd.find_similar_records(embedding, k=top_k)
|
| 56 |
+
else:
|
| 57 |
+
logger.info(
|
| 58 |
+
f"{response.status_code} returned by the image API")
|
| 59 |
+
results = []
|
| 60 |
+
results = [os.path.join("coco", "val2017", "val2017", fname)
|
| 61 |
+
for i, fname in enumerate(results)]
|
| 62 |
+
|
| 63 |
+
return preview, results
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
with gr.Blocks() as demo:
|
| 67 |
+
gr.Markdown("## Multimodal Image Search with CLIP")
|
| 68 |
+
gr.Markdown("Search images using **text** or **image upload**.")
|
| 69 |
+
|
| 70 |
+
with gr.Row():
|
| 71 |
+
with gr.Column(scale=1):
|
| 72 |
+
# Inputs
|
| 73 |
+
search_mode = gr.Radio(
|
| 74 |
+
["Text", "Image"], label="Search Mode", value="Text")
|
| 75 |
+
text_input = gr.Textbox(
|
| 76 |
+
label="Enter text query", placeholder="Type something...", visible=True, value='Empty street')
|
| 77 |
+
file_input = gr.Image(
|
| 78 |
+
type="filepath",
|
| 79 |
+
label="Upload image",
|
| 80 |
+
value="Image.jpg",
|
| 81 |
+
visible=False
|
| 82 |
+
)
|
| 83 |
+
top_k = gr.Slider(1, 20, value=6, step=1,
|
| 84 |
+
label="Number of results")
|
| 85 |
+
submit_btn = gr.Button("Search")
|
| 86 |
+
|
| 87 |
+
with gr.Column(scale=2):
|
| 88 |
+
preview_img = gr.Image(label="Uploaded / Default Image")
|
| 89 |
+
result_gallery = gr.Gallery(
|
| 90 |
+
label="Results", columns=3, height="auto")
|
| 91 |
+
|
| 92 |
+
def toggle_inputs(mode):
|
| 93 |
+
if mode == "Text":
|
| 94 |
+
return (
|
| 95 |
+
gr.update(visible=True),
|
| 96 |
+
gr.update(visible=False, value=None),
|
| 97 |
+
[],
|
| 98 |
+
None
|
| 99 |
+
)
|
| 100 |
+
else:
|
| 101 |
+
return (
|
| 102 |
+
gr.update(visible=False),
|
| 103 |
+
gr.update(visible=True, value=None),
|
| 104 |
+
[],
|
| 105 |
+
"Image.jpg"
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
search_mode.change(toggle_inputs, inputs=search_mode,
|
| 109 |
+
outputs=[text_input, file_input, result_gallery, preview_img])
|
| 110 |
+
|
| 111 |
+
submit_btn.click(fn=search_images,
|
| 112 |
+
inputs=[text_input,
|
| 113 |
+
file_input, search_mode, top_k],
|
| 114 |
+
outputs=[preview_img, result_gallery,])
|
| 115 |
+
|
| 116 |
+
demo.launch()
|