msg
Browse files- app.py +59 -69
- requirements.txt +14 -3
app.py
CHANGED
|
@@ -1,88 +1,78 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
| 3 |
-
|
| 4 |
-
import io
|
| 5 |
-
import json
|
| 6 |
|
| 7 |
-
# API
|
| 8 |
-
API_URL = "http://
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
st.
|
| 12 |
-
|
| 13 |
-
password = st.sidebar.text_input("Password", type="password", key="password_input")
|
| 14 |
-
login_button = st.sidebar.button("Login")
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
| 24 |
if response.status_code == 200:
|
| 25 |
-
st.session_state
|
| 26 |
-
st.
|
| 27 |
-
st.rerun()
|
| 28 |
else:
|
| 29 |
-
st.sidebar.error("β
|
| 30 |
-
except Exception as e:
|
| 31 |
-
st.sidebar.error("β οΈ Authentication error")
|
| 32 |
-
st.sidebar.text(str(e))
|
| 33 |
|
| 34 |
-
|
|
|
|
|
|
|
| 35 |
st.stop()
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 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 |
-
|
| 56 |
-
st.image(url, caption=f"Score: {match['score']:.4f}", use_container_width=True)
|
| 57 |
else:
|
| 58 |
-
st.warning("No
|
| 59 |
else:
|
| 60 |
-
st.error("Error
|
| 61 |
-
else:
|
| 62 |
-
st.error("Please enter a search term.")
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 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.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
requests
|
| 3 |
-
|
| 4 |
-
python-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|