Chittrarasu commited on
Commit
b6816c7
Β·
0 Parent(s):
Files changed (4) hide show
  1. README.md +10 -0
  2. app.py +66 -0
  3. requirements.txt +3 -0
  4. utils/api.py +21 -0
README.md ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Image Search Engine Streamlit
3
+ emoji: πŸ“ˆ
4
+ colorFrom: red
5
+ colorTo: indigo
6
+ sdk: streamlit
7
+ sdk_version: 1.42.2
8
+ app_file: app.py
9
+ pinned: false
10
+ ---
app.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+ from PIL import Image
4
+
5
+ # FastAPI Backend URL
6
+ API_URL = "http://127.0.0.1:8000/search"
7
+
8
+ st.title("πŸ” Image Search Engine")
9
+
10
+ # Choose Search Type (Text or Image)
11
+ option = st.radio("Search by:", ("Text", "Image"))
12
+
13
+ # πŸ“ Text-based Search
14
+ if option == "Text":
15
+ query = st.text_input("Enter search query:")
16
+
17
+ if st.button("Search") and query:
18
+ try:
19
+ response = requests.get(f"{API_URL}/text", params={"query": query})
20
+ response.raise_for_status()
21
+
22
+ results = response.json()
23
+
24
+ if "matches" in results and results["matches"]:
25
+ st.subheader("πŸ”Ž Similar Images")
26
+ for match in results["matches"]:
27
+ image_url = match.get("url", "")
28
+ if image_url:
29
+ st.image(image_url, width=300)
30
+ else:
31
+ st.warning("⚠️ No image URL found.")
32
+ else:
33
+ st.warning("⚠️ No similar images found.")
34
+
35
+ except requests.exceptions.RequestException as e:
36
+ st.error(f"❌ Error: {e}")
37
+
38
+ # πŸ“· Image-based Search
39
+ elif option == "Image":
40
+ uploaded_file = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])
41
+
42
+ if uploaded_file:
43
+ image = Image.open(uploaded_file)
44
+ st.image(image, caption="πŸ“· Uploaded Image", width=300)
45
+
46
+ if st.button("Search"):
47
+ try:
48
+ files = {"file": (uploaded_file.name, uploaded_file.getvalue(), uploaded_file.type)}
49
+ response = requests.post(f"{API_URL}/image", files=files)
50
+ response.raise_for_status()
51
+
52
+ results = response.json()
53
+
54
+ if "matches" in results and results["matches"]:
55
+ st.subheader("πŸ”Ž Similar Images")
56
+ for match in results["matches"]:
57
+ image_url = match.get("url", "")
58
+ if image_url:
59
+ st.image(image_url, width=300)
60
+ else:
61
+ st.warning("⚠️ No image URL found.")
62
+ else:
63
+ st.warning("⚠️ No similar images found.")
64
+
65
+ except requests.exceptions.RequestException as e:
66
+ st.error(f"❌ Error: {e}")
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ streamlit>=1.20.0
2
+ requests>=2.28.0
3
+ pillow>=9.0.0
utils/api.py ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ from typing import Dict, Any
3
+
4
+ API_URL = "http://127.0.0.1:8000/search"
5
+
6
+ def search_by_text(query: str) -> Dict[str, Any]:
7
+ try:
8
+ response = requests.get(f"{API_URL}/text", params={"query": query})
9
+ response.raise_for_status()
10
+ return response.json()
11
+ except requests.RequestException as e:
12
+ return {"error": str(e)}
13
+
14
+ def search_by_image(image_file) -> Dict[str, Any]:
15
+ try:
16
+ files = {"file": (image_file.name, image_file.getvalue(), image_file.type)}
17
+ response = requests.post(f"{API_URL}/image", files=files)
18
+ response.raise_for_status()
19
+ return response.json()
20
+ except requests.RequestException as e:
21
+ return {"error": str(e)}