Added tab for entire database
Browse files- api_client.py +10 -0
- app.py +67 -30
- components/display_structured_data.py +7 -7
- components/upload_section.py +0 -1
api_client.py
CHANGED
|
@@ -2,6 +2,7 @@ import requests
|
|
| 2 |
import os
|
| 3 |
import requests
|
| 4 |
import base64
|
|
|
|
| 5 |
|
| 6 |
#BACKEND_URL = "http://localhost:8000"
|
| 7 |
BACKEND_URL = os.getenv("BACKEND_URL", "https://dcorcoran-pokemon-card-image-processor-api.hf.space")
|
|
@@ -65,3 +66,12 @@ def health_check() -> bool:
|
|
| 65 |
except:
|
| 66 |
return False
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import os
|
| 3 |
import requests
|
| 4 |
import base64
|
| 5 |
+
import streamlit as st
|
| 6 |
|
| 7 |
#BACKEND_URL = "http://localhost:8000"
|
| 8 |
BACKEND_URL = os.getenv("BACKEND_URL", "https://dcorcoran-pokemon-card-image-processor-api.hf.space")
|
|
|
|
| 66 |
except:
|
| 67 |
return False
|
| 68 |
|
| 69 |
+
|
| 70 |
+
@st.cache_data(ttl=300)
|
| 71 |
+
def get_all_cards(limit: int = 50) -> list:
|
| 72 |
+
try:
|
| 73 |
+
response = requests.get(f"{BACKEND_URL}/cards", params={"limit": limit}, timeout=10)
|
| 74 |
+
response.raise_for_status()
|
| 75 |
+
return response.json()
|
| 76 |
+
except Exception as e:
|
| 77 |
+
raise
|
app.py
CHANGED
|
@@ -4,11 +4,13 @@ import io
|
|
| 4 |
import requests
|
| 5 |
import os
|
| 6 |
|
| 7 |
-
from api_client import predict, health_check
|
| 8 |
from components.upload_section import render_upload_section
|
| 9 |
from components.display_structured_data import render_card_data
|
| 10 |
from components.similarity_grid import render_similarity_grid
|
| 11 |
|
|
|
|
|
|
|
| 12 |
# --------------------------------
|
| 13 |
# ----- PAGE CONFIG --------------
|
| 14 |
# --------------------------------
|
|
@@ -23,51 +25,86 @@ st.set_page_config(
|
|
| 23 |
# ----- HEADER -------------------
|
| 24 |
# --------------------------------
|
| 25 |
|
| 26 |
-
st.title("
|
| 27 |
-
st.
|
| 28 |
-
|
| 29 |
|
| 30 |
|
| 31 |
# --------------------------------
|
| 32 |
# ----- BACKEND STATUS -----------
|
| 33 |
# --------------------------------
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
|
| 40 |
|
| 41 |
# --------------------------------
|
| 42 |
# ----- UPLOAD -------------------
|
| 43 |
# --------------------------------
|
| 44 |
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
#
|
| 48 |
-
|
| 49 |
-
# Reading Pokemon Card
|
| 50 |
-
image_bytes = uploaded_file.read()
|
| 51 |
-
image = Image.open(io.BytesIO(image_bytes))
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
|
|
|
| 58 |
|
| 59 |
-
with
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
result = predict(image_bytes, uploaded_file.name)
|
| 63 |
|
| 64 |
-
|
| 65 |
-
st.error(f"
|
| 66 |
-
|
| 67 |
-
render_card_data(result)
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
|
|
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
import requests
|
| 5 |
import os
|
| 6 |
|
| 7 |
+
from api_client import predict, health_check, get_all_cards
|
| 8 |
from components.upload_section import render_upload_section
|
| 9 |
from components.display_structured_data import render_card_data
|
| 10 |
from components.similarity_grid import render_similarity_grid
|
| 11 |
|
| 12 |
+
BACKEND_URL = os.getenv("BACKEND_URL", "https://dcorcoran-pokemon-card-image-processor-api.hf.space")
|
| 13 |
+
|
| 14 |
# --------------------------------
|
| 15 |
# ----- PAGE CONFIG --------------
|
| 16 |
# --------------------------------
|
|
|
|
| 25 |
# ----- HEADER -------------------
|
| 26 |
# --------------------------------
|
| 27 |
|
| 28 |
+
st.title("Pokemon Card Image Processor")
|
| 29 |
+
st.badge("Upload a Pokémon card image to extract its data and find similar cards.", color = "blue")
|
|
|
|
| 30 |
|
| 31 |
|
| 32 |
# --------------------------------
|
| 33 |
# ----- BACKEND STATUS -----------
|
| 34 |
# --------------------------------
|
| 35 |
|
| 36 |
+
if not health_check():
|
| 37 |
+
st.error("⚠️ Backend is not reachable. Make sure the FastAPI Space is running.")
|
| 38 |
+
st.stop()
|
|
|
|
| 39 |
|
| 40 |
|
| 41 |
# --------------------------------
|
| 42 |
# ----- UPLOAD -------------------
|
| 43 |
# --------------------------------
|
| 44 |
|
| 45 |
+
tab1, tab2 = st.tabs(["Upload Pokemon Card", "Card Database"])
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
with tab1:
|
| 49 |
+
uploaded_file = render_upload_section()
|
| 50 |
+
|
| 51 |
+
# Check if file was uploaded
|
| 52 |
+
if uploaded_file:
|
| 53 |
+
# Reading Pokemon Card
|
| 54 |
+
image_bytes = uploaded_file.read()
|
| 55 |
+
image = Image.open(io.BytesIO(image_bytes))
|
| 56 |
+
|
| 57 |
+
# Separate page into two columns
|
| 58 |
+
col1, col2 = st.columns([1, 2])
|
| 59 |
+
|
| 60 |
+
with col1:
|
| 61 |
+
st.image(image, width="content")
|
| 62 |
+
|
| 63 |
+
with col2:
|
| 64 |
+
with st.spinner("Analyzing card..."):
|
| 65 |
+
result = predict(image_bytes, uploaded_file.name)
|
| 66 |
+
|
| 67 |
+
# Check if there was an error in the predict endpoint
|
| 68 |
+
if "error_type" in result:
|
| 69 |
+
st.error(f"Error ({result['error_type']}): {result.get('backend_response') or result.get('exception')}")
|
| 70 |
+
|
| 71 |
+
# Display the rendered card data if there is no error
|
| 72 |
+
else:
|
| 73 |
+
render_card_data(result)
|
| 74 |
+
|
| 75 |
+
# Add a divider between the uploaded card and the similar cards
|
| 76 |
+
st.divider()
|
| 77 |
|
| 78 |
+
# Add header for similar cards
|
| 79 |
+
st.header("Similar Cards")
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
+
if "error" not in result:
|
| 82 |
+
render_similarity_grid(result.get("similar_cards", []))
|
| 83 |
|
| 84 |
+
with tab2:
|
| 85 |
+
st.header("Card Database")
|
| 86 |
+
st.subheader("Showing a sample of available cards in the database.")
|
| 87 |
|
| 88 |
+
with st.spinner("Loading cards..."):
|
| 89 |
+
try:
|
| 90 |
+
cards = get_all_cards()
|
|
|
|
| 91 |
|
| 92 |
+
except Exception as e:
|
| 93 |
+
st.error(f"Could not load card database: {e}")
|
| 94 |
+
cards = []
|
|
|
|
| 95 |
|
| 96 |
+
if cards:
|
| 97 |
+
cols_per_row = 5
|
| 98 |
+
rows = [cards[i:i+cols_per_row] for i in range(0, len(cards), cols_per_row)]
|
| 99 |
|
| 100 |
+
for row in rows:
|
| 101 |
+
cols = st.columns(cols_per_row)
|
| 102 |
+
for col, card in zip(cols, row):
|
| 103 |
+
with col:
|
| 104 |
+
if card.get("image_url"):
|
| 105 |
+
st.image(card["image_url"], width="content")
|
| 106 |
+
else:
|
| 107 |
+
st.markdown("🃏")
|
| 108 |
+
st.caption(card.get("name", "Unknown"))
|
| 109 |
+
else:
|
| 110 |
+
st.info("No cards to display.")
|
components/display_structured_data.py
CHANGED
|
@@ -2,34 +2,34 @@ import streamlit as st
|
|
| 2 |
|
| 3 |
def render_card_data(result: dict):
|
| 4 |
# Header for the uploaded card data
|
| 5 |
-
st.
|
| 6 |
|
| 7 |
# Create two columns
|
| 8 |
col1, col2 = st.columns(2)
|
| 9 |
|
| 10 |
# Column one filled with name and HP
|
| 11 |
with col1:
|
| 12 |
-
st.
|
| 13 |
-
st.
|
| 14 |
|
| 15 |
# Column two filled with Pokemon types
|
| 16 |
with col2:
|
| 17 |
types = result.get("types")
|
| 18 |
-
st.
|
| 19 |
|
| 20 |
# Display moves below above columns
|
| 21 |
moves = result.get("moves")
|
| 22 |
|
| 23 |
# Check if moves category exists
|
| 24 |
if moves:
|
| 25 |
-
st.
|
| 26 |
|
| 27 |
# Loop through all resulting moves
|
| 28 |
for move in moves:
|
| 29 |
-
# Write the move name,
|
| 30 |
with st.expander(f"{move.get('name')} — {move.get('damage', '?')} damage"):
|
| 31 |
st.write(move.get("text") or "No description available.")
|
| 32 |
-
|
| 33 |
# Placeholder if no moves detected
|
| 34 |
else:
|
| 35 |
st.metric("Moves", "Not detected")
|
|
|
|
| 2 |
|
| 3 |
def render_card_data(result: dict):
|
| 4 |
# Header for the uploaded card data
|
| 5 |
+
st.header("Extracted Card Data")
|
| 6 |
|
| 7 |
# Create two columns
|
| 8 |
col1, col2 = st.columns(2)
|
| 9 |
|
| 10 |
# Column one filled with name and HP
|
| 11 |
with col1:
|
| 12 |
+
st.subheader("Name", result.get("name") or "Not detected")
|
| 13 |
+
st.subheader("HP", result.get("hp") or "Not detected")
|
| 14 |
|
| 15 |
# Column two filled with Pokemon types
|
| 16 |
with col2:
|
| 17 |
types = result.get("types")
|
| 18 |
+
st.subheader("Types", ", ".join(types) if types else "Not detected")
|
| 19 |
|
| 20 |
# Display moves below above columns
|
| 21 |
moves = result.get("moves")
|
| 22 |
|
| 23 |
# Check if moves category exists
|
| 24 |
if moves:
|
| 25 |
+
st.subheader("**Moves**")
|
| 26 |
|
| 27 |
# Loop through all resulting moves
|
| 28 |
for move in moves:
|
| 29 |
+
# Write the move name, damage done and text
|
| 30 |
with st.expander(f"{move.get('name')} — {move.get('damage', '?')} damage"):
|
| 31 |
st.write(move.get("text") or "No description available.")
|
| 32 |
+
|
| 33 |
# Placeholder if no moves detected
|
| 34 |
else:
|
| 35 |
st.metric("Moves", "Not detected")
|
components/upload_section.py
CHANGED
|
@@ -2,7 +2,6 @@ import streamlit as st
|
|
| 2 |
|
| 3 |
def render_upload_section():
|
| 4 |
uploaded_file = st.file_uploader(
|
| 5 |
-
"Upload a Pokémon card image",
|
| 6 |
type=["png", "jpg", "jpeg"],
|
| 7 |
help="Supported formats: PNG, JPG, JPEG"
|
| 8 |
)
|
|
|
|
| 2 |
|
| 3 |
def render_upload_section():
|
| 4 |
uploaded_file = st.file_uploader(
|
|
|
|
| 5 |
type=["png", "jpg", "jpeg"],
|
| 6 |
help="Supported formats: PNG, JPG, JPEG"
|
| 7 |
)
|