SmokeyBandit commited on
Commit
6ff8317
Β·
verified Β·
1 Parent(s): 93474f9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +107 -14
app.py CHANGED
@@ -6,6 +6,10 @@ from datetime import datetime
6
 
7
  FLIP_LOG = "flips.md"
8
  is_flipping = False
 
 
 
 
9
 
10
  # Make sure the flip log exists
11
  with open(FLIP_LOG, "a") as f:
@@ -13,39 +17,128 @@ with open(FLIP_LOG, "a") as f:
13
 
14
  # Flip forever in the background
15
  def flip_forever():
 
16
  while True:
17
  result = random.choice(["Heads", "Tails"])
18
  timestamp = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC")
 
 
 
 
 
 
 
 
19
  with open(FLIP_LOG, "a") as f:
20
  f.write(f"- {timestamp}: {result}\n")
 
21
  time.sleep(1)
22
 
23
  # Trigger flipping once
24
  def start_flipping():
25
- global is_flipping
26
  if not is_flipping:
27
  is_flipping = True
 
28
  threading.Thread(target=flip_forever, daemon=True).start()
29
  return "Started flipping! No going back now."
30
  return "Already flipping."
31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  # Load last 20 flips from the log
33
  def get_latest_flips():
34
- with open(FLIP_LOG, "r") as f:
35
- lines = f.readlines()
36
- return "".join(lines[-20:]) if lines else "No flips yet."
 
 
 
37
 
38
- # Build UI
39
- with gr.Blocks() as app:
40
- gr.Markdown("# πŸͺ™ Eternal Coin Flipper")
41
- gr.Markdown("Click once. Flip forever. Simple as that.")
42
 
43
- flip_button = gr.Button("Start Flipping")
44
- refresh_button = gr.Button("Refresh Log")
45
- flip_log = gr.Textbox(label="Recent Flips", lines=20)
 
 
 
 
 
 
 
 
 
 
 
 
46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  flip_button.click(fn=start_flipping, outputs=None)
48
- refresh_button.click(fn=get_latest_flips, outputs=flip_log)
49
- app.load(fn=get_latest_flips, outputs=flip_log)
 
 
 
 
 
50
 
51
- app.launch()
 
 
6
 
7
  FLIP_LOG = "flips.md"
8
  is_flipping = False
9
+ start_time = None
10
+ heads_count = 0
11
+ tails_count = 0
12
+ flip_lock = threading.Lock()
13
 
14
  # Make sure the flip log exists
15
  with open(FLIP_LOG, "a") as f:
 
17
 
18
  # Flip forever in the background
19
  def flip_forever():
20
+ global heads_count, tails_count
21
  while True:
22
  result = random.choice(["Heads", "Tails"])
23
  timestamp = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC")
24
+
25
+ # Update counters thread-safely
26
+ with flip_lock:
27
+ if result == "Heads":
28
+ heads_count += 1
29
+ else:
30
+ tails_count += 1
31
+
32
  with open(FLIP_LOG, "a") as f:
33
  f.write(f"- {timestamp}: {result}\n")
34
+
35
  time.sleep(1)
36
 
37
  # Trigger flipping once
38
  def start_flipping():
39
+ global is_flipping, start_time
40
  if not is_flipping:
41
  is_flipping = True
42
+ start_time = datetime.utcnow()
43
  threading.Thread(target=flip_forever, daemon=True).start()
44
  return "Started flipping! No going back now."
45
  return "Already flipping."
46
 
47
+ # Get runtime duration
48
+ def get_runtime():
49
+ if start_time is None:
50
+ return "Not started yet"
51
+
52
+ current_time = datetime.utcnow()
53
+ runtime = current_time - start_time
54
+
55
+ # Format runtime nicely
56
+ total_seconds = int(runtime.total_seconds())
57
+ hours = total_seconds // 3600
58
+ minutes = (total_seconds % 3600) // 60
59
+ seconds = total_seconds % 60
60
+
61
+ if hours > 0:
62
+ return f"{hours}h {minutes}m {seconds}s"
63
+ elif minutes > 0:
64
+ return f"{minutes}m {seconds}s"
65
+ else:
66
+ return f"{seconds}s"
67
+
68
+ # Get current tally
69
+ def get_tally():
70
+ with flip_lock:
71
+ total_flips = heads_count + tails_count
72
+ if total_flips == 0:
73
+ return "No flips yet"
74
+
75
+ heads_percent = (heads_count / total_flips) * 100
76
+ tails_percent = (tails_count / total_flips) * 100
77
+
78
+ return f"πŸͺ™ **Total Flips:** {total_flips}\n" \
79
+ f"πŸ‘‘ **Heads:** {heads_count} ({heads_percent:.1f}%)\n" \
80
+ f"πŸ”„ **Tails:** {tails_count} ({tails_percent:.1f}%)"
81
+
82
  # Load last 20 flips from the log
83
  def get_latest_flips():
84
+ try:
85
+ with open(FLIP_LOG, "r") as f:
86
+ lines = f.readlines()
87
+ return "".join(lines[-20:]) if lines else "No flips yet."
88
+ except FileNotFoundError:
89
+ return "No flips yet."
90
 
91
+ # Combined update function for all stats
92
+ def update_all_stats():
93
+ return get_tally(), get_runtime(), get_latest_flips()
 
94
 
95
+ # Load existing data on startup (count flips from log)
96
+ def load_existing_data():
97
+ global heads_count, tails_count
98
+ try:
99
+ with open(FLIP_LOG, "r") as f:
100
+ lines = f.readlines()
101
+
102
+ # Count existing flips
103
+ for line in lines:
104
+ if ": Heads" in line:
105
+ heads_count += 1
106
+ elif ": Tails" in line:
107
+ tails_count += 1
108
+ except FileNotFoundError:
109
+ pass
110
 
111
+ # Load existing data on startup
112
+ load_existing_data()
113
+
114
+ # Build UI
115
+ with gr.Blocks(theme=gr.themes.Soft()) as app:
116
+ gr.Markdown("# πŸͺ™ Eternal Coin Flipper")
117
+ gr.Markdown("Click once. Flip forever. Track everything.")
118
+
119
+ with gr.Row():
120
+ with gr.Column():
121
+ flip_button = gr.Button("πŸš€ Start Flipping", variant="primary", size="lg")
122
+ refresh_button = gr.Button("πŸ”„ Refresh Stats", variant="secondary")
123
+
124
+ with gr.Column():
125
+ # Stats display
126
+ tally_display = gr.Markdown(value=get_tally(), label="Flip Tally")
127
+ runtime_display = gr.Markdown(value=f"⏱️ **Runtime:** {get_runtime()}")
128
+
129
+ # Flip log
130
+ gr.Markdown("## Recent Flips")
131
+ flip_log = gr.Textbox(label="Last 20 Flips", lines=15, max_lines=20)
132
+
133
+ # Event handlers
134
  flip_button.click(fn=start_flipping, outputs=None)
135
+ refresh_button.click(
136
+ fn=update_all_stats,
137
+ outputs=[tally_display, runtime_display, flip_log]
138
+ )
139
+
140
+ # Auto-refresh every 5 seconds when page loads
141
+ app.load(fn=update_all_stats, outputs=[tally_display, runtime_display, flip_log])
142
 
143
+ if __name__ == "__main__":
144
+ app.launch()