Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import os
|
3 |
+
import random
|
4 |
+
import hashlib
|
5 |
+
import json
|
6 |
+
from datetime import datetime
|
7 |
+
|
8 |
+
# Function to generate a short user hash
|
9 |
+
def generate_user_hash():
|
10 |
+
if 'user_hash' not in st.session_state:
|
11 |
+
# Generate a unique identifier for the session
|
12 |
+
session_id = str(random.getrandbits(128))
|
13 |
+
# Create a hash of the session ID
|
14 |
+
hash_object = hashlib.md5(session_id.encode())
|
15 |
+
st.session_state['user_hash'] = hash_object.hexdigest()[:8] # Use first 8 characters of the hash
|
16 |
+
return st.session_state['user_hash']
|
17 |
+
|
18 |
+
# Function to load vote history from file
|
19 |
+
def load_vote_history():
|
20 |
+
try:
|
21 |
+
with open('vote_history.json', 'r') as f:
|
22 |
+
return json.load(f)
|
23 |
+
except FileNotFoundError:
|
24 |
+
return {}
|
25 |
+
|
26 |
+
# Function to save vote history to file
|
27 |
+
def save_vote_history(history):
|
28 |
+
with open('vote_history.json', 'w') as f:
|
29 |
+
json.dump(history, f)
|
30 |
+
|
31 |
+
# Function to update vote history in Markdown file
|
32 |
+
def update_history_md(image_name, user_hash):
|
33 |
+
with open('history.md', 'a') as f:
|
34 |
+
f.write(f"- {datetime.now().strftime('%Y-%m-%d %H:%M:%S')} - User {user_hash} voted for {image_name}\n")
|
35 |
+
|
36 |
+
# Function to display images
|
37 |
+
def display_images(image_dir):
|
38 |
+
col1, col2 = st.columns(2)
|
39 |
+
valid_extensions = ('.png', '.jpg', '.jpeg', '.gif', '.bmp', '.tiff', '.webp')
|
40 |
+
images = [f for f in os.listdir(image_dir) if f.lower().endswith(valid_extensions)]
|
41 |
+
|
42 |
+
if len(images) < 2:
|
43 |
+
st.error("Not enough images in the directory.")
|
44 |
+
return
|
45 |
+
|
46 |
+
image1, image2 = random.sample(images, 2)
|
47 |
+
with col1:
|
48 |
+
st.image(os.path.join(image_dir, image1))
|
49 |
+
if st.button(f"Upvote {image1}", key=f"upvote_{image1}"):
|
50 |
+
handle_vote(image1)
|
51 |
+
|
52 |
+
with col2:
|
53 |
+
st.image(os.path.join(image_dir, image2))
|
54 |
+
if st.button(f"Upvote {image2}", key=f"upvote_{image2}"):
|
55 |
+
handle_vote(image2)
|
56 |
+
|
57 |
+
# Function to handle voting
|
58 |
+
def handle_vote(image_name):
|
59 |
+
user_hash = generate_user_hash()
|
60 |
+
vote_history = load_vote_history()
|
61 |
+
|
62 |
+
if image_name not in vote_history:
|
63 |
+
vote_history[image_name] = {'votes': 0, 'users': []}
|
64 |
+
|
65 |
+
if user_hash not in vote_history[image_name]['users']:
|
66 |
+
vote_history[image_name]['votes'] += 1
|
67 |
+
vote_history[image_name]['users'].append(user_hash)
|
68 |
+
save_vote_history(vote_history)
|
69 |
+
update_history_md(image_name, user_hash)
|
70 |
+
st.success(f"Upvoted {image_name}! Total votes: {vote_history[image_name]['votes']}")
|
71 |
+
else:
|
72 |
+
st.warning("You've already voted for this image.")
|
73 |
+
|
74 |
+
# Function to show vote history in sidebar
|
75 |
+
def show_vote_history():
|
76 |
+
st.sidebar.title("Vote History")
|
77 |
+
vote_history = load_vote_history()
|
78 |
+
|
79 |
+
# Sort images by vote count in descending order
|
80 |
+
sorted_images = sorted(vote_history.items(), key=lambda x: x[1]['votes'], reverse=True)
|
81 |
+
|
82 |
+
for i, (image_name, data) in enumerate(sorted_images):
|
83 |
+
votes = data['votes']
|
84 |
+
st.sidebar.write(f"{image_name}: {votes} votes")
|
85 |
+
|
86 |
+
# Display top 3 images
|
87 |
+
if i < 3:
|
88 |
+
st.sidebar.image(os.path.join('.', image_name), caption=f"#{i+1}: {image_name}", width=150)
|
89 |
+
|
90 |
+
# Main function
|
91 |
+
def main():
|
92 |
+
st.title("Image Voting App")
|
93 |
+
|
94 |
+
# Set up the sidebar for vote history
|
95 |
+
show_vote_history()
|
96 |
+
|
97 |
+
# Display images for voting
|
98 |
+
image_dir = '.' # Current directory where the app is running
|
99 |
+
display_images(image_dir)
|
100 |
+
|
101 |
+
# Display user hash
|
102 |
+
st.sidebar.write(f"Your User ID: {generate_user_hash()}")
|
103 |
+
|
104 |
+
# Run the app
|
105 |
+
if __name__ == "__main__":
|
106 |
+
main()
|