logeswari commited on
Commit
263fba2
Β·
1 Parent(s): 9cc2025
Files changed (2) hide show
  1. app.py +59 -69
  2. requirements.txt +14 -3
app.py CHANGED
@@ -1,88 +1,78 @@
1
  import streamlit as st
2
  import requests
3
- from PIL import Image
4
- import io
5
- import json
6
 
7
- # API Base URL
8
- API_URL = "http://0.0.0.0:7860"
9
 
10
- # Authentication
11
- st.sidebar.title("πŸ”‘ Login")
12
- username = st.sidebar.text_input("Username", key="username_input")
13
- password = st.sidebar.text_input("Password", type="password", key="password_input")
14
- login_button = st.sidebar.button("Login")
15
 
16
- if "authenticated" not in st.session_state:
17
- st.session_state["authenticated"] = False
18
 
19
- if login_button:
20
- try:
21
- response = requests.post(
22
- f"{API_URL}/embed_text/", data={"text": "test"}, auth=(username, password)
23
- )
 
 
 
24
  if response.status_code == 200:
25
- st.session_state["authenticated"] = True
26
- st.session_state["username"] = username
27
- st.rerun()
28
  else:
29
- st.sidebar.error("❌ Invalid credentials")
30
- except Exception as e:
31
- st.sidebar.error("⚠️ Authentication error")
32
- st.sidebar.text(str(e))
33
 
34
- if not st.session_state["authenticated"]:
 
 
35
  st.stop()
36
 
37
- st.title("πŸ“Œ Image & Text Search with CLIP and Pinecone")
38
- option = st.radio("Choose input type:", ("Text", "Image"))
 
 
 
 
 
 
 
 
 
39
 
40
- if option == "Text":
41
- query_text = st.text_input("Enter a search term:")
42
- if st.button("Search"):
43
- if query_text:
44
- response = requests.post(
45
- f"{API_URL}/embed_text/", data={"text": query_text}, auth=(username, password)
46
- )
47
  if response.status_code == 200:
48
- embedding = response.json()["embedding"]
49
- search_response = requests.post(
50
- f"{API_URL}/search/", json={"query_embedding": embedding}, auth=(username, password)
51
- )
52
- results = search_response.json().get("results", [])
53
  if results:
 
54
  for match in results:
55
- url = match["metadata"].get("url", "No URL")
56
- st.image(url, caption=f"Score: {match['score']:.4f}", use_container_width=True)
57
  else:
58
- st.warning("No matching images found.")
59
  else:
60
- st.error("Error retrieving embedding.")
61
- else:
62
- st.error("Please enter a search term.")
63
 
64
- elif option == "Image":
65
- uploaded_file = st.file_uploader("Upload an image:", type=["jpg", "jpeg", "png"])
66
- if uploaded_file:
67
- image = Image.open(uploaded_file)
68
- st.image(image, caption="Uploaded Image", use_container_width=True)
69
- if st.button("Search"):
70
- img_byte_arr = io.BytesIO()
71
- image.save(img_byte_arr, format="PNG")
72
- img_byte_arr = img_byte_arr.getvalue()
73
- files = {"file": (uploaded_file.name, img_byte_arr, "image/png")}
74
- response = requests.post(f"{API_URL}/embed_image/", files=files, auth=(username, password))
75
- if response.status_code == 200:
76
- embedding = response.json()["embedding"]
77
- search_response = requests.post(
78
- f"{API_URL}/search/", json={"query_embedding": embedding}, auth=(username, password)
79
- )
80
- results = search_response.json().get("results", [])
81
- if results:
82
- for match in results:
83
- url = match["metadata"].get("url", "No URL")
84
- st.image(url, caption=f"Score: {match['score']:.4f}", use_container_width=True)
85
- else:
86
- st.warning("No similar images found.")
87
  else:
88
- st.error("Error retrieving embedding.")
 
 
 
 
 
1
  import streamlit as st
2
  import requests
3
+ import os
 
 
4
 
