Update pages/4_Image_Search.py
Browse files54 unmodified lines555657585960616263646566676869707172739 unmodified lines838485868788899091929394959697983 unmodified lines1021031041051061071083 unmodified lines11211311411511611711854 unmodified linesimport streamlit as stfrom PIL import Imageimport osst.set_page_config(page_title="Image Search with CLIP + Custom Model", layout="wide")from image_search import (train_rag_on_images,query_similar_images,query_custom_model)# Check if ChromaDB is already traineddef is_trained():return os.path.exists("chroma_db/") and len(os.listdir("chroma_db/")) > 0st.title("π Image Search using CLIP + Custom Model")st.markdown("Upload an image, enter a prompt, or both to find similar images!")9 unmodified linessearch_button = st.button("π Search")if not is_trained():st.warning("Setting up search database... Please wait a moment.")train_rag_on_images("flickr8k/Flicker8k_Dataset")if search_button:if not prompt and not query_image:st.error("Please enter a prompt, upload an image, or both.")else:with st.spinner("Searching for similar images..."):clip_results, custom_results = query_similar_images(prompt=prompt, image=query_image, k=5)if clip_results:st.subheader("π CLIP (ChromaDB) Results")3 unmodified linesimg = Image.open(clip_results[i])col.image(img.resize((200, 200)), caption=os.path.basename(clip_results[i]))except:col.write("No image")if custom_results:st.subheader("π― Custom Model Results")3 unmodified linesimg = Image.open(custom_results[i])col.image(img.resize((200, 200)), caption=os.path.basename(custom_results[i]))except:col.write("No image")54 unmodified lines5556575859606162636465666768697071727374757677787980818283848586878889909192939 unmodified lines1031041051061071081091101111121131141151161171181191201213 unmodified lines1251261271281291301313 unmodified lines13513613713813914014114254 unmodified linesimport streamlit as stfrom PIL import Imageimport osimport pickleimport numpy as npst.set_page_config(page_title="Image Search with CLIP + Custom Model", layout="wide")from image_search import (train_rag_on_images,query_similar_images,query_custom_model,load_custom_model)from download import download_flickr8k_dataset# Ensure dataset is downloaded and unzipped before anything elseif not os.path.exists("flickr8k/Flicker8k_Dataset"):with st.spinner("π¦ Downloading and extracting Flickr8k dataset..."):download_flickr8k_dataset()# Check if ChromaDB is already traineddef is_trained():return os.path.exists("chroma_db/") and len(os.listdir("chroma_db/")) > 0# Load custom-trained model files@st.cache_resourcedef get_custom_model():return load_custom_model(model_dir="model_files",base_image_dir="flickr8k/Flicker8k_Dataset")custom_nn_model, custom_image_embeddings, custom_image_paths = get_custom_model()st.title("π Image Search using CLIP + Custom Model")st.markdown("Upload an image, enter a prompt, or both to find similar images!")9 unmodified linessearch_button = st.button("π Search")# Ensure ChromaDB is readyif not is_trained():st.warning("Setting up search database... Please wait a moment.")train_rag_on_images("flickr8k/Flicker8k_Dataset")# Perform searchif search_button:if not prompt and not query_image:st.error("Please enter a prompt, upload an image, or both.")else:with st.spinner("Searching for similar images..."):clip_results, _ = query_similar_images(prompt=prompt, image=query_image, k=5)custom_results = query_custom_model(prompt=prompt, image=query_image, k=5)if clip_results:st.subheader("π CLIP (ChromaDB) Results")3 unmodified linesimg = Image.open(clip_results[i])col.image(img.resize((200, 200)), caption=os.path.basename(clip_results[i]))except:col.write("β No image")if custom_results:st.subheader("π― Custom Model Results")3 unmodified linesimg = Image.open(custom_results[i])col.image(img.resize((200, 200)), caption=os.path.basename(custom_results[i]))except:col.write("β No image")
pages/4_Image_Search.py+28-4