| import streamlit as st |
| import requests |
| from PIL import Image |
| import io |
|
|
| |
| API_URL = "http://0.0.0.0:7860" |
|
|
| |
| st.set_page_config(page_title="π CLIP Image & Text Search", layout="wide") |
|
|
| |
| st.sidebar.title("π Search Options") |
| top_k = st.sidebar.slider("π’ Number of Similar Images", 1, 20, 10) |
|
|
| |
| 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") |
|
|
| |
| 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") |
|
|
| |
| 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) |
| 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) |
|
|
| with cols[i % 3]: |
| 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.") |
|
|
| |
| 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) |
| 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) |
|
|
| with cols[i % 3]: |
| 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.") |
|
|
|
|
|
|