import streamlit as st import requests from PIL import Image # FastAPI Backend URL API_URL = "https://chittrarasu-image-search-engine-fastapi.hf.space/search" st.title("🔍 Image Search Engine") # Choose Search Type (Text or Image) option = st.radio("Search by:", ("Text", "Image")) # 📝 Text-based Search if option == "Text": query = st.text_input("Enter search query:") if st.button("Search") and query: try: response = requests.get(f"{API_URL}/text", params={"query": query}) response.raise_for_status() results = response.json() if "matches" in results and results["matches"]: st.subheader("🔎 Similar Images") for match in results["matches"]: image_url = match.get("url", "") if image_url: st.image(image_url, width=300) else: st.warning("⚠️ No image URL found.") else: st.warning("⚠️ No similar images found.") except requests.exceptions.RequestException as e: st.error(f"❌ Error: {e}") # 📷 Image-based Search elif option == "Image": uploaded_file = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"]) if uploaded_file: image = Image.open(uploaded_file) st.image(image, caption="📷 Uploaded Image", width=300) if st.button("Search"): try: files = {"file": (uploaded_file.name, uploaded_file.getvalue(), uploaded_file.type)} response = requests.post(f"{API_URL}/image", files=files) response.raise_for_status() results = response.json() if "matches" in results and results["matches"]: st.subheader("🔎 Similar Images") for match in results["matches"]: image_url = match.get("url", "") if image_url: st.image(image_url, width=300) else: st.warning("⚠️ No image URL found.") else: st.warning("⚠️ No similar images found.") except requests.exceptions.RequestException as e: st.error(f"❌ Error: {e}")