5
+ # Backend API URL
6
+ API_URL = "http://127.0.0.1:8000"
7
 
8
+ # Session state for authentication
9
+ if "access_token" not in st.session_state:
10
+ st.session_state.access_token = None
 
 
11
 
12
+ st.title("πŸ” Image Search System")
13
+ st.sidebar.subheader("Login")
14
 
15
+ # Login form
16
+ with st.sidebar.form(key="login_form"):
17
+ username = st.text_input("Username", value="admin")
18
+ password = st.text_input("Password", type="password", value="password123")
19
+ submit_button = st.form_submit_button("Login")
20
+
21
+ if submit_button:
22
+ response = requests.post(f"{API_URL}/token", data={"username": username, "password": password})
23
  if response.status_code == 200:
24
+ st.session_state.access_token = response.json().get("access_token")
25
+ st.sidebar.success("βœ… Logged in successfully!")
 
26
  else:
27
+ st.sidebar.error("❌ Incorrect username or password")
 
 
 
28
 
29
+ # Check authentication
30
+ if not st.session_state.access_token:
31
+ st.warning("πŸ”’ Please log in first.")
32
  st.stop()
33
 
34
+ # Tabs for text search and image search
35
+ tab1, tab2 = st.tabs(["πŸ”  Search by Text", "πŸ–ΌοΈ Search by Image"])
36
+
37
+ # Text-based search
38
+ with tab1:
39
+ st.subheader("πŸ”  Search by Text")
40
+ text_query = st.text_input("Enter a search query:")
41
+ if st.button("πŸ” Search"):
42
+ if text_query:
43
+ headers = {"Authorization": f"Bearer {st.session_state.access_token}"}
44
+ response = requests.get(f"{API_URL}/search/text/", params={"query": text_query}, headers=headers)
45
 
 
 
 
 
 
 
 
46
  if response.status_code == 200:
47
+ results = response.json().get("matches", [])
 
 
 
 
48
  if results:
49
+ st.success(f"βœ… Found {len(results)} similar images!")
50
  for match in results:
51
+ st.image(match["url"], caption=f"Match ID: {match['id']} - Score: {match['score']:.4f}")
 
52
  else:
53
+ st.warning("❌ No matches found.")
54
  else:
55
+ st.error("⚠️ Error searching for images.")
 
 
56
 
57
+ # Image-based search
58
+ with tab2:
59
+ st.subheader("πŸ–ΌοΈ Search by Image")
60
+ uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
61
+
62
+ if st.button("πŸ” Search by Image") and uploaded_file:
63
+ files = {"file": uploaded_file.getvalue()}
64
+ headers = {"Authorization": f"Bearer {st.session_state.access_token}"}
65
+ response = requests.post(f"{API_URL}/search/image/", files=files, headers=headers)
66
+
67
+ if response.status_code == 200:
68
+ results = response.json().get("matches", [])
69
+ if results:
70
+ st.success(f"βœ… Found {len(results)} similar images!")
71
+ for match in results:
72
+ st.image(match["url"], caption=f"Match ID: {match['id']} - Score: {match['score']:.4f}")
 
 
 
 
 
 
 
73
  else:
74
+ st.warning("❌ No matches found.")
75
+ else:
76
+ st.error("⚠️ Error searching for images.")
77
+
78
+ st.sidebar.button("Logout", on_click=lambda: st.session_state.pop("access_token", None))
requirements.txt CHANGED
@@ -1,4 +1,15 @@
1
- streamlit
 
 
 
 
2
  requests
3
- Pillow
4
- python-multipart
 
 
 
 
 
 
 
 
1
+ # FastAPI and dependencies
2
+ fastapi
3
+ uvicorn
4
+ python-dotenv
5
+ pillow
6
  requests
7
+ jose
8
+ python-jose
9
+ pyjwt
10
+ transformers
11
+ torch
12
+ pinecone
13
+
14
+ # Streamlit for frontend
15
+ streamlit