File size: 3,048 Bytes
a5a457e 263fba2 a5a457e 263fba2 efd2df1 db6af3e 263fba2 a5a457e 263fba2 a5a457e 263fba2 2900266 263fba2 ca4912d 263fba2 ca4912d 263fba2 ca4912d 263fba2 ca4912d 263fba2 a5a457e c253ce9 263fba2 ca4912d 263fba2 ca4912d 263fba2 ca4912d 263fba2 a5a457e 263fba2 a5a457e 263fba2 a5a457e 263fba2 |
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 67 68 69 70 71 72 73 74 75 76 77 78 79 |
import streamlit as st
import requests
import os
# Backend API URL
API_URL = "https://logeswari-new-1.hf.space"
# Session state for authentication
if "access_token" not in st.session_state:
st.session_state.access_token = None
st.title("π Image Search System")
st.sidebar.subheader("Login")
# Login form
with st.sidebar.form(key="login_form"):
username = st.text_input("Username")
password = st.text_input("Password", type="password")
submit_button = st.form_submit_button("Login")
if submit_button:
response = requests.post(f"{API_URL}/token", data={"username": username, "password": password})
if response.status_code == 200:
st.session_state.access_token = response.json().get("access_token")
st.sidebar.success("β
Logged in successfully!")
else:
st.sidebar.error("β Incorrect username or password")
# Check authentication
if not st.session_state.access_token:
st.warning("π Please log in first.")
st.stop()
# Tabs for text search and image search
tab1, tab2 = st.tabs(["π Search by Text", "πΌοΈ Search by Image"])
# Text-based search
with tab1:
st.subheader("π Search by Text")
text_query = st.text_input("Enter a search query:")
if st.button("π Search"):
if text_query:
headers = {"Authorization": f"Bearer {st.session_state.access_token}"}
response = requests.get(f"{API_URL}/search/text/", params={"query": text_query}, headers=headers)
if response.status_code == 200:
results = response.json().get("matches", [])
if results:
st.success(f"β
Found {len(results)} similar images!")
for match in results:
st.image(match["url"], caption=f"Match ID: {match['id']} - Score: {match['score']:.4f}")
else:
st.warning("β No matches found.")
else:
st.error("β οΈ Error searching for images.")
# Image-based search
with tab2:
st.subheader("πΌοΈ Search by Image")
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
if st.button("π Search by Image") and uploaded_file:
files = {"file": uploaded_file.getvalue()}
headers = {"Authorization": f"Bearer {st.session_state.access_token}"}
response = requests.post(f"{API_URL}/search/image/", files=files, headers=headers)
if response.status_code == 200:
results = response.json().get("matches", [])
if results:
st.success(f"β
Found {len(results)} similar images!")
for match in results:
st.image(match["url"], caption=f"Match ID: {match['id']} - Score: {match['score']:.4f}")
else:
st.warning("β No matches found.")
else:
st.error("β οΈ Error searching for images.")
st.sidebar.button("Logout", on_click=lambda: st.session_state.pop("access_token", None))
|