Initial Commit
Browse files- .dockerignore +4 -0
- .gitattributes +0 -35
- Dockerfile +7 -13
- api_client.py +29 -0
- app.py +70 -0
- components/display_structured_data.py +24 -0
- components/similarity_grid.py +29 -0
- components/upload_section.py +9 -0
- requirements.txt +4 -3
- src/streamlit_app.py +0 -40
.dockerignore
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
__pycache__
|
| 2 |
+
*.pyc
|
| 3 |
+
.env
|
| 4 |
+
.git
|
.gitattributes
DELETED
|
@@ -1,35 +0,0 @@
|
|
| 1 |
-
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
-
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
-
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
-
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
-
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
-
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
-
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
-
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
-
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
-
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
-
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
-
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
-
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
-
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
-
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
-
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
-
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
-
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
-
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
-
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
-
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
-
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
-
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
-
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
-
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
-
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
-
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
-
*.tar filter=lfs diff=lfs merge=lfs -text
|
| 29 |
-
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 30 |
-
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 31 |
-
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 32 |
-
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 33 |
-
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
-
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
-
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Dockerfile
CHANGED
|
@@ -1,20 +1,14 @@
|
|
| 1 |
-
FROM python:3.
|
| 2 |
|
| 3 |
WORKDIR /app
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
git \
|
| 9 |
-
&& rm -rf /var/lib/apt/lists/*
|
| 10 |
|
| 11 |
-
|
| 12 |
-
COPY
|
| 13 |
-
|
| 14 |
-
RUN pip3 install -r requirements.txt
|
| 15 |
|
| 16 |
EXPOSE 8501
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
ENTRYPOINT ["streamlit", "run", "src/streamlit_app.py", "--server.port=8501", "--server.address=0.0.0.0"]
|
|
|
|
| 1 |
+
FROM python:3.11-slim
|
| 2 |
|
| 3 |
WORKDIR /app
|
| 4 |
|
| 5 |
+
# Install python dependencies
|
| 6 |
+
COPY requirements.txt .
|
| 7 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
# Copy all frontend code
|
| 10 |
+
COPY . .
|
|
|
|
|
|
|
| 11 |
|
| 12 |
EXPOSE 8501
|
| 13 |
|
| 14 |
+
CMD ["streamlit", "run", "app.py", "--server.port=8501", "--server.address=0.0.0.0"]
|
|
|
|
|
|
api_client.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
#BACKEND_URL = "http://localhost:8000"
|
| 5 |
+
BACKEND_URL = os.getenv("BACKEND_URL", "https://your-username-pokemon-backend.hf.space")
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def predict(image_bytes: bytes, filename: str) -> dict:
|
| 9 |
+
try:
|
| 10 |
+
response = requests.post(
|
| 11 |
+
f"{BACKEND_URL}/predict",
|
| 12 |
+
files={"file": (filename, image_bytes, "image/png")}
|
| 13 |
+
)
|
| 14 |
+
response.raise_for_status()
|
| 15 |
+
return response.json()
|
| 16 |
+
except requests.exceptions.ConnectionError:
|
| 17 |
+
return {"error": "Could not connect to backend. Make sure it is running."}
|
| 18 |
+
except requests.exceptions.HTTPError as e:
|
| 19 |
+
return {"error": f"Backend error: {e}"}
|
| 20 |
+
except Exception as e:
|
| 21 |
+
return {"error": f"Unexpected error: {e}"}
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def health_check() -> bool:
|
| 25 |
+
try:
|
| 26 |
+
response = requests.get(f"{BACKEND_URL}/health", timeout=3)
|
| 27 |
+
return response.status_code == 200
|
| 28 |
+
except:
|
| 29 |
+
return False
|
app.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import io
|
| 4 |
+
|
| 5 |
+
from api_client import predict, health_check
|
| 6 |
+
from components.upload_section import render_upload_section
|
| 7 |
+
from components.display_structured_data import render_card_data
|
| 8 |
+
from components.similarity_grid import render_similarity_grid
|
| 9 |
+
|
| 10 |
+
# --------------------------------
|
| 11 |
+
# ----- PAGE CONFIG --------------
|
| 12 |
+
# --------------------------------
|
| 13 |
+
|
| 14 |
+
st.set_page_config(
|
| 15 |
+
page_title="Pokemon Card Image Processor",
|
| 16 |
+
page_icon="🃏",
|
| 17 |
+
layout="wide"
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
# --------------------------------
|
| 23 |
+
# ----- HEADER -------------------
|
| 24 |
+
# --------------------------------
|
| 25 |
+
|
| 26 |
+
st.title("🃏 Pokemon Card Image Processor")
|
| 27 |
+
st.caption("Upload a Pokémon card image to extract its data and find similar cards.")
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
# --------------------------------
|
| 32 |
+
# ----- BACKEND STATUS -----------
|
| 33 |
+
# --------------------------------
|
| 34 |
+
|
| 35 |
+
if not health_check():
|
| 36 |
+
st.error("⚠️ Backend is not running. Start it with: uvicorn app.main:app --reload")
|
| 37 |
+
st.stop()
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
# --------------------------------
|
| 42 |
+
# ----- UPLOAD -------------------
|
| 43 |
+
# --------------------------------
|
| 44 |
+
|
| 45 |
+
uploaded_file = render_upload_section()
|
| 46 |
+
|
| 47 |
+
if uploaded_file:
|
| 48 |
+
image_bytes = uploaded_file.read()
|
| 49 |
+
image = Image.open(io.BytesIO(image_bytes))
|
| 50 |
+
|
| 51 |
+
col1, col2 = st.columns([1, 2])
|
| 52 |
+
|
| 53 |
+
with col1:
|
| 54 |
+
st.subheader("Uploaded Card")
|
| 55 |
+
st.image(image, use_column_width=True)
|
| 56 |
+
|
| 57 |
+
with col2:
|
| 58 |
+
with st.spinner("Analyzing card..."):
|
| 59 |
+
result = predict(image_bytes, uploaded_file.name)
|
| 60 |
+
|
| 61 |
+
if "error" in result:
|
| 62 |
+
st.error(result["error"])
|
| 63 |
+
else:
|
| 64 |
+
render_card_data(result)
|
| 65 |
+
|
| 66 |
+
st.divider()
|
| 67 |
+
st.subheader("Similar Cards")
|
| 68 |
+
|
| 69 |
+
if "error" not in result:
|
| 70 |
+
render_similarity_grid(result.get("similar_cards", []))
|
components/display_structured_data.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
|
| 3 |
+
def render_card_data(result: dict):
|
| 4 |
+
st.subheader("Extracted Card Data")
|
| 5 |
+
|
| 6 |
+
col1, col2 = st.columns(2)
|
| 7 |
+
|
| 8 |
+
with col1:
|
| 9 |
+
st.metric("Name", result.get("name") or "Not detected")
|
| 10 |
+
st.metric("HP", result.get("hp") or "Not detected")
|
| 11 |
+
|
| 12 |
+
with col2:
|
| 13 |
+
types = result.get("types")
|
| 14 |
+
st.metric("Types", ", ".join(types) if types else "Not detected")
|
| 15 |
+
|
| 16 |
+
# Moves
|
| 17 |
+
moves = result.get("moves")
|
| 18 |
+
if moves:
|
| 19 |
+
st.markdown("**Moves**")
|
| 20 |
+
for move in moves:
|
| 21 |
+
with st.expander(f"{move.get('name')} — {move.get('damage', '?')} damage"):
|
| 22 |
+
st.write(move.get("text") or "No description available.")
|
| 23 |
+
else:
|
| 24 |
+
st.metric("Moves", "Not detected")
|
components/similarity_grid.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import io
|
| 5 |
+
|
| 6 |
+
def render_similarity_grid(similar_cards: list):
|
| 7 |
+
if not similar_cards:
|
| 8 |
+
st.info("No similar cards found.")
|
| 9 |
+
return
|
| 10 |
+
|
| 11 |
+
cols = st.columns(len(similar_cards))
|
| 12 |
+
|
| 13 |
+
for col, card in zip(cols, similar_cards):
|
| 14 |
+
with col:
|
| 15 |
+
# Load and display card image
|
| 16 |
+
image_url = card.get("image_url")
|
| 17 |
+
if image_url:
|
| 18 |
+
try:
|
| 19 |
+
response = requests.get(image_url, timeout=5)
|
| 20 |
+
image = Image.open(io.BytesIO(response.content))
|
| 21 |
+
st.image(image, use_column_width=True)
|
| 22 |
+
except:
|
| 23 |
+
st.write("Image unavailable")
|
| 24 |
+
|
| 25 |
+
st.markdown(f"**{card.get('name')}**")
|
| 26 |
+
st.caption(f"Set: {card.get('set') or 'Unknown'}")
|
| 27 |
+
st.caption(f"Types: {', '.join(card.get('types') or [])}")
|
| 28 |
+
st.caption(f"Rarity: {card.get('rarity') or 'Unknown'}")
|
| 29 |
+
st.caption(f"Score: {round(card.get('score', 0), 3)}")
|
components/upload_section.py
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
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 |
+
)
|
| 9 |
+
return uploaded_file
|
requirements.txt
CHANGED
|
@@ -1,3 +1,4 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
requests
|
| 3 |
+
pillow
|
| 4 |
+
os
|
src/streamlit_app.py
DELETED
|
@@ -1,40 +0,0 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
-
import streamlit as st
|
| 5 |
-
|
| 6 |
-
"""
|
| 7 |
-
# Welcome to Streamlit!
|
| 8 |
-
|
| 9 |
-
Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
|
| 10 |
-
If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
|
| 11 |
-
forums](https://discuss.streamlit.io).
|
| 12 |
-
|
| 13 |
-
In the meantime, below is an example of what you can do with just a few lines of code:
|
| 14 |
-
"""
|
| 15 |
-
|
| 16 |
-
num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
|
| 17 |
-
num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
|
| 18 |
-
|
| 19 |
-
indices = np.linspace(0, 1, num_points)
|
| 20 |
-
theta = 2 * np.pi * num_turns * indices
|
| 21 |
-
radius = indices
|
| 22 |
-
|
| 23 |
-
x = radius * np.cos(theta)
|
| 24 |
-
y = radius * np.sin(theta)
|
| 25 |
-
|
| 26 |
-
df = pd.DataFrame({
|
| 27 |
-
"x": x,
|
| 28 |
-
"y": y,
|
| 29 |
-
"idx": indices,
|
| 30 |
-
"rand": np.random.randn(num_points),
|
| 31 |
-
})
|
| 32 |
-
|
| 33 |
-
st.altair_chart(alt.Chart(df, height=700, width=700)
|
| 34 |
-
.mark_point(filled=True)
|
| 35 |
-
.encode(
|
| 36 |
-
x=alt.X("x", axis=None),
|
| 37 |
-
y=alt.Y("y", axis=None),
|
| 38 |
-
color=alt.Color("idx", legend=None, scale=alt.Scale()),
|
| 39 |
-
size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
|
| 40 |
-
))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|