File size: 2,432 Bytes
b6816c7
 
 
 
 
bd4a581
b6816c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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}")