new
Browse files- app.py +84 -0
- requirements.txt +10 -0
app.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import io
|
| 5 |
+
|
| 6 |
+
# β
Backend API URL (Replace with your actual API URL)
|
| 7 |
+
API_URL = "http://127.0.0.1:8000" # Change if running on a different server
|
| 8 |
+
|
| 9 |
+
# β
Set Streamlit Page Config
|
| 10 |
+
st.set_page_config(page_title="π CLIP Image & Text Search", layout="wide")
|
| 11 |
+
|
| 12 |
+
# β
Sidebar for Search Options
|
| 13 |
+
st.sidebar.title("π Search Options")
|
| 14 |
+
top_k = st.sidebar.slider("π’ Number of Similar Images", 1, 20, 10)
|
| 15 |
+
|
| 16 |
+
# π **Option 1: Text-to-Image Search**
|
| 17 |
+
st.sidebar.subheader("π Search by Text")
|
| 18 |
+
search_query = st.sidebar.text_input("Enter a description (e.g., 'a cute cat', 'a red car')")
|
| 19 |
+
text_search_btn = st.sidebar.button("π Search by Text")
|
| 20 |
+
|
| 21 |
+
# π **Option 2: Image-to-Image Search**
|
| 22 |
+
st.sidebar.subheader("πΌοΈ Search by Image")
|
| 23 |
+
uploaded_file = st.sidebar.file_uploader("Upload an image...", type=["jpg", "png", "jpeg"])
|
| 24 |
+
image_search_btn = st.sidebar.button("π Search by Image")
|
| 25 |
+
|
| 26 |
+
# β
Process Text Search
|
| 27 |
+
if search_query and text_search_btn:
|
| 28 |
+
st.subheader("π Searching for similar images...")
|
| 29 |
+
|
| 30 |
+
with st.spinner("Fetching results..."):
|
| 31 |
+
response = requests.post(f"{API_URL}/search/text", json={"query": search_query})
|
| 32 |
+
|
| 33 |
+
if response.status_code == 200:
|
| 34 |
+
results = response.json().get("results", [])
|
| 35 |
+
if results:
|
| 36 |
+
cols = st.columns(3) # Arrange in 3 columns
|
| 37 |
+
for i, match in enumerate(results):
|
| 38 |
+
photo_id = match.get("id", "Unknown ID")
|
| 39 |
+
url = match.get("metadata", {}).get("url", None)
|
| 40 |
+
cosine_distance = 1 - match.get("score", 0) # Convert similarity score to cosine distance
|
| 41 |
+
|
| 42 |
+
with cols[i % 3]: # Alternate images in columns
|
| 43 |
+
st.write(f"π· **Photo ID**: {photo_id} | π **Cosine Distance**: {cosine_distance:.4f}")
|
| 44 |
+
if url:
|
| 45 |
+
st.image(url, caption=f"Photo ID: {photo_id}", use_container_width=True)
|
| 46 |
+
else:
|
| 47 |
+
st.warning(f"β οΈ No image URL found for Photo ID: {photo_id}")
|
| 48 |
+
else:
|
| 49 |
+
st.warning("β οΈ No similar images found!")
|
| 50 |
+
else:
|
| 51 |
+
st.error("π¨ Error fetching search results.")
|
| 52 |
+
|
| 53 |
+
# β
Process Image Search
|
| 54 |
+
if uploaded_file and image_search_btn:
|
| 55 |
+
st.subheader("π Searching for similar images...")
|
| 56 |
+
|
| 57 |
+
image = Image.open(uploaded_file).convert("RGB")
|
| 58 |
+
st.image(image, caption="Uploaded Image", use_container_width=True)
|
| 59 |
+
|
| 60 |
+
with st.spinner("Fetching results..."):
|
| 61 |
+
files = {"file": uploaded_file.getvalue()}
|
| 62 |
+
response = requests.post(f"{API_URL}/search/image", files=files)
|
| 63 |
+
|
| 64 |
+
if response.status_code == 200:
|
| 65 |
+
results = response.json().get("results", [])
|
| 66 |
+
if results:
|
| 67 |
+
cols = st.columns(3) # Arrange in 3 columns
|
| 68 |
+
for i, match in enumerate(results):
|
| 69 |
+
photo_id = match.get("id", "Unknown ID")
|
| 70 |
+
url = match.get("metadata", {}).get("url", None)
|
| 71 |
+
cosine_distance = 1 - match.get("score", 0) # Convert similarity score to cosine distance
|
| 72 |
+
|
| 73 |
+
with cols[i % 3]: # Alternate images in columns
|
| 74 |
+
st.write(f"π· **Photo ID**: {photo_id} | π **Cosine Distance**: {cosine_distance:.4f}")
|
| 75 |
+
if url:
|
| 76 |
+
st.image(url, caption=f"Photo ID: {photo_id}", use_container_width=True)
|
| 77 |
+
else:
|
| 78 |
+
st.warning(f"β οΈ No image URL found for Photo ID: {photo_id}")
|
| 79 |
+
else:
|
| 80 |
+
st.warning("β οΈ No similar images found!")
|
| 81 |
+
else:
|
| 82 |
+
st.error("π¨ Error fetching search results.")
|
| 83 |
+
|
| 84 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
streamlit
|
| 4 |
+
requests
|
| 5 |
+
pillow
|
| 6 |
+
torch
|
| 7 |
+
transformers
|
| 8 |
+
numpy
|
| 9 |
+
pinecone
|
| 10 |
+
python-multipart
|