|
import streamlit as st |
|
import requests |
|
from PIL import Image |
|
|
|
|
|
API_URL = "https://chittrarasu-image-search-engine-fastapi.hf.space/search" |
|
|
|
st.title("π Image Search Engine") |
|
|
|
|
|
option = st.radio("Search by:", ("Text", "Image")) |
|
|
|
|
|
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}") |
|
|
|
|
|
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}") |