File size: 2,907 Bytes
aae95da
3dfb668
 
 
61b23b1
 
 
 
 
 
 
 
3dfb668
 
 
 
 
 
 
 
 
 
 
61b23b1
 
 
 
3dfb668
 
 
 
 
 
e5d4b6d
3dfb668
 
e5d4b6d
3dfb668
e5d4b6d
3dfb668
e5d4b6d
 
 
3dfb668
e5d4b6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aae95da
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
80
81
82
import streamlit as st
from similarity import PokemonSimilarity
from PIL import Image
import io
import logging

# Configure logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)

# Set page config first
st.set_page_config(
    page_title="Pokemon Similarity Finder",
    page_icon="๐ŸŽฎ",
    layout="centered"
)

# Initialize the similarity engine
@st.cache_resource
def get_similarity_engine():
    logger.info("Initializing similarity engine...")
    engine = PokemonSimilarity()
    logger.info("Similarity engine initialized successfully")
    return engine

similarity_engine = get_similarity_engine()

# Title and description
st.title("๐ŸŽฎ Pokemon Similarity Finder")
st.markdown("""
Upload an image of a Pokemon or provide an image URL and we'll find the closest match in our database!
""")

# File uploader and URL input
uploaded_file = st.file_uploader("Choose a Pokemon image...", type=["jpg", "jpeg", "png"])
image_url = st.text_input("Or enter an image URL:")

# Determine which input to use
image_input = None
input_type = None
if uploaded_file is not None:
    image_input = uploaded_file.getvalue()
    input_type = 'file'
elif image_url:
    image_input = image_url
    input_type = 'url'

if image_input is not None:
    if input_type == 'file':
        logger.info(f"File uploaded: {uploaded_file.name}")
        try:
            # Display the uploaded image
            image = Image.open(io.BytesIO(image_input))
            st.image(image, caption="Uploaded Image", use_column_width=True)
            logger.info("Successfully displayed uploaded image")
        except Exception as e:
            logger.error(f"Error loading image: {str(e)}")
            st.error(f"โŒ Error loading image: {str(e)}")
            st.info("Please make sure you've uploaded a valid image file.")
    elif input_type == 'url':
        logger.info(f"Image URL provided: {image_url}")
        st.image(image_url, caption="Image from URL", use_column_width=True)

    # Add a button to trigger the similarity search
    if st.button("Find Similar Pokemon"):
        logger.info("Find Similar Pokemon button clicked")
        with st.spinner("Analyzing image..."):
            try:
                logger.info(f"Finding closest Pokemon match using {input_type} input...")
                pokemon_name = similarity_engine.find_closest_pokemon(image_input)
                logger.info(f"Found closest Pokemon: {pokemon_name}")
                st.success(f"๐ŸŽฏ The closest Pokemon is: **{pokemon_name}**")
                st.balloons()
            except Exception as e:
                logger.error(f"Error during Pokemon matching: {str(e)}")
                st.error(f"โŒ Error: {str(e)}")
                st.info("Please try uploading a different image, using a different URL, or try again later.")