Update app.py
Browse files
app.py
CHANGED
|
@@ -4,8 +4,6 @@ import random
|
|
| 4 |
import hashlib
|
| 5 |
import json
|
| 6 |
from datetime import datetime
|
| 7 |
-
from collections import Counter
|
| 8 |
-
import re
|
| 9 |
import time
|
| 10 |
|
| 11 |
# Your existing quotes list
|
|
@@ -112,6 +110,14 @@ quotes = [
|
|
| 112 |
{"Number": 100, "Quote Topic": "Love ❤️", "Quote": "Through love, we find connection, unity, and the essence of existence."}
|
| 113 |
]
|
| 114 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
# Function to generate a short user hash
|
| 116 |
def generate_user_hash():
|
| 117 |
if 'user_hash' not in st.session_state:
|
|
@@ -138,19 +144,6 @@ def update_history_md(image_name, user_hash):
|
|
| 138 |
with open('history.md', 'a') as f:
|
| 139 |
f.write(f"- {datetime.now().strftime('%Y-%m-%d %H:%M:%S')} - User {user_hash} voted for {image_name}\n")
|
| 140 |
|
| 141 |
-
# Function to extract words from file name
|
| 142 |
-
def extract_words(filename):
|
| 143 |
-
words = re.findall(r'\w+', filename.lower())
|
| 144 |
-
return [word for word in words if len(word) > 2]
|
| 145 |
-
|
| 146 |
-
# Function to update word counts
|
| 147 |
-
def update_word_counts(history, image_name):
|
| 148 |
-
words = extract_words(image_name)
|
| 149 |
-
for word in words:
|
| 150 |
-
if word not in history['words']:
|
| 151 |
-
history['words'][word] = 0
|
| 152 |
-
history['words'][word] += 1
|
| 153 |
-
|
| 154 |
# Function to display images
|
| 155 |
def display_images(image_dir):
|
| 156 |
col1, col2 = st.columns(2)
|
|
@@ -189,7 +182,6 @@ def handle_vote(image_name):
|
|
| 189 |
vote_history['images'][image_name]['users'][user_hash] += 1
|
| 190 |
vote_history['users'][user_hash] += 1
|
| 191 |
|
| 192 |
-
update_word_counts(vote_history, image_name)
|
| 193 |
save_vote_history(vote_history)
|
| 194 |
update_history_md(image_name, user_hash)
|
| 195 |
st.success(f"Upvoted {image_name}! Total votes: {vote_history['images'][image_name]['votes']}")
|
|
@@ -220,28 +212,16 @@ def show_vote_history():
|
|
| 220 |
# Display top 3 images
|
| 221 |
if i < 3:
|
| 222 |
st.sidebar.image(os.path.join('.', image_name), caption=f"#{i+1}: {image_name}", width=150)
|
| 223 |
-
|
| 224 |
-
# Display top 3 words
|
| 225 |
-
st.sidebar.subheader("Top 3 Words in Voted Images")
|
| 226 |
-
top_words = sorted(vote_history['words'].items(), key=lambda x: x[1], reverse=True)[:3]
|
| 227 |
-
for word, count in top_words:
|
| 228 |
-
st.sidebar.write(f"{word}: {count} occurrences")
|
| 229 |
-
|
| 230 |
-
# Function to display a random quote
|
| 231 |
-
def display_random_quote():
|
| 232 |
-
quote = random.choice(quotes)
|
| 233 |
-
st.markdown(f"### {quote['Number']}. {quote['Quote Topic']}")
|
| 234 |
-
st.markdown(quote['Quote'])
|
| 235 |
|
| 236 |
# Main function
|
| 237 |
def main():
|
| 238 |
st.title("Image Voting App with Quotes")
|
| 239 |
|
| 240 |
# Initialize session state variables
|
| 241 |
-
if
|
| 242 |
-
st.session_state.last_interaction = time.time()
|
| 243 |
-
if 'auto_repeat' not in st.session_state:
|
| 244 |
st.session_state.auto_repeat = "On"
|
|
|
|
|
|
|
| 245 |
|
| 246 |
# Set up the sidebar for vote history and user stats
|
| 247 |
show_vote_history()
|
|
@@ -254,31 +234,26 @@ def main():
|
|
| 254 |
st.sidebar.subheader("Your User ID")
|
| 255 |
st.sidebar.write(generate_user_hash())
|
| 256 |
|
| 257 |
-
# Add refresh rate slider
|
| 258 |
-
refresh_rate = st.sidebar.slider("Refresh rate (seconds)", 0, 120, 30,
|
| 259 |
-
help="Set to 0 for no auto-refresh")
|
| 260 |
-
|
| 261 |
# AutoRepeat radio button
|
| 262 |
-
st.session_state.auto_repeat = st.
|
| 263 |
|
| 264 |
-
# Display
|
| 265 |
st.subheader("Quote of the Moment")
|
| 266 |
-
|
| 267 |
|
| 268 |
-
#
|
| 269 |
-
if
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
st.write(f"Time until next refresh: {time_left} seconds")
|
| 278 |
|
| 279 |
# Manual refresh button
|
| 280 |
if st.button("Refresh Now"):
|
| 281 |
-
st.session_state.
|
| 282 |
st.rerun()
|
| 283 |
|
| 284 |
# Run the app
|
|
|
|
| 4 |
import hashlib
|
| 5 |
import json
|
| 6 |
from datetime import datetime
|
|
|
|
|
|
|
| 7 |
import time
|
| 8 |
|
| 9 |
# Your existing quotes list
|
|
|
|
| 110 |
{"Number": 100, "Quote Topic": "Love ❤️", "Quote": "Through love, we find connection, unity, and the essence of existence."}
|
| 111 |
]
|
| 112 |
|
| 113 |
+
# Function to display a quote
|
| 114 |
+
def display_quote(index):
|
| 115 |
+
number = quotes[index]['Number']
|
| 116 |
+
topic = quotes[index]['Quote Topic']
|
| 117 |
+
quote = quotes[index]['Quote']
|
| 118 |
+
st.markdown(f"### {number}. {topic}")
|
| 119 |
+
st.markdown(quote)
|
| 120 |
+
|
| 121 |
# Function to generate a short user hash
|
| 122 |
def generate_user_hash():
|
| 123 |
if 'user_hash' not in st.session_state:
|
|
|
|
| 144 |
with open('history.md', 'a') as f:
|
| 145 |
f.write(f"- {datetime.now().strftime('%Y-%m-%d %H:%M:%S')} - User {user_hash} voted for {image_name}\n")
|
| 146 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
# Function to display images
|
| 148 |
def display_images(image_dir):
|
| 149 |
col1, col2 = st.columns(2)
|
|
|
|
| 182 |
vote_history['images'][image_name]['users'][user_hash] += 1
|
| 183 |
vote_history['users'][user_hash] += 1
|
| 184 |
|
|
|
|
| 185 |
save_vote_history(vote_history)
|
| 186 |
update_history_md(image_name, user_hash)
|
| 187 |
st.success(f"Upvoted {image_name}! Total votes: {vote_history['images'][image_name]['votes']}")
|
|
|
|
| 212 |
# Display top 3 images
|
| 213 |
if i < 3:
|
| 214 |
st.sidebar.image(os.path.join('.', image_name), caption=f"#{i+1}: {image_name}", width=150)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
|
| 216 |
# Main function
|
| 217 |
def main():
|
| 218 |
st.title("Image Voting App with Quotes")
|
| 219 |
|
| 220 |
# Initialize session state variables
|
| 221 |
+
if "auto_repeat" not in st.session_state:
|
|
|
|
|
|
|
| 222 |
st.session_state.auto_repeat = "On"
|
| 223 |
+
if "current_index" not in st.session_state:
|
| 224 |
+
st.session_state.current_index = random.randint(0, len(quotes)-1)
|
| 225 |
|
| 226 |
# Set up the sidebar for vote history and user stats
|
| 227 |
show_vote_history()
|
|
|
|
| 234 |
st.sidebar.subheader("Your User ID")
|
| 235 |
st.sidebar.write(generate_user_hash())
|
| 236 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
# AutoRepeat radio button
|
| 238 |
+
st.session_state.auto_repeat = st.radio("🔄 AutoRepeat", ["On", "Off"], horizontal=True)
|
| 239 |
|
| 240 |
+
# Display the current quote
|
| 241 |
st.subheader("Quote of the Moment")
|
| 242 |
+
display_quote(st.session_state.current_index)
|
| 243 |
|
| 244 |
+
# Timer logic
|
| 245 |
+
if st.session_state.auto_repeat == "On":
|
| 246 |
+
timer_placeholder = st.empty()
|
| 247 |
+
for i in range(10, 0, -1):
|
| 248 |
+
timer_placeholder.text(f"Time left: {i} seconds")
|
| 249 |
+
time.sleep(1)
|
| 250 |
+
if i == 1:
|
| 251 |
+
st.session_state.current_index = random.randint(0, len(quotes)-1)
|
| 252 |
+
st.rerun()
|
|
|
|
| 253 |
|
| 254 |
# Manual refresh button
|
| 255 |
if st.button("Refresh Now"):
|
| 256 |
+
st.session_state.current_index = random.randint(0, len(quotes)-1)
|
| 257 |
st.rerun()
|
| 258 |
|
| 259 |
# Run the app
|