Nipun commited on
Commit
0483091
·
1 Parent(s): 43928d2
Files changed (2) hide show
  1. app.py +55 -38
  2. requirements.txt +2 -1
app.py CHANGED
@@ -1,39 +1,56 @@
1
  import streamlit as st
2
- import os, json
3
-
4
- IMG_DIR = "images"
5
- VOTE_FILE = "votes.json"
6
-
7
- # Ensure votes.json exists with image names
8
- if not os.path.exists(VOTE_FILE):
9
- votes = {}
10
-
11
- if os.path.exists(VOTE_FILE):
12
- with open(VOTE_FILE, 'r') as f:
13
- votes = json.load(f)
14
-
15
- # Initialize votes for new images
16
- for img in os.listdir(IMG_DIR):
17
- if img not in votes:
18
- votes[img] = 0
19
-
20
- # Save updated votes.json
21
- with open(VOTE_FILE, 'w') as f:
22
- json.dump(votes, f, indent=2)
23
-
24
- st.title("Vote for the Best Logo")
25
-
26
- # Display images and vote buttons
27
- for img in sorted(votes.keys()):
28
- st.image(os.path.join(IMG_DIR, img), width=200)
29
- if st.button(f"Vote for {img}"):
30
- votes[img] += 1
31
- with open(VOTE_FILE, 'w') as f:
32
- json.dump(votes, f, indent=2)
33
- st.success(f"Voted for {img}")
34
- st.experimental_rerun()
35
-
36
- # Display current vote tally
37
- st.header("Live Results")
38
- for img, count in sorted(votes.items(), key=lambda x: -x[1]):
39
- st.markdown(f"**{img}**: {count} votes")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ from PIL import Image
3
+ import os
4
+
5
+ # Title of the app
6
+ st.title("Logo Voter App")
7
+
8
+ # Path to the images folder
9
+ images_folder = "images"
10
+
11
+ # Check if the images folder exists
12
+ if not os.path.exists(images_folder):
13
+ st.error("The 'images' folder is missing!")
14
+ st.stop()
15
+
16
+ # Get a list of all image files in the folder
17
+ image_files = [f for f in os.listdir(images_folder) if f.endswith(('png', 'jpg', 'jpeg'))]
18
+
19
+ # If no images found, show a message
20
+ if not image_files:
21
+ st.error("No images found in the 'images' folder.")
22
+ st.stop()
23
+
24
+ # Display images in a grid layout
25
+ st.write("Uploaded Images:")
26
+
27
+ # Define the number of columns for the grid
28
+ columns = st.columns(3) # 3 columns for layout
29
+
30
+ # Initialize a dictionary to store votes for each image
31
+ votes = {image_file: 0 for image_file in image_files}
32
+
33
+ # Loop through the images and display them in the columns
34
+ for idx, image_file in enumerate(image_files):
35
+ with columns[idx % 3]: # Loop through columns
36
+ # Open the image
37
+ img_path = os.path.join(images_folder, image_file)
38
+ img = Image.open(img_path)
39
+
40
+ # Display the image
41
+ st.image(img, use_column_width=True, caption=image_file)
42
+
43
+ # Voting buttons
44
+ col1, col2 = st.columns([1, 1])
45
+ with col1:
46
+ if st.button(f"Like {image_file}", key=f"like_{idx}"):
47
+ votes[image_file] += 1
48
+ with col2:
49
+ if st.button(f"Dislike {image_file}", key=f"dislike_{idx}"):
50
+ votes[image_file] -= 1
51
+
52
+ # Show voting results
53
+ st.write("### Voting Results:")
54
+ for image_file, vote_count in votes.items():
55
+ st.write(f"**{image_file}:** {vote_count} votes")
56
+
requirements.txt CHANGED
@@ -1 +1,2 @@
1
- streamlit
 
 
1
+ streamlit
2
+ pillow