File size: 2,589 Bytes
1e77d79
 
 
 
b1ad51c
1e77d79
b1ad51c
adf1925
1e77d79
 
b1ad51c
 
 
1e77d79
b1ad51c
 
 
 
adf1925
 
 
1e77d79
 
9dac053
1e77d79
 
 
 
9dac053
1e77d79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b1ad51c
1e77d79
 
 
 
 
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
import streamlit as st
import os
import random

# πŸ–ΌοΈ 1. Display two columns, each with a random image from the current directory
def display_images(image_dir):
    """πŸ–ΌοΈ Function 1: Displays two random images side by side for voting, filtered to only show image files."""

    col1, col2 = st.columns(2)

    # πŸ—‚οΈ Load and filter only image files from the directory
    valid_extensions = ('.png', '.jpg', '.jpeg', '.gif', '.bmp', '.tiff', '.webp')  # Common image file types
    images = [f for f in os.listdir(image_dir) if f.lower().endswith(valid_extensions)]
    
    if len(images) < 2:
        st.error("Not enough images in the directory.")
        return

    # πŸ“Έ Randomly select two *unique* images
    image1, image2 = random.sample(images, 2)

    with col1:
        st.image(os.path.join(image_dir, image1))
        if st.button(f"Upvote {image1}", key=f"upvote_{image1}"):  # Use image filename as key
            handle_vote(image1)
    
    with col2:
        st.image(os.path.join(image_dir, image2))
        if st.button(f"Upvote {image2}", key=f"upvote_{image2}"):  # Use image filename as key
            handle_vote(image2)

# 🎯 2. Handle the voting logic and store the history
def handle_vote(image_name):
    """🎯 Function 2: Handles voting and stores vote history."""
    # βœ… Save the upvote to session state or database
    if 'vote_history' not in st.session_state:
        st.session_state['vote_history'] = {}
    
    if image_name in st.session_state['vote_history']:
        st.session_state['vote_history'][image_name] += 1
    else:
        st.session_state['vote_history'][image_name] = 1

    st.success(f"Upvoted {image_name}! Total votes: {st.session_state['vote_history'][image_name]}")

# πŸ“Š 3. Sidebar to show vote history
def show_vote_history():
    """πŸ“Š Function 3: Displays the vote history in the sidebar."""
    st.sidebar.title("Vote History")
    if 'vote_history' in st.session_state:
        for image_name, votes in st.session_state['vote_history'].items():
            st.sidebar.write(f"{image_name}: {votes} votes")
    else:
        st.sidebar.write("No votes yet!")

# βš™οΈ 4. Main function to structure app execution
def main():
    """βš™οΈ Function 4: Main function to run the app."""
    st.title("Image Voting App")
    
    # 🎨 Set up the sidebar for vote history
    show_vote_history()
    
    # πŸ–ΌοΈ Display images for voting
    image_dir = '.'  # Current directory where the app is running
    display_images(image_dir)

# πŸš€ 5. Run the app
if __name__ == "__main__":
    main()