img_frnt / app.py
tharu22's picture
new1
70230cf
import streamlit as st
import requests
from PIL import Image
import io
# βœ… Backend API URL (Replace with your actual API URL)
API_URL = "http://0.0.0.0:7860" # Change if running on a different server
# βœ… Set Streamlit Page Config
st.set_page_config(page_title="πŸ” CLIP Image & Text Search", layout="wide")
# βœ… Sidebar for Search Options
st.sidebar.title("πŸ” Search Options")
top_k = st.sidebar.slider("πŸ”’ Number of Similar Images", 1, 20, 10)
# πŸ“Œ **Option 1: Text-to-Image Search**
st.sidebar.subheader("πŸ“ Search by Text")
search_query = st.sidebar.text_input("Enter a description (e.g., 'a cute cat', 'a red car')")
text_search_btn = st.sidebar.button("πŸ” Search by Text")
# πŸ“Œ **Option 2: Image-to-Image Search**
st.sidebar.subheader("πŸ–ΌοΈ Search by Image")
uploaded_file = st.sidebar.file_uploader("Upload an image...", type=["jpg", "png", "jpeg"])
image_search_btn = st.sidebar.button("πŸ” Search by Image")
# βœ… Process Text Search
if search_query and text_search_btn:
st.subheader("πŸ”Ž Searching for similar images...")
with st.spinner("Fetching results..."):
response = requests.post(f"{API_URL}/search/text", json={"query": search_query})
if response.status_code == 200:
results = response.json().get("results", [])
if results:
cols = st.columns(3) # Arrange in 3 columns
for i, match in enumerate(results):
photo_id = match.get("id", "Unknown ID")
url = match.get("metadata", {}).get("url", None)
cosine_distance = 1 - match.get("score", 0) # Convert similarity score to cosine distance
with cols[i % 3]: # Alternate images in columns
st.write(f"πŸ“· **Photo ID**: {photo_id} | πŸ“ **Cosine Distance**: {cosine_distance:.4f}")
if url:
st.image(url, caption=f"Photo ID: {photo_id}", use_container_width=True)
else:
st.warning(f"⚠️ No image URL found for Photo ID: {photo_id}")
else:
st.warning("⚠️ No similar images found!")
else:
st.error("🚨 Error fetching search results.")
# βœ… Process Image Search
if uploaded_file and image_search_btn:
st.subheader("πŸ”Ž Searching for similar images...")
image = Image.open(uploaded_file).convert("RGB")
st.image(image, caption="Uploaded Image", use_container_width=True)
with st.spinner("Fetching results..."):
files = {"file": uploaded_file.getvalue()}
response = requests.post(f"{API_URL}/search/image", files=files)
if response.status_code == 200:
results = response.json().get("results", [])
if results:
cols = st.columns(3) # Arrange in 3 columns
for i, match in enumerate(results):
photo_id = match.get("id", "Unknown ID")
url = match.get("metadata", {}).get("url", None)
cosine_distance = 1 - match.get("score", 0) # Convert similarity score to cosine distance
with cols[i % 3]: # Alternate images in columns
st.write(f"πŸ“· **Photo ID**: {photo_id} | πŸ“ **Cosine Distance**: {cosine_distance:.4f}")
if url:
st.image(url, caption=f"Photo ID: {photo_id}", use_container_width=True)
else:
st.warning(f"⚠️ No image URL found for Photo ID: {photo_id}")
else:
st.warning("⚠️ No similar images found!")
else:
st.error("🚨 Error fetching search results.")