| import streamlit as st |
| import requests |
| import os |
|
|
| |
| API_URL = "https://logeswari-new-1.hf.space" |
|
|
| |
| if "access_token" not in st.session_state: |
| st.session_state.access_token = None |
|
|
| st.title("π Image Search System") |
| st.sidebar.subheader("Login") |
|
|
| |
| 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") |
|
|
| |
| if not st.session_state.access_token: |
| st.warning("π Please log in first.") |
| st.stop() |
|
|
| |
| tab1, tab2 = st.tabs(["π Search by Text", "πΌοΈ Search by Image"]) |
|
|
| |
| 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.") |
|
|
| |
| 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)) |
|
